@carbonorm/carbonnode 1.6.11 → 1.6.12
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/api/restRequest.d.ts +2 -2
- package/dist/index.cjs.js +90 -89
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +90 -89
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/api/restRequest.ts +108 -117
package/src/api/restRequest.ts
CHANGED
|
@@ -121,7 +121,7 @@ export type iAPI<RestTableInterfaces extends { [key: string]: any }> = RestTable
|
|
|
121
121
|
dataInsertMultipleRows?: RestTableInterfaces[],
|
|
122
122
|
cacheResults?: boolean, // aka ignoreCache
|
|
123
123
|
// todo - this should really only be used for get requests - add this to the Get interface or throw error (im actually inclined to ts ignore the function and add to iGetC6 atm; back later)
|
|
124
|
-
fetchDependencies?: number | eFetchDependencies |
|
|
124
|
+
fetchDependencies?: number | eFetchDependencies | apiReturn<iGetC6RestResponse<any>>[],
|
|
125
125
|
debug?: boolean,
|
|
126
126
|
success?: string | ((r: AxiosResponse) => (string | void)),
|
|
127
127
|
error?: string | ((r: AxiosResponse) => (string | void)),
|
|
@@ -779,110 +779,114 @@ export default function restApi<
|
|
|
779
779
|
|
|
780
780
|
// returning the promise with this then is important for tests. todo - we could make that optional.
|
|
781
781
|
// https://rapidapi.com/guides/axios-async-await
|
|
782
|
-
return axiosActiveRequest.then(async (response) => {
|
|
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
|
-
|
|
797
|
-
|
|
798
|
-
if (cachingConfirmed) {
|
|
796
|
+
}
|
|
799
797
|
|
|
800
|
-
|
|
798
|
+
if (cachingConfirmed) {
|
|
801
799
|
|
|
802
|
-
|
|
800
|
+
const cacheIndex = apiRequestCache.findIndex(cache => cache.requestArgumentsSerialized === querySerialized);
|
|
803
801
|
|
|
804
|
-
|
|
805
|
-
apiRequestCache[cacheIndex].response = response
|
|
802
|
+
apiRequestCache[cacheIndex].final = false === returnGetNextPageFunction
|
|
806
803
|
|
|
807
|
-
|
|
804
|
+
// only cache get method requests
|
|
805
|
+
apiRequestCache[cacheIndex].response = response
|
|
808
806
|
|
|
809
|
-
|
|
807
|
+
}
|
|
810
808
|
|
|
811
|
-
|
|
809
|
+
apiResponse = TestRestfulResponse(response, request?.success, request?.error ?? "An unexpected API error occurred!")
|
|
812
810
|
|
|
813
|
-
if (
|
|
811
|
+
if (false === apiResponse) {
|
|
814
812
|
|
|
815
|
-
|
|
813
|
+
if (request.debug && isLocal) {
|
|
816
814
|
|
|
817
|
-
|
|
815
|
+
toast.warning("DEVS: TestRestfulResponse returned false for (" + operatingTable + ").", toastOptionsDevs);
|
|
818
816
|
|
|
819
|
-
|
|
817
|
+
}
|
|
820
818
|
|
|
821
|
-
|
|
819
|
+
return response;
|
|
822
820
|
|
|
823
|
-
|
|
821
|
+
}
|
|
824
822
|
|
|
825
|
-
|
|
823
|
+
responseCallback(response, request, apiResponse)
|
|
826
824
|
|
|
827
|
-
|
|
825
|
+
if (C6.GET === requestMethod) {
|
|
828
826
|
|
|
829
|
-
|
|
830
|
-
query?.[C6.PAGINATION]?.[C6.LIMIT] === responseData.rest.length
|
|
827
|
+
const responseData = response.data as iGetC6RestResponse<any>;
|
|
831
828
|
|
|
832
|
-
|
|
829
|
+
returnGetNextPageFunction = 1 !== query?.[C6.PAGINATION]?.[C6.LIMIT] &&
|
|
830
|
+
query?.[C6.PAGINATION]?.[C6.LIMIT] === responseData.rest.length
|
|
833
831
|
|
|
834
|
-
|
|
832
|
+
if (false === isTest || true === isVerbose) {
|
|
835
833
|
|
|
836
|
-
|
|
834
|
+
console.groupCollapsed('%c API: Response (' + requestMethod + ' ' + tableName + ') returned length (' + responseData.rest?.length + ') of possible (' + query?.[C6.PAGINATION]?.[C6.LIMIT] + ') limit!', 'color: #0c0')
|
|
837
835
|
|
|
838
|
-
|
|
836
|
+
console.log('%c ' + requestMethod + ' ' + tableName, 'color: #0c0')
|
|
839
837
|
|
|
840
|
-
|
|
838
|
+
console.log('%c Request Data (note you may see the success and/or error prompt):', 'color: #0c0', request)
|
|
841
839
|
|
|
842
|
-
|
|
840
|
+
console.log('%c Response Data:', 'color: #0c0', responseData.rest)
|
|
843
841
|
|
|
844
|
-
|
|
842
|
+
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)
|
|
845
843
|
|
|
846
|
-
|
|
844
|
+
console.trace();
|
|
847
845
|
|
|
848
|
-
|
|
846
|
+
console.groupEnd()
|
|
849
847
|
|
|
850
|
-
|
|
851
|
-
&& true === request.debug
|
|
852
|
-
&& isLocal) {
|
|
848
|
+
}
|
|
853
849
|
|
|
854
|
-
|
|
850
|
+
if (false === returnGetNextPageFunction
|
|
851
|
+
&& true === request.debug
|
|
852
|
+
&& isLocal) {
|
|
855
853
|
|
|
856
|
-
|
|
854
|
+
toast.success("DEVS: Response returned length (" + responseData.rest?.length + ") less than limit (" + query?.[C6.PAGINATION]?.[C6.LIMIT] + ").", toastOptionsDevs);
|
|
857
855
|
|
|
858
|
-
|
|
859
|
-
&& 'number' === typeof request.fetchDependencies) {
|
|
856
|
+
}
|
|
860
857
|
|
|
861
|
-
|
|
858
|
+
request.fetchDependencies ??= eFetchDependencies.NONE;
|
|
862
859
|
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
}, fetchDependencies: number) => Object.keys(
|
|
866
|
-
fetchData
|
|
867
|
-
).map((column) => {
|
|
860
|
+
if (request.fetchDependencies
|
|
861
|
+
&& 'number' === typeof request.fetchDependencies) {
|
|
868
862
|
|
|
869
|
-
|
|
863
|
+
const fetchDependencies = request.fetchDependencies as number;
|
|
870
864
|
|
|
871
|
-
|
|
872
|
-
// todo - this may need [0][x]
|
|
873
|
-
if (!(column in responseData.rest)
|
|
874
|
-
&& !(0 in responseData.rest
|
|
875
|
-
&& column in responseData.rest[0])) {
|
|
865
|
+
console.groupCollapsed('%c API: Fetch Dependencies segment (' + requestMethod + ' ' + tableName + ')', 'color: #33ccff')
|
|
876
866
|
|
|
877
|
-
|
|
867
|
+
// noinspection JSBitwiseOperatorUsage
|
|
868
|
+
let dependencies: { [key: string]: iConstraint[] } = {
|
|
869
|
+
...fetchDependencies & eFetchDependencies.REFERENCED ? C6.TABLES[operatingTable].TABLE_REFERENCED_BY : {},
|
|
870
|
+
...fetchDependencies & eFetchDependencies.REFERENCES ? C6.TABLES[operatingTable].TABLE_REFERENCES : {}
|
|
871
|
+
};
|
|
878
872
|
|
|
879
|
-
|
|
873
|
+
let fetchReferences: {
|
|
874
|
+
[externalTable: string]: {
|
|
875
|
+
[column: string]: string[]
|
|
876
|
+
}
|
|
877
|
+
} = {}
|
|
880
878
|
|
|
881
|
-
|
|
879
|
+
let apiRequestPromises: Array<apiReturn<iGetC6RestResponse<any>>> = []
|
|
882
880
|
|
|
883
|
-
|
|
881
|
+
Object.keys(
|
|
882
|
+
dependencies
|
|
883
|
+
).forEach(column => dependencies[column].forEach((constraint) => {
|
|
884
|
+
fetchReferences[constraint.TABLE] ??= {};
|
|
885
|
+
fetchReferences[constraint.TABLE][constraint.COLUMN] = []
|
|
886
|
+
fetchReferences[constraint.TABLE][constraint.COLUMN].push(responseData.rest[column] ?? responseData.rest.map((row) => row[column]))
|
|
887
|
+
}));
|
|
884
888
|
|
|
885
|
-
|
|
889
|
+
for (const table in fetchReferences) {
|
|
886
890
|
|
|
887
891
|
if (fetchDependencies & eFetchDependencies.C6ENTITY
|
|
888
892
|
&& tableName === "carbon_carbons") {
|
|
@@ -890,101 +894,88 @@ export default function restApi<
|
|
|
890
894
|
// todo - let's assume this handles a single row for now, handle multiple rows could improve performance
|
|
891
895
|
// this more or less implies
|
|
892
896
|
// todo - rethink the table ref entity system - when tables are renamed? no hooks exist in mysql
|
|
893
|
-
const
|
|
897
|
+
const tagFilter = (tag: string) => tag.split('/').pop() ?? ''
|
|
898
|
+
|
|
899
|
+
// since were already filtering on column, we can assume the first row constraint is the same as the rest
|
|
900
|
+
const referencesTable: string = tagFilter(responseData.rest[0]['entity_tag'] ?? '')
|
|
894
901
|
|
|
895
|
-
|
|
902
|
+
// todo - allow c6 requests with multiple table endpoints to be fetched
|
|
903
|
+
// const referencesTables: string[] = !(0 in responseData.rest) ? [tagFilter(responseData.rest['entity_tag'] ?? '')] : responseData.rest.map(row => tagFilter(row['entity_tag'] ?? '')).filter(n => n)
|
|
904
|
+
if (!table.endsWith(referencesTable)) {
|
|
896
905
|
|
|
897
|
-
console.log('%c C6ENTITY: The constraintTableName (' +
|
|
906
|
+
console.log('%c C6ENTITY: The constraintTableName (' + table + ') did not end with referencesTable (' + referencesTable + ')', 'color: #c00')
|
|
898
907
|
|
|
899
|
-
|
|
908
|
+
continue;
|
|
900
909
|
|
|
901
910
|
}
|
|
902
911
|
|
|
903
|
-
console.log('%c C6ENTITY: The constraintTableName (' +
|
|
912
|
+
console.log('%c C6ENTITY: The constraintTableName (' + table + ') ended with referencesTable (' + referencesTable + ')', 'color: #0c0')
|
|
904
913
|
|
|
905
914
|
}
|
|
906
915
|
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
const fetchTable = await C6.IMPORT(constraint.TABLE)
|
|
916
|
+
const fetchTable = await C6.IMPORT(table)
|
|
910
917
|
|
|
911
918
|
const RestApi = fetchTable.default
|
|
912
919
|
|
|
913
|
-
console.log('Fetch Dependencies will select (' +
|
|
920
|
+
console.log('Fetch Dependencies will select (' + table + ') using GET request')
|
|
914
921
|
|
|
915
922
|
// todo - filter out ids that exist in state?!?
|
|
916
923
|
|
|
917
924
|
// this is a dynamic call to the rest api, any generated table may resolve with (RestApi)
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
},
|
|
923
|
-
fetchDependencies: fetchDependencies & eFetchDependencies.RECURSIVE
|
|
924
|
-
|| (fetchDependencies & eFetchDependencies.C6ENTITY
|
|
925
|
-
&& constraintTableName === "carbon_carbons")
|
|
926
|
-
? fetchDependencies
|
|
927
|
-
: eFetchDependencies.NONE
|
|
928
|
-
})
|
|
929
|
-
|
|
930
|
-
});
|
|
925
|
+
// todo - using value to avoid joins.... but. maybe this should be a parameterizable option -- think race conditions; its safer to join
|
|
926
|
+
apiRequestPromises.push(RestApi.Get({
|
|
927
|
+
[C6.WHERE]: {
|
|
928
|
+
0: Object.keys(fetchReferences[table]).reduce((sum, column) => {
|
|
931
929
|
|
|
932
|
-
|
|
930
|
+
fetchReferences[table][column] = fetchReferences[table][column].flat(Infinity)
|
|
933
931
|
|
|
934
|
-
|
|
932
|
+
if (0 === fetchReferences[table][column].length) {
|
|
935
933
|
|
|
936
|
-
|
|
937
|
-
if (request.fetchDependencies & eFetchDependencies.REFERENCED) {
|
|
934
|
+
console.warn('The column (' + column + ') was not found in the response data. We will not fetch.', responseData)
|
|
938
935
|
|
|
939
|
-
|
|
936
|
+
return false;
|
|
940
937
|
|
|
941
|
-
|
|
938
|
+
}
|
|
942
939
|
|
|
943
|
-
|
|
940
|
+
sum[column] = fetchReferences[table][column].length === 1 ? fetchReferences[table][column][0] : [
|
|
941
|
+
[C6.IN, fetchReferences[table][column]]
|
|
942
|
+
]
|
|
944
943
|
|
|
945
|
-
|
|
944
|
+
return sum
|
|
945
|
+
}, {})
|
|
946
|
+
},
|
|
947
|
+
fetchDependencies: fetchDependencies & eFetchDependencies.RECURSIVE
|
|
948
|
+
|| (fetchDependencies & eFetchDependencies.C6ENTITY
|
|
949
|
+
&& table === "carbon_carbons")
|
|
950
|
+
? fetchDependencies
|
|
951
|
+
: eFetchDependencies.NONE
|
|
952
|
+
}
|
|
953
|
+
));
|
|
946
954
|
|
|
947
955
|
}
|
|
948
956
|
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
if (request.fetchDependencies & eFetchDependencies.REFERENCES) {
|
|
957
|
+
await Promise.all(apiRequestPromises)
|
|
952
958
|
|
|
953
|
-
|
|
959
|
+
request.fetchDependencies = apiRequestPromises
|
|
954
960
|
|
|
955
|
-
console.
|
|
961
|
+
console.trace();
|
|
956
962
|
|
|
957
|
-
|
|
963
|
+
console.groupEnd()
|
|
958
964
|
|
|
959
|
-
dependencies.push(await fetchDependencies(references, request.fetchDependencies))
|
|
960
|
-
|
|
961
|
-
}
|
|
962
965
|
}
|
|
963
966
|
|
|
964
|
-
dependencies = dependencies.flat(Infinity)
|
|
965
|
-
|
|
966
|
-
request.fetchDependencies = dependencies;
|
|
967
|
-
|
|
968
|
-
await Promise.all(dependencies)
|
|
969
|
-
|
|
970
|
-
console.trace();
|
|
971
|
-
|
|
972
|
-
console.groupEnd()
|
|
973
967
|
|
|
974
968
|
}
|
|
975
969
|
|
|
970
|
+
if (request.debug && isLocal) {
|
|
976
971
|
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
if (request.debug && isLocal) {
|
|
980
|
-
|
|
981
|
-
toast.success("DEVS: (" + requestMethod + ") request complete.", toastOptionsDevs);
|
|
972
|
+
toast.success("DEVS: (" + requestMethod + ") request complete.", toastOptionsDevs);
|
|
982
973
|
|
|
983
|
-
|
|
974
|
+
}
|
|
984
975
|
|
|
985
|
-
|
|
976
|
+
return response;
|
|
986
977
|
|
|
987
|
-
|
|
978
|
+
});
|
|
988
979
|
|
|
989
980
|
} catch (error) {
|
|
990
981
|
|