@carbonorm/carbonnode 1.6.33 → 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.
- package/dist/index.cjs.js +41 -19
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +41 -19
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/api/restRequest.ts +185 -145
package/src/api/restRequest.ts
CHANGED
|
@@ -781,246 +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
|
-
|
|
784
|
+
if (typeof response.data === 'string') {
|
|
785
785
|
|
|
786
|
-
|
|
786
|
+
if (isTest) {
|
|
787
787
|
|
|
788
|
-
|
|
788
|
+
console.trace()
|
|
789
789
|
|
|
790
|
-
|
|
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
791
|
|
|
792
|
-
|
|
792
|
+
}
|
|
793
793
|
|
|
794
|
-
|
|
794
|
+
return Promise.reject(response);
|
|
795
795
|
|
|
796
|
-
|
|
796
|
+
}
|
|
797
797
|
|
|
798
|
-
|
|
798
|
+
if (cachingConfirmed) {
|
|
799
799
|
|
|
800
|
-
|
|
800
|
+
const cacheIndex = apiRequestCache.findIndex(cache => cache.requestArgumentsSerialized === querySerialized);
|
|
801
801
|
|
|
802
|
-
|
|
802
|
+
apiRequestCache[cacheIndex].final = false === returnGetNextPageFunction
|
|
803
803
|
|
|
804
|
-
|
|
805
|
-
|
|
804
|
+
// only cache get method requests
|
|
805
|
+
apiRequestCache[cacheIndex].response = response
|
|
806
806
|
|
|
807
|
-
|
|
807
|
+
}
|
|
808
808
|
|
|
809
|
-
|
|
809
|
+
apiResponse = TestRestfulResponse(response, request?.success, request?.error ?? "An unexpected API error occurred!")
|
|
810
810
|
|
|
811
|
-
|
|
811
|
+
if (false === apiResponse) {
|
|
812
812
|
|
|
813
|
-
|
|
813
|
+
if (request.debug && isLocal) {
|
|
814
814
|
|
|
815
|
-
|
|
815
|
+
toast.warning("DEVS: TestRestfulResponse returned false for (" + operatingTable + ").", toastOptionsDevs);
|
|
816
816
|
|
|
817
|
-
|
|
817
|
+
}
|
|
818
818
|
|
|
819
|
-
|
|
819
|
+
return response;
|
|
820
820
|
|
|
821
|
-
|
|
821
|
+
}
|
|
822
822
|
|
|
823
|
-
|
|
824
|
-
|
|
823
|
+
// stateful operations are done in the response callback - its leverages rest generated functions
|
|
824
|
+
responseCallback(response, request, apiResponse)
|
|
825
825
|
|
|
826
|
-
|
|
826
|
+
if (C6.GET === requestMethod) {
|
|
827
827
|
|
|
828
|
-
|
|
828
|
+
const responseData = response.data as iGetC6RestResponse<any>;
|
|
829
829
|
|
|
830
|
-
|
|
831
|
-
|
|
830
|
+
returnGetNextPageFunction = 1 !== query?.[C6.PAGINATION]?.[C6.LIMIT] &&
|
|
831
|
+
query?.[C6.PAGINATION]?.[C6.LIMIT] === responseData.rest.length
|
|
832
832
|
|
|
833
|
-
|
|
833
|
+
if (false === isTest || true === isVerbose) {
|
|
834
834
|
|
|
835
|
-
|
|
835
|
+
console.groupCollapsed('%c API: Response (' + requestMethod + ' ' + tableName + ') returned length (' + responseData.rest?.length + ') of possible (' + query?.[C6.PAGINATION]?.[C6.LIMIT] + ') limit!', 'color: #0c0')
|
|
836
836
|
|
|
837
|
-
|
|
837
|
+
console.log('%c ' + requestMethod + ' ' + tableName, 'color: #0c0')
|
|
838
838
|
|
|
839
|
-
|
|
839
|
+
console.log('%c Request Data (note you may see the success and/or error prompt):', 'color: #0c0', request)
|
|
840
840
|
|
|
841
|
-
|
|
841
|
+
console.log('%c Response Data:', 'color: #0c0', responseData.rest)
|
|
842
842
|
|
|
843
|
-
|
|
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)
|
|
844
844
|
|
|
845
|
-
|
|
845
|
+
console.trace();
|
|
846
846
|
|
|
847
|
-
|
|
847
|
+
console.groupEnd()
|
|
848
848
|
|
|
849
|
-
|
|
849
|
+
}
|
|
850
850
|
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
851
|
+
if (false === returnGetNextPageFunction
|
|
852
|
+
&& true === request.debug
|
|
853
|
+
&& isLocal) {
|
|
854
854
|
|
|
855
|
-
|
|
855
|
+
toast.success("DEVS: Response returned length (" + responseData.rest?.length + ") less than limit (" + query?.[C6.PAGINATION]?.[C6.LIMIT] + ").", toastOptionsDevs);
|
|
856
856
|
|
|
857
|
-
|
|
857
|
+
}
|
|
858
858
|
|
|
859
|
-
|
|
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
|
+
}
|
|
860
904
|
|
|
861
|
-
|
|
862
|
-
&& 'number' === typeof request.fetchDependencies
|
|
863
|
-
&& responseData.rest.length > 0) {
|
|
905
|
+
} else {
|
|
864
906
|
|
|
865
|
-
|
|
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
|
+
};
|
|
866
916
|
|
|
867
|
-
|
|
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')
|
|
917
|
+
}
|
|
872
918
|
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
919
|
+
let fetchReferences: {
|
|
920
|
+
[externalTable: string]: {
|
|
921
|
+
[column: string]: string[]
|
|
922
|
+
}
|
|
923
|
+
} = {}
|
|
876
924
|
|
|
877
|
-
|
|
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
|
-
};
|
|
925
|
+
let apiRequestPromises: Array<Awaited<apiReturn<iGetC6RestResponse<any>>>> = []
|
|
882
926
|
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
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
|
+
}));
|
|
888
937
|
|
|
889
|
-
|
|
938
|
+
console.log('fetchReferences', fetchReferences)
|
|
890
939
|
|
|
891
|
-
|
|
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
|
-
}));
|
|
940
|
+
for (const tableToFetch in fetchReferences) {
|
|
901
941
|
|
|
902
|
-
|
|
942
|
+
if (fetchDependencies & eFetchDependencies.C6ENTITY
|
|
943
|
+
&& 'string' === typeof tableName
|
|
944
|
+
&& tableName.endsWith("carbon_carbons")) {
|
|
903
945
|
|
|
904
|
-
|
|
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
|
|
905
949
|
|
|
906
|
-
|
|
907
|
-
|
|
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());
|
|
908
956
|
|
|
909
|
-
|
|
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
|
|
957
|
+
const shouldContinue = referencesTables.find((referencesTable) => tableToFetch.endsWith(referencesTable))
|
|
912
958
|
|
|
913
|
-
|
|
914
|
-
if ('entity_tag' in row && !accumulator.includes(row['entity_tag'])) {
|
|
915
|
-
accumulator.push(row['entity_tag']);
|
|
916
|
-
}
|
|
917
|
-
return accumulator;
|
|
918
|
-
}, []).map((entityTag) => entityTag.split('\\').pop().toLowerCase());
|
|
959
|
+
if (!shouldContinue) {
|
|
919
960
|
|
|
920
|
-
|
|
961
|
+
console.log('%c C6ENTITY: The constraintTableName (' + tableToFetch + ') did not end with any value in referencesTables', 'color: #c00', referencesTables)
|
|
921
962
|
|
|
922
|
-
|
|
963
|
+
continue;
|
|
923
964
|
|
|
924
|
-
|
|
965
|
+
}
|
|
925
966
|
|
|
926
|
-
|
|
967
|
+
console.log('%c C6ENTITY: The constraintTableName (' + tableToFetch + ') will be fetched.', 'color: #0c0')
|
|
927
968
|
|
|
928
969
|
}
|
|
929
970
|
|
|
930
|
-
|
|
971
|
+
const fetchTable = await C6.IMPORT(tableToFetch)
|
|
931
972
|
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
const fetchTable = await C6.IMPORT(table)
|
|
973
|
+
const RestApi = fetchTable.default
|
|
935
974
|
|
|
936
|
-
|
|
975
|
+
console.log('%c Fetch Dependencies will select (' + tableToFetch + ') using GET request', 'color: #33ccff')
|
|
937
976
|
|
|
938
|
-
|
|
977
|
+
let nextFetchDependencies = eFetchDependencies.NONE
|
|
939
978
|
|
|
940
|
-
|
|
979
|
+
if (fetchDependencies & eFetchDependencies.RECURSIVE) {
|
|
941
980
|
|
|
942
|
-
|
|
981
|
+
if (fetchDependencies & eFetchDependencies.ALL) {
|
|
943
982
|
|
|
944
|
-
|
|
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.')
|
|
945
984
|
|
|
946
|
-
|
|
985
|
+
}
|
|
947
986
|
|
|
948
|
-
|
|
987
|
+
nextFetchDependencies = fetchDependencies
|
|
949
988
|
|
|
950
|
-
|
|
989
|
+
} else if (fetchDependencies & eFetchDependencies.C6ENTITY) {
|
|
951
990
|
|
|
952
|
-
|
|
991
|
+
if (tableToFetch === "carbon_carbons") {
|
|
953
992
|
|
|
954
|
-
|
|
993
|
+
nextFetchDependencies = fetchDependencies
|
|
955
994
|
|
|
956
|
-
|
|
995
|
+
} else {
|
|
957
996
|
|
|
958
|
-
|
|
997
|
+
nextFetchDependencies = fetchDependencies ^ eFetchDependencies.C6ENTITY
|
|
959
998
|
|
|
960
|
-
|
|
999
|
+
}
|
|
961
1000
|
|
|
962
1001
|
}
|
|
963
1002
|
|
|
964
|
-
|
|
1003
|
+
console.log('fetchReferences', fetchReferences[tableToFetch], "Current fetchDependencies for (" + operatingTable + "):", fetchDependencies, "New fetchDependencies for (" + tableToFetch + "): ", nextFetchDependencies)
|
|
965
1004
|
|
|
966
|
-
|
|
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
|
|
967
1007
|
|
|
968
|
-
|
|
969
|
-
|
|
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) => {
|
|
970
1013
|
|
|
971
|
-
|
|
972
|
-
// todo - using value to avoid joins.... but. maybe this should be a parameterizable option -- think race conditions; its safer to join
|
|
973
|
-
apiRequestPromises.push(await RestApi.Get({
|
|
974
|
-
[C6.WHERE]: {
|
|
975
|
-
0: Object.keys(fetchReferences[table]).reduce((sum, column) => {
|
|
1014
|
+
fetchReferences[tableToFetch][column] = fetchReferences[tableToFetch][column].flat(Infinity)
|
|
976
1015
|
|
|
977
|
-
|
|
1016
|
+
if (0 === fetchReferences[tableToFetch][column].length) {
|
|
978
1017
|
|
|
979
|
-
|
|
1018
|
+
console.warn('The column (' + column + ') was not found in the response data. We will not fetch.', responseData)
|
|
980
1019
|
|
|
981
|
-
|
|
1020
|
+
return false;
|
|
982
1021
|
|
|
983
|
-
|
|
1022
|
+
}
|
|
984
1023
|
|
|
985
|
-
|
|
1024
|
+
sum[column] = fetchReferences[tableToFetch][column].length === 1
|
|
1025
|
+
? fetchReferences[tableToFetch][column][0]
|
|
1026
|
+
: [
|
|
1027
|
+
C6.IN, fetchReferences[tableToFetch][column]
|
|
1028
|
+
]
|
|
986
1029
|
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
1030
|
+
return sum
|
|
1031
|
+
}, {})
|
|
1032
|
+
},
|
|
1033
|
+
fetchDependencies: nextFetchDependencies
|
|
1034
|
+
}
|
|
1035
|
+
));
|
|
992
1036
|
|
|
993
|
-
|
|
994
|
-
}, {})
|
|
995
|
-
},
|
|
996
|
-
fetchDependencies: nextFetchDependencies
|
|
997
|
-
}
|
|
998
|
-
));
|
|
1037
|
+
}
|
|
999
1038
|
|
|
1000
|
-
|
|
1039
|
+
await Promise.all(apiRequestPromises)
|
|
1040
|
+
|
|
1041
|
+
request.fetchDependencies = apiRequestPromises
|
|
1001
1042
|
|
|
1002
|
-
|
|
1043
|
+
console.groupEnd()
|
|
1003
1044
|
|
|
1004
|
-
|
|
1045
|
+
}
|
|
1005
1046
|
|
|
1006
|
-
console.groupEnd()
|
|
1007
1047
|
|
|
1008
1048
|
}
|
|
1009
1049
|
|
|
1050
|
+
if (request.debug && isLocal) {
|
|
1010
1051
|
|
|
1011
|
-
|
|
1052
|
+
toast.success("DEVS: (" + requestMethod + ") request complete.", toastOptionsDevs);
|
|
1012
1053
|
|
|
1013
|
-
|
|
1054
|
+
}
|
|
1014
1055
|
|
|
1015
|
-
|
|
1056
|
+
return response;
|
|
1016
1057
|
|
|
1017
1058
|
}
|
|
1059
|
+
)
|
|
1060
|
+
;
|
|
1018
1061
|
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
});
|
|
1022
|
-
|
|
1023
|
-
} catch (error) {
|
|
1062
|
+
} catch
|
|
1063
|
+
(error) {
|
|
1024
1064
|
|
|
1025
1065
|
if (isTest) {
|
|
1026
1066
|
|