@carbonorm/carbonnode 1.6.32 → 1.6.34

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.
@@ -781,236 +781,286 @@ export default function restApi<
781
781
  // https://rapidapi.com/guides/axios-async-await
782
782
  return axiosActiveRequest.then(async (response): Promise<AxiosResponse<ResponseDataType, any>> => {
783
783
 
784
- if (typeof response.data === 'string') {
784
+ if (typeof response.data === 'string') {
785
785
 
786
- if (isTest) {
786
+ if (isTest) {
787
787
 
788
- console.trace()
788
+ console.trace()
789
789
 
790
- throw new Error('The response data was a string this typically indicated html was sent. Make sure all cookies (' + JSON.stringify(response.config.headers) + ') needed are present! (' + response.data + ')')
790
+ throw new Error('The response data was a string this typically indicated html was sent. Make sure all cookies (' + JSON.stringify(response.config.headers) + ') needed are present! (' + response.data + ')')
791
+
792
+ }
793
+
794
+ return Promise.reject(response);
791
795
 
792
796
  }
793
797
 
794
- return Promise.reject(response);
798
+ if (cachingConfirmed) {
795
799
 
796
- }
800
+ const cacheIndex = apiRequestCache.findIndex(cache => cache.requestArgumentsSerialized === querySerialized);
797
801
 
798
- if (cachingConfirmed) {
802
+ apiRequestCache[cacheIndex].final = false === returnGetNextPageFunction
799
803
 
800
- const cacheIndex = apiRequestCache.findIndex(cache => cache.requestArgumentsSerialized === querySerialized);
804
+ // only cache get method requests
805
+ apiRequestCache[cacheIndex].response = response
801
806
 
802
- apiRequestCache[cacheIndex].final = false === returnGetNextPageFunction
807
+ }
803
808
 
804
- // only cache get method requests
805
- apiRequestCache[cacheIndex].response = response
809
+ apiResponse = TestRestfulResponse(response, request?.success, request?.error ?? "An unexpected API error occurred!")
806
810
 
807
- }
811
+ if (false === apiResponse) {
808
812
 
809
- apiResponse = TestRestfulResponse(response, request?.success, request?.error ?? "An unexpected API error occurred!")
813
+ if (request.debug && isLocal) {
810
814
 
811
- if (false === apiResponse) {
815
+ toast.warning("DEVS: TestRestfulResponse returned false for (" + operatingTable + ").", toastOptionsDevs);
812
816
 
813
- if (request.debug && isLocal) {
817
+ }
814
818
 
815
- toast.warning("DEVS: TestRestfulResponse returned false for (" + operatingTable + ").", toastOptionsDevs);
819
+ return response;
816
820
 
817
821
  }
818
822
 
819
- return response;
823
+ // stateful operations are done in the response callback - its leverages rest generated functions
824
+ responseCallback(response, request, apiResponse)
820
825
 
821
- }
826
+ if (C6.GET === requestMethod) {
822
827
 
823
- // stateful operations are done in the response callback - its leverages rest generated functions
824
- responseCallback(response, request, apiResponse)
828
+ const responseData = response.data as iGetC6RestResponse<any>;
825
829
 
826
- if (C6.GET === requestMethod) {
830
+ returnGetNextPageFunction = 1 !== query?.[C6.PAGINATION]?.[C6.LIMIT] &&
831
+ query?.[C6.PAGINATION]?.[C6.LIMIT] === responseData.rest.length
827
832
 
828
- const responseData = response.data as iGetC6RestResponse<any>;
833
+ if (false === isTest || true === isVerbose) {
829
834
 
830
- returnGetNextPageFunction = 1 !== query?.[C6.PAGINATION]?.[C6.LIMIT] &&
831
- query?.[C6.PAGINATION]?.[C6.LIMIT] === responseData.rest.length
835
+ console.groupCollapsed('%c API: Response (' + requestMethod + ' ' + tableName + ') returned length (' + responseData.rest?.length + ') of possible (' + query?.[C6.PAGINATION]?.[C6.LIMIT] + ') limit!', 'color: #0c0')
832
836
 
833
- if (false === isTest || true === isVerbose) {
837
+ console.log('%c ' + requestMethod + ' ' + tableName, 'color: #0c0')
834
838
 
835
- console.groupCollapsed('%c API: Response (' + requestMethod + ' ' + tableName + ') returned length (' + responseData.rest?.length + ') of possible (' + query?.[C6.PAGINATION]?.[C6.LIMIT] + ') limit!', 'color: #0c0')
839
+ console.log('%c Request Data (note you may see the success and/or error prompt):', 'color: #0c0', request)
836
840
 
837
- console.log('%c ' + requestMethod + ' ' + tableName, 'color: #0c0')
841
+ console.log('%c Response Data:', 'color: #0c0', responseData.rest)
838
842
 
839
- console.log('%c Request Data (note you may see the success and/or error prompt):', 'color: #0c0', request)
843
+ 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)
840
844
 
841
- console.log('%c Response Data:', 'color: #0c0', responseData.rest)
845
+ console.trace();
842
846
 
843
- 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)
847
+ console.groupEnd()
844
848
 
845
- console.trace();
849
+ }
846
850
 
847
- console.groupEnd()
851
+ if (false === returnGetNextPageFunction
852
+ && true === request.debug
853
+ && isLocal) {
848
854
 
849
- }
855
+ toast.success("DEVS: Response returned length (" + responseData.rest?.length + ") less than limit (" + query?.[C6.PAGINATION]?.[C6.LIMIT] + ").", toastOptionsDevs);
850
856
 
851
- if (false === returnGetNextPageFunction
852
- && true === request.debug
853
- && isLocal) {
857
+ }
854
858
 
855
- toast.success("DEVS: Response returned length (" + responseData.rest?.length + ") less than limit (" + query?.[C6.PAGINATION]?.[C6.LIMIT] + ").", toastOptionsDevs);
859
+ request.fetchDependencies ??= eFetchDependencies.NONE;
860
+
861
+ if (request.fetchDependencies
862
+ && 'number' === typeof request.fetchDependencies
863
+ && responseData.rest.length > 0) {
864
+
865
+ const fetchDependencies = request.fetchDependencies as number;
866
+
867
+ console.groupCollapsed('%c API: Fetch Dependencies segment (' + requestMethod + ' ' + tableName + ')'
868
+ + (fetchDependencies & eFetchDependencies.CHILDREN ? ' | (CHILDREN|REFERENCED) ' : '')
869
+ + (fetchDependencies & eFetchDependencies.PARENTS ? ' | (PARENTS|REFRENCED_BY)' : '')
870
+ + (fetchDependencies & eFetchDependencies.C6ENTITY ? ' | (C6ENTITY)' : '')
871
+ + (fetchDependencies & eFetchDependencies.RECURSIVE ? ' | (RECURSIVE)' : ''), 'color: #33ccff')
872
+
873
+ console.groupCollapsed('Collapsed JS Trace');
874
+ console.trace(); // hidden in collapsed group
875
+ console.groupEnd();
876
+
877
+ // noinspection JSBitwiseOperatorUsage
878
+ let dependencies: {
879
+ [key: string]: iConstraint[]
880
+ } = {};
881
+
882
+ if (fetchDependencies & eFetchDependencies.C6ENTITY) {
883
+
884
+ // if we get PARENTS in a C6 context we need to remove all entities that are not the operatingTables primary key
885
+
886
+ dependencies = operatingTable.endsWith("carbon_carbons")
887
+ ? {
888
+ // the context of the entity system is a bit different
889
+ ...fetchDependencies & eFetchDependencies.CHILDREN // REFERENCED === CHILDREN
890
+ ? C6.TABLES[operatingTable].TABLE_REFERENCED_BY
891
+ : {},
892
+ ...fetchDependencies & eFetchDependencies.PARENTS // REFERENCES === PARENTS
893
+ ? C6.TABLES[operatingTable].TABLE_REFERENCES
894
+ : {}
895
+ } : {
896
+ // the context of the entity system is a bit different
897
+ ...fetchDependencies & eFetchDependencies.CHILDREN // REFERENCED === CHILDREN
898
+ ? C6.TABLES[operatingTable].TABLE_REFERENCES
899
+ : {},
900
+ ...fetchDependencies & eFetchDependencies.PARENTS // REFERENCES === PARENTS
901
+ ? C6.TABLES[operatingTable].TABLE_REFERENCED_BY
902
+ : {}
903
+ }
856
904
 
857
- }
905
+ } else {
906
+
907
+ // this is the natural mysql context
908
+ dependencies = {
909
+ ...fetchDependencies & eFetchDependencies.REFERENCED // REFERENCED === CHILDREN
910
+ ? C6.TABLES[operatingTable].TABLE_REFERENCED_BY
911
+ : {},
912
+ ...fetchDependencies & eFetchDependencies.REFERENCES // REFERENCES === PARENTS
913
+ ? C6.TABLES[operatingTable].TABLE_REFERENCES
914
+ : {}
915
+ };
858
916
 
859
- request.fetchDependencies ??= eFetchDependencies.NONE;
917
+ }
860
918
 
861
- if (request.fetchDependencies
862
- && 'number' === typeof request.fetchDependencies
863
- && responseData.rest.length > 0) {
919
+ let fetchReferences: {
920
+ [externalTable: string]: {
921
+ [column: string]: string[]
922
+ }
923
+ } = {}
864
924
 
865
- const fetchDependencies = request.fetchDependencies as number;
925
+ let apiRequestPromises: Array<Awaited<apiReturn<iGetC6RestResponse<any>>>> = []
866
926
 
867
- console.groupCollapsed('%c API: Fetch Dependencies segment (' + requestMethod + ' ' + tableName + ')'
868
- + (fetchDependencies & eFetchDependencies.CHILDREN ? ' | (CHILDREN|REFERENCED) ' : '')
869
- + (fetchDependencies & eFetchDependencies.PARENTS ? ' | (PARENTS|REFRENCED_BY)' : '')
870
- + (fetchDependencies & eFetchDependencies.C6ENTITY ? ' | (C6ENTITY)' : '')
871
- + (fetchDependencies & eFetchDependencies.RECURSIVE ? ' | (RECURSIVE)' : ''), 'color: #33ccff')
927
+ console.log('dependencies', dependencies)
928
+ Object.keys(
929
+ dependencies
930
+ ).forEach(column => dependencies[column].forEach((constraint) => {
931
+ fetchReferences[constraint.TABLE] ??= {};
932
+ fetchReferences[constraint.TABLE][constraint.COLUMN] ??= []
933
+ // carbon_users -> user_id | user_photo -> carbon_carbons -> entity_id
934
+ fetchReferences[constraint.TABLE][constraint.COLUMN].push(responseData.rest[column] ?? responseData.rest.map((row) => row[column]).filter(n => n)) // todo - we are mapping all carbons received to every fk reference despite its tag name
935
+ console.log('fetchReferences[constraint.TABLE][constraint.COLUMN]', constraint.TABLE, constraint.COLUMN, fetchReferences[constraint.TABLE][constraint.COLUMN], responseData.rest)
936
+ }));
872
937
 
873
- console.groupCollapsed('Collapsed JS Trace');
874
- console.trace(); // hidden in collapsed group
875
- console.groupEnd();
938
+ console.log('fetchReferences', fetchReferences)
876
939
 
877
- // noinspection JSBitwiseOperatorUsage
878
- let dependencies: { [key: string]: iConstraint[] } = {
879
- ...fetchDependencies & eFetchDependencies.REFERENCED ? C6.TABLES[operatingTable].TABLE_REFERENCED_BY : {},
880
- ...fetchDependencies & eFetchDependencies.REFERENCES ? C6.TABLES[operatingTable].TABLE_REFERENCES : {}
881
- };
940
+ for (const tableToFetch in fetchReferences) {
882
941
 
883
- let fetchReferences: {
884
- [externalTable: string]: {
885
- [column: string]: string[]
886
- }
887
- } = {}
942
+ if (fetchDependencies & eFetchDependencies.C6ENTITY
943
+ && 'string' === typeof tableName
944
+ && tableName.endsWith("carbon_carbons")) {
888
945
 
889
- let apiRequestPromises: Array<Awaited<apiReturn<iGetC6RestResponse<any>>>> = []
946
+ // this more or less implies
947
+ // todo - rethink the table ref entity system - when tables are renamed? no hooks exist in mysql
948
+ // since were already filtering on column, we can assume the first row constraint is the same as the rest
890
949
 
891
- console.log('dependencies', dependencies)
892
- Object.keys(
893
- dependencies
894
- ).forEach(column => dependencies[column].forEach((constraint) => {
895
- fetchReferences[constraint.TABLE] ??= {};
896
- fetchReferences[constraint.TABLE][constraint.COLUMN] ??= []
897
- // carbon_users -> user_id | user_photo -> carbon_carbons -> entity_id
898
- fetchReferences[constraint.TABLE][constraint.COLUMN].push(responseData.rest[column] ?? responseData.rest.map((row) => row[column]).filter(n => n)) // todo - we are mapping all carbons received to every fk reference despite its tag name
899
- console.log('fetchReferences[constraint.TABLE][constraint.COLUMN]', constraint.TABLE, constraint.COLUMN, fetchReferences[constraint.TABLE][constraint.COLUMN], responseData.rest)
900
- }));
950
+ const referencesTables: string[] = responseData.rest.reduce((accumulator: string[], row) => {
951
+ if ('entity_tag' in row && !accumulator.includes(row['entity_tag'])) {
952
+ accumulator.push(row['entity_tag']);
953
+ }
954
+ return accumulator;
955
+ }, []).map((entityTag) => entityTag.split('\\').pop().toLowerCase());
901
956
 
902
- console.log('fetchReferences', fetchReferences)
957
+ const shouldContinue = referencesTables.find((referencesTable) => tableToFetch.endsWith(referencesTable))
903
958
 
904
- for (const table in fetchReferences) {
959
+ if (!shouldContinue) {
905
960
 
906
- if (fetchDependencies & eFetchDependencies.C6ENTITY
907
- && tableName === "carbon_carbons") {
961
+ console.log('%c C6ENTITY: The constraintTableName (' + tableToFetch + ') did not end with any value in referencesTables', 'color: #c00', referencesTables)
908
962
 
909
- // this more or less implies
910
- // todo - rethink the table ref entity system - when tables are renamed? no hooks exist in mysql
911
- // since were already filtering on column, we can assume the first row constraint is the same as the rest
963
+ continue;
912
964
 
913
- const referencesTables: string[] = responseData.rest.reduce((accumulator: string[], row) => {
914
- if ('entity_tag' in row && !accumulator.includes(row['entity_tag'])) {
915
- accumulator.push(row['entity_tag']);
916
965
  }
917
- return accumulator;
918
- }, []).map((entityTag) => entityTag.split('\\').pop().toLowerCase());
919
966
 
920
- const shouldContinue = referencesTables.find((referencesTable) => table.endsWith(referencesTable))
967
+ console.log('%c C6ENTITY: The constraintTableName (' + tableToFetch + ') will be fetched.', 'color: #0c0')
921
968
 
922
- if (!shouldContinue) {
969
+ }
923
970
 
924
- console.log('%c C6ENTITY: The constraintTableName (' + table + ') did not end with any value in referencesTables', 'color: #c00', referencesTables)
971
+ const fetchTable = await C6.IMPORT(tableToFetch)
925
972
 
926
- continue;
973
+ const RestApi = fetchTable.default
927
974
 
928
- }
975
+ console.log('%c Fetch Dependencies will select (' + tableToFetch + ') using GET request', 'color: #33ccff')
929
976
 
930
- console.log('%c C6ENTITY: The constraintTableName (' + table + ') will be fetched.', 'color: #0c0')
977
+ let nextFetchDependencies = eFetchDependencies.NONE
931
978
 
932
- }
979
+ if (fetchDependencies & eFetchDependencies.RECURSIVE) {
980
+
981
+ if (fetchDependencies & eFetchDependencies.ALL) {
982
+
983
+ throw Error('Recursive fetch dependencies with both PARENT and CHILD reference will result in an infin1ite loop. As there is not real ending condition, this is not supported.')
984
+
985
+ }
933
986
 
934
- const fetchTable = await C6.IMPORT(table)
987
+ nextFetchDependencies = fetchDependencies
935
988
 
936
- const RestApi = fetchTable.default
989
+ } else if (fetchDependencies & eFetchDependencies.C6ENTITY) {
937
990
 
938
- console.log('%c Fetch Dependencies will select (' + table + ') using GET request', 'color: #33ccff')
991
+ if (tableToFetch === "carbon_carbons") {
939
992
 
940
- let nextFetchDependencies = eFetchDependencies.NONE
993
+ nextFetchDependencies = fetchDependencies
941
994
 
942
- if (fetchDependencies & eFetchDependencies.RECURSIVE) { // todo - recusive has no real ending condition, this is a bad idea
995
+ } else {
943
996
 
944
- if (fetchDependencies & eFetchDependencies.ALL) {
997
+ nextFetchDependencies = fetchDependencies ^ eFetchDependencies.C6ENTITY
945
998
 
946
- throw Error('Recursive fetch dependencies with both PARENT and CHILD reference will result in an infin1ite loop. As there is not real ending condition, this is not supported.')
999
+ }
947
1000
 
948
1001
  }
949
1002
 
950
- nextFetchDependencies = fetchDependencies
951
- } else if (fetchDependencies & eFetchDependencies.C6ENTITY
952
- && operatingTable === "carbon_carbons") {
953
- nextFetchDependencies = fetchDependencies ^ eFetchDependencies.C6ENTITY
954
- }
1003
+ console.log('fetchReferences', fetchReferences[tableToFetch], "Current fetchDependencies for (" + operatingTable + "):", fetchDependencies, "New fetchDependencies for (" + tableToFetch + "): ", nextFetchDependencies)
955
1004
 
956
- console.log('fetchReferences', fetchReferences[table], "Current fetchDependencies for (" + operatingTable + "):", fetchDependencies, "New fetchDependencies for (" + table + "): ", nextFetchDependencies)
1005
+ // todo - filter out ids that exist in state?!? note - remember that this does not necessarily mean the pk, but only known is its an FK to somewhere
1006
+ // it not certain that they are using carbons' entities either
957
1007
 
958
- // todo - filter out ids that exist in state?!? note - remember that this does not necessarily mean the pk, but only known is its an FK to somewhere
959
- // it not certain that they are using carbons' entities either
1008
+ // this is a dynamic call to the rest api, any generated table may resolve with (RestApi)
1009
+ // todo - using value to avoid joins.... but. maybe this should be a parameterizable option -- think race conditions; its safer to join
1010
+ apiRequestPromises.push(await RestApi.Get({
1011
+ [C6.WHERE]: {
1012
+ 0: Object.keys(fetchReferences[tableToFetch]).reduce((sum, column) => {
960
1013
 
961
- // this is a dynamic call to the rest api, any generated table may resolve with (RestApi)
962
- // todo - using value to avoid joins.... but. maybe this should be a parameterizable option -- think race conditions; its safer to join
963
- apiRequestPromises.push(await RestApi.Get({
964
- [C6.WHERE]: {
965
- 0: Object.keys(fetchReferences[table]).reduce((sum, column) => {
1014
+ fetchReferences[tableToFetch][column] = fetchReferences[tableToFetch][column].flat(Infinity)
966
1015
 
967
- fetchReferences[table][column] = fetchReferences[table][column].flat(Infinity)
1016
+ if (0 === fetchReferences[tableToFetch][column].length) {
968
1017
 
969
- if (0 === fetchReferences[table][column].length) {
1018
+ console.warn('The column (' + column + ') was not found in the response data. We will not fetch.', responseData)
970
1019
 
971
- console.warn('The column (' + column + ') was not found in the response data. We will not fetch.', responseData)
1020
+ return false;
972
1021
 
973
- return false;
1022
+ }
974
1023
 
975
- }
1024
+ sum[column] = fetchReferences[tableToFetch][column].length === 1
1025
+ ? fetchReferences[tableToFetch][column][0]
1026
+ : [
1027
+ C6.IN, fetchReferences[tableToFetch][column]
1028
+ ]
976
1029
 
977
- sum[column] = fetchReferences[table][column].length === 1
978
- ? fetchReferences[table][column][0]
979
- : [
980
- C6.IN, fetchReferences[table][column]
981
- ]
1030
+ return sum
1031
+ }, {})
1032
+ },
1033
+ fetchDependencies: nextFetchDependencies
1034
+ }
1035
+ ));
982
1036
 
983
- return sum
984
- }, {})
985
- },
986
- fetchDependencies: nextFetchDependencies
987
- }
988
- ));
1037
+ }
989
1038
 
990
- }
1039
+ await Promise.all(apiRequestPromises)
1040
+
1041
+ request.fetchDependencies = apiRequestPromises
991
1042
 
992
- await Promise.all(apiRequestPromises)
1043
+ console.groupEnd()
993
1044
 
994
- request.fetchDependencies = apiRequestPromises
1045
+ }
995
1046
 
996
- console.groupEnd()
997
1047
 
998
1048
  }
999
1049
 
1050
+ if (request.debug && isLocal) {
1000
1051
 
1001
- }
1052
+ toast.success("DEVS: (" + requestMethod + ") request complete.", toastOptionsDevs);
1002
1053
 
1003
- if (request.debug && isLocal) {
1054
+ }
1004
1055
 
1005
- toast.success("DEVS: (" + requestMethod + ") request complete.", toastOptionsDevs);
1056
+ return response;
1006
1057
 
1007
1058
  }
1059
+ )
1060
+ ;
1008
1061
 
1009
- return response;
1010
-
1011
- });
1012
-
1013
- } catch (error) {
1062
+ } catch
1063
+ (error) {
1014
1064
 
1015
1065
  if (isTest) {
1016
1066