@carbonorm/carbonnode 1.5.2 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import axiosInstance from "api/axiosInstance";
2
2
  import convertForRequestBody from "api/convertForRequestBody";
3
- import {iC6RestfulModel} from "api/interfaces/ormInterfaces";
3
+ import {iC6RestfulModel, iConstraint, iRestApiFunctions, tC6RestApi} from "api/interfaces/ormInterfaces";
4
4
  import {AxiosInstance, AxiosPromise, AxiosResponse} from "axios";
5
5
 
6
6
  import {toast} from "react-toastify";
@@ -105,10 +105,19 @@ type DeepPartialAny<T> = {
105
105
  [P in keyof T]?: T[P] extends AnyObject ? DeepPartialAny<T[P]> : any
106
106
  }
107
107
 
108
+ export enum eFetchDependencies {
109
+ NONE = 0,
110
+ REFERENCED = 1,
111
+ CHILDREN = 1,
112
+ REFERENCES = 2,
113
+ PARENTS = 2,
114
+ ALL = 3
115
+ }
116
+
108
117
  export type iAPI<RestTableInterfaces extends { [key: string]: any }> = RestTableInterfaces & {
109
118
  dataInsertMultipleRows?: RestTableInterfaces[],
110
119
  cacheResults?: boolean, // aka ignoreCache
111
- fetchDependencies?: boolean,
120
+ fetchDependencies?: eFetchDependencies|Promise<apiReturn<iGetC6RestResponse<any>>>[],
112
121
  debug?: boolean,
113
122
  success?: string | ((r: AxiosResponse) => (string | void)),
114
123
  error?: string | ((r: AxiosResponse) => (string | void)),
@@ -297,11 +306,15 @@ export interface iPutC6RestResponse<RestData = any, RequestData = any> extends i
297
306
  }
298
307
 
299
308
  export interface iC6Object {
309
+ C6VERSION: string,
300
310
  TABLES: {
301
311
  [key: string]: iC6RestfulModel &
302
312
  { [key: string]: string | number }
303
313
  },
304
314
  PREFIX: string,
315
+ IMPORT: (tableName: string) => Promise<{
316
+ default: iRestApiFunctions
317
+ }>,
305
318
 
306
319
  [key: string]: any
307
320
  }
@@ -325,6 +338,7 @@ interface iRest<CustomAndRequiredFields extends { [key: string]: any }, RestTabl
325
338
  }, ResponseDataType = any,
326
339
  RestShortTableNames extends string = any> {
327
340
  C6: iC6Object,
341
+ C6RestApi: () => tC6RestApi,
328
342
  axios?: AxiosInstance,
329
343
  restURL?: string,
330
344
  withCredentials?: boolean,
@@ -364,6 +378,7 @@ export default function restApi<
364
378
  RestShortTableNames extends string = any
365
379
  >({
366
380
  C6,
381
+ C6RestApi,
367
382
  axios = axiosInstance,
368
383
  restURL = '/rest/',
369
384
  withCredentials = true,
@@ -751,8 +766,9 @@ export default function restApi<
751
766
  // we had removed the value from the request to add to the URI.
752
767
  addBackPK?.(); // adding back so post-processing methods work
753
768
 
769
+ // returning the promise with this then is important for tests. todo - we could make that optional.
754
770
  // https://rapidapi.com/guides/axios-async-await
755
- return axiosActiveRequest.then(response => {
771
+ return axiosActiveRequest.then(async (response) => {
756
772
 
757
773
  if (typeof response.data === 'string') {
758
774
 
@@ -768,58 +784,145 @@ export default function restApi<
768
784
 
769
785
  }
770
786
 
787
+ if (cachingConfirmed) {
788
+
789
+ const cacheIndex = apiRequestCache.findIndex(cache => cache.requestArgumentsSerialized === querySerialized);
790
+
791
+ apiRequestCache[cacheIndex].final = false === returnGetNextPageFunction
792
+
793
+ // only cache get method requests
794
+ apiRequestCache[cacheIndex].response = response
795
+
796
+ }
797
+
771
798
  apiResponse = TestRestfulResponse(response, request?.success, request?.error ?? "An unexpected API error occurred!")
772
799
 
773
- if (false !== apiResponse) {
800
+ if (false === apiResponse) {
801
+
802
+ if (request.debug && isLocal) {
803
+
804
+ toast.warning("DEVS: TestRestfulResponse returned false for (" + operatingTable + ").", toastOptionsDevs);
774
805
 
775
- responseCallback(response, request, apiResponse)
806
+ }
776
807
 
777
- if (C6.GET === requestMethod) {
808
+ return response;
778
809
 
779
- const responseData = response.data as iGetC6RestResponse<any>;
810
+ }
780
811
 
781
- // @ts-ignore
782
- returnGetNextPageFunction = 1 !== query?.[C6.PAGINATION]?.[C6.LIMIT] &&
783
- query?.[C6.PAGINATION]?.[C6.LIMIT] === responseData.rest.length
812
+ responseCallback(response, request, apiResponse)
784
813
 
785
- if (false === isTest || true === isVerbose) {
814
+ if (C6.GET === requestMethod) {
786
815
 
787
- console.groupCollapsed('%c API: Response returned length (' + responseData.rest?.length + ') of possible (' + query?.[C6.PAGINATION]?.[C6.LIMIT] + ') limit!', 'color: #0c0')
816
+ const responseData = response.data as iGetC6RestResponse<any>;
788
817
 
789
- console.log('%c ' + requestMethod + ' ' + tableName, 'color: #0c0')
818
+ returnGetNextPageFunction = 1 !== query?.[C6.PAGINATION]?.[C6.LIMIT] &&
819
+ query?.[C6.PAGINATION]?.[C6.LIMIT] === responseData.rest.length
790
820
 
791
- console.log('%c Request Data (note you may see the success and/or error prompt):', 'color: #0c0', request)
821
+ if (false === isTest || true === isVerbose) {
792
822
 
793
- console.log('%c Response Data:', 'color: #0c0', responseData.rest)
823
+ console.groupCollapsed('%c API: Response returned length (' + responseData.rest?.length + ') of possible (' + query?.[C6.PAGINATION]?.[C6.LIMIT] + ') limit!', 'color: #0c0')
794
824
 
795
- console.log('%c Will return get next page function:' + (1 !== query?.[C6.PAGINATION]?.[C6.LIMIT] ? '' : ' (Will not return with explicit limit 1 set)'), 'color: #0c0', true === returnGetNextPageFunction)
825
+ console.log('%c ' + requestMethod + ' ' + tableName, 'color: #0c0')
796
826
 
797
- console.trace();
827
+ console.log('%c Request Data (note you may see the success and/or error prompt):', 'color: #0c0', request)
798
828
 
799
- console.groupEnd()
829
+ console.log('%c Response Data:', 'color: #0c0', responseData.rest)
800
830
 
801
- }
831
+ console.log('%c Will return get next page function:' + (1 !== query?.[C6.PAGINATION]?.[C6.LIMIT] ? '' : ' (Will not return with explicit limit 1 set)'), 'color: #0c0', true === returnGetNextPageFunction)
802
832
 
803
- if (false === returnGetNextPageFunction
804
- && true === request.debug
805
- && isLocal) {
833
+ console.trace();
806
834
 
807
- toast.success("DEVS: Response returned length (" + responseData.rest?.length + ") less than limit (" + query?.[C6.PAGINATION]?.[C6.LIMIT] + ").", toastOptionsDevs);
835
+ console.groupEnd()
808
836
 
809
- }
837
+ }
838
+
839
+ if (false === returnGetNextPageFunction
840
+ && true === request.debug
841
+ && isLocal) {
842
+
843
+ toast.success("DEVS: Response returned length (" + responseData.rest?.length + ") less than limit (" + query?.[C6.PAGINATION]?.[C6.LIMIT] + ").", toastOptionsDevs);
810
844
 
811
845
  }
812
846
 
813
- }
847
+ if (eFetchDependencies.NONE !== request.fetchDependencies) {
814
848
 
815
- if (cachingConfirmed) {
849
+ const C6FullApi = C6RestApi()
816
850
 
817
- const cacheIndex = apiRequestCache.findIndex(cache => cache.requestArgumentsSerialized === querySerialized);
851
+ console.groupCollapsed('%c API: fetchDependencies segment', 'color: #0c0')
818
852
 
819
- apiRequestCache[cacheIndex].final = false === returnGetNextPageFunction
853
+ console.log('%c ' + requestMethod + ' ' + tableName, 'color: #0c0')
820
854
 
821
- // only cache get method requests
822
- apiRequestCache[cacheIndex].response = response
855
+ console.trace();
856
+
857
+ const fetchDependencies = async (fetchData: {
858
+ [key: string]: iConstraint[]
859
+ }) => Object.keys(
860
+ fetchData
861
+ ).map((column) => {
862
+
863
+ // check if the column is in the response
864
+ if (undefined === responseData.rest[column]) {
865
+
866
+ return false
867
+
868
+ }
869
+
870
+ return fetchData[column].map(async (constraint) => {
871
+
872
+ console.log(C6FullApi, column, constraint.TABLE)
873
+
874
+ const fetchTable = await C6.IMPORT(constraint.TABLE)
875
+
876
+ const RestApi = fetchTable.default
877
+
878
+ return RestApi.Get({
879
+ [C6.WHERE]: {
880
+ [constraint.COLUMN]: responseData.rest[column]
881
+ }
882
+ });
883
+
884
+ });
885
+
886
+ });
887
+
888
+ let dependencies: any[] = [];
889
+
890
+ // noinspection FallThroughInSwitchStatementJS
891
+ switch (request.fetchDependencies) {
892
+ case eFetchDependencies.ALL:
893
+ case eFetchDependencies.CHILDREN:
894
+ // @ts-ignore
895
+ case eFetchDependencies.REFERENCED:
896
+
897
+ const referencedBy = C6.TABLES[operatingTable].TABLE_REFERENCED_BY;
898
+
899
+ dependencies = (await fetchDependencies(referencedBy)).flat(Infinity)
900
+
901
+ if (request.fetchDependencies !== eFetchDependencies.ALL) {
902
+
903
+ break;
904
+
905
+ }
906
+
907
+ case eFetchDependencies.PARENTS:
908
+ case eFetchDependencies.REFERENCES:
909
+
910
+ const references = C6.TABLES[operatingTable].TABLE_REFERENCES;
911
+
912
+ dependencies = [...dependencies, (await fetchDependencies(references)).flat(Infinity)]
913
+
914
+ break;
915
+ default:
916
+ throw new Error('The value of fetchDependencies (' + request.fetchDependencies?.toString() + ') was not recognized.')
917
+ }
918
+
919
+ request.fetchDependencies = dependencies;
920
+
921
+ await Promise.all(dependencies)
922
+
923
+ }
924
+
925
+ console.groupEnd()
823
926
 
824
927
  }
825
928
 
@@ -1,22 +0,0 @@
1
- import {
2
- tWsLiveUpdate
3
- } from "@carbonorm/carbonnode";
4
-
5
- {{#TABLES}}
6
- import { putState{{TABLE_NAME_SHORT_PASCAL_CASE}}, postState{{TABLE_NAME_SHORT_PASCAL_CASE}}, deleteState{{TABLE_NAME_SHORT_PASCAL_CASE}} } from "./{{TABLE_NAME_SHORT_PASCAL_CASE}}";
7
- {{/TABLES}}
8
-
9
- const wsLiveUpdates: tWsLiveUpdate = {
10
- {{#TABLES}}
11
- {{TABLE_NAME_SHORT}}: {
12
- PUT: putState{{TABLE_NAME_SHORT_PASCAL_CASE}},
13
- POST: postState{{TABLE_NAME_SHORT_PASCAL_CASE}},
14
- DELETE: deleteState{{TABLE_NAME_SHORT_PASCAL_CASE}},
15
- },
16
- {{/TABLES}}
17
- };
18
-
19
-
20
- export default wsLiveUpdates;
21
-
22
-