@carbonorm/carbonnode 1.6.10 → 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 -87
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +90 -87
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/api/restRequest.ts +108 -118
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,111 +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
|
-
|
|
793
|
-
|
|
794
|
-
return Promise.reject(response);
|
|
792
|
+
}
|
|
795
793
|
|
|
796
|
-
|
|
794
|
+
return Promise.reject(response);
|
|
797
795
|
|
|
798
|
-
|
|
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
|
-
|
|
818
|
-
|
|
819
|
-
return response;
|
|
815
|
+
toast.warning("DEVS: TestRestfulResponse returned false for (" + operatingTable + ").", toastOptionsDevs);
|
|
820
816
|
|
|
821
|
-
|
|
817
|
+
}
|
|
822
818
|
|
|
823
|
-
|
|
819
|
+
return response;
|
|
824
820
|
|
|
825
|
-
|
|
821
|
+
}
|
|
826
822
|
|
|
827
|
-
|
|
823
|
+
responseCallback(response, request, apiResponse)
|
|
828
824
|
|
|
829
|
-
|
|
830
|
-
query?.[C6.PAGINATION]?.[C6.LIMIT] === responseData.rest.length
|
|
825
|
+
if (C6.GET === requestMethod) {
|
|
831
826
|
|
|
832
|
-
|
|
827
|
+
const responseData = response.data as iGetC6RestResponse<any>;
|
|
833
828
|
|
|
834
|
-
|
|
829
|
+
returnGetNextPageFunction = 1 !== query?.[C6.PAGINATION]?.[C6.LIMIT] &&
|
|
830
|
+
query?.[C6.PAGINATION]?.[C6.LIMIT] === responseData.rest.length
|
|
835
831
|
|
|
836
|
-
|
|
832
|
+
if (false === isTest || true === isVerbose) {
|
|
837
833
|
|
|
838
|
-
|
|
834
|
+
console.groupCollapsed('%c API: Response (' + requestMethod + ' ' + tableName + ') returned length (' + responseData.rest?.length + ') of possible (' + query?.[C6.PAGINATION]?.[C6.LIMIT] + ') limit!', 'color: #0c0')
|
|
839
835
|
|
|
840
|
-
|
|
836
|
+
console.log('%c ' + requestMethod + ' ' + tableName, 'color: #0c0')
|
|
841
837
|
|
|
842
|
-
|
|
838
|
+
console.log('%c Request Data (note you may see the success and/or error prompt):', 'color: #0c0', request)
|
|
843
839
|
|
|
844
|
-
|
|
840
|
+
console.log('%c Response Data:', 'color: #0c0', responseData.rest)
|
|
845
841
|
|
|
846
|
-
|
|
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)
|
|
847
843
|
|
|
848
|
-
|
|
844
|
+
console.trace();
|
|
849
845
|
|
|
850
|
-
|
|
851
|
-
&& true === request.debug
|
|
852
|
-
&& isLocal) {
|
|
846
|
+
console.groupEnd()
|
|
853
847
|
|
|
854
|
-
|
|
848
|
+
}
|
|
855
849
|
|
|
856
|
-
|
|
850
|
+
if (false === returnGetNextPageFunction
|
|
851
|
+
&& true === request.debug
|
|
852
|
+
&& isLocal) {
|
|
857
853
|
|
|
858
|
-
|
|
859
|
-
&& 'number' === typeof request.fetchDependencies) {
|
|
854
|
+
toast.success("DEVS: Response returned length (" + responseData.rest?.length + ") less than limit (" + query?.[C6.PAGINATION]?.[C6.LIMIT] + ").", toastOptionsDevs);
|
|
860
855
|
|
|
861
|
-
|
|
856
|
+
}
|
|
862
857
|
|
|
863
|
-
|
|
864
|
-
[key: string]: iConstraint[]
|
|
865
|
-
}, fetchDependencies: number) => Object.keys(
|
|
866
|
-
fetchData
|
|
867
|
-
).map((column) => {
|
|
858
|
+
request.fetchDependencies ??= eFetchDependencies.NONE;
|
|
868
859
|
|
|
869
|
-
|
|
860
|
+
if (request.fetchDependencies
|
|
861
|
+
&& 'number' === typeof request.fetchDependencies) {
|
|
870
862
|
|
|
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])) {
|
|
863
|
+
const fetchDependencies = request.fetchDependencies as number;
|
|
876
864
|
|
|
877
|
-
|
|
865
|
+
console.groupCollapsed('%c API: Fetch Dependencies segment (' + requestMethod + ' ' + tableName + ')', 'color: #33ccff')
|
|
878
866
|
|
|
879
|
-
|
|
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
|
+
};
|
|
880
872
|
|
|
881
|
-
|
|
873
|
+
let fetchReferences: {
|
|
874
|
+
[externalTable: string]: {
|
|
875
|
+
[column: string]: string[]
|
|
876
|
+
}
|
|
877
|
+
} = {}
|
|
882
878
|
|
|
879
|
+
let apiRequestPromises: Array<apiReturn<iGetC6RestResponse<any>>> = []
|
|
883
880
|
|
|
884
|
-
|
|
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
|
+
}));
|
|
885
888
|
|
|
886
|
-
|
|
889
|
+
for (const table in fetchReferences) {
|
|
887
890
|
|
|
888
891
|
if (fetchDependencies & eFetchDependencies.C6ENTITY
|
|
889
892
|
&& tableName === "carbon_carbons") {
|
|
@@ -891,101 +894,88 @@ export default function restApi<
|
|
|
891
894
|
// todo - let's assume this handles a single row for now, handle multiple rows could improve performance
|
|
892
895
|
// this more or less implies
|
|
893
896
|
// todo - rethink the table ref entity system - when tables are renamed? no hooks exist in mysql
|
|
894
|
-
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'] ?? '')
|
|
895
901
|
|
|
896
|
-
|
|
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)) {
|
|
897
905
|
|
|
898
|
-
console.log('%c C6ENTITY: The constraintTableName (' +
|
|
906
|
+
console.log('%c C6ENTITY: The constraintTableName (' + table + ') did not end with referencesTable (' + referencesTable + ')', 'color: #c00')
|
|
899
907
|
|
|
900
|
-
|
|
908
|
+
continue;
|
|
901
909
|
|
|
902
910
|
}
|
|
903
911
|
|
|
904
|
-
console.log('%c C6ENTITY: The constraintTableName (' +
|
|
912
|
+
console.log('%c C6ENTITY: The constraintTableName (' + table + ') ended with referencesTable (' + referencesTable + ')', 'color: #0c0')
|
|
905
913
|
|
|
906
914
|
}
|
|
907
915
|
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
const fetchTable = await C6.IMPORT(constraint.TABLE)
|
|
916
|
+
const fetchTable = await C6.IMPORT(table)
|
|
911
917
|
|
|
912
918
|
const RestApi = fetchTable.default
|
|
913
919
|
|
|
914
|
-
console.log('Fetch Dependencies will select (' +
|
|
920
|
+
console.log('Fetch Dependencies will select (' + table + ') using GET request')
|
|
915
921
|
|
|
916
922
|
// todo - filter out ids that exist in state?!?
|
|
917
923
|
|
|
918
924
|
// this is a dynamic call to the rest api, any generated table may resolve with (RestApi)
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
},
|
|
924
|
-
fetchDependencies: fetchDependencies & eFetchDependencies.RECURSIVE
|
|
925
|
-
|| (fetchDependencies & eFetchDependencies.C6ENTITY
|
|
926
|
-
&& constraintTableName === "carbon_carbons")
|
|
927
|
-
? fetchDependencies
|
|
928
|
-
: eFetchDependencies.NONE
|
|
929
|
-
})
|
|
930
|
-
|
|
931
|
-
});
|
|
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) => {
|
|
932
929
|
|
|
933
|
-
|
|
930
|
+
fetchReferences[table][column] = fetchReferences[table][column].flat(Infinity)
|
|
934
931
|
|
|
935
|
-
|
|
932
|
+
if (0 === fetchReferences[table][column].length) {
|
|
936
933
|
|
|
937
|
-
|
|
938
|
-
if (request.fetchDependencies & eFetchDependencies.REFERENCED) {
|
|
934
|
+
console.warn('The column (' + column + ') was not found in the response data. We will not fetch.', responseData)
|
|
939
935
|
|
|
940
|
-
|
|
936
|
+
return false;
|
|
941
937
|
|
|
942
|
-
|
|
938
|
+
}
|
|
943
939
|
|
|
944
|
-
|
|
940
|
+
sum[column] = fetchReferences[table][column].length === 1 ? fetchReferences[table][column][0] : [
|
|
941
|
+
[C6.IN, fetchReferences[table][column]]
|
|
942
|
+
]
|
|
945
943
|
|
|
946
|
-
|
|
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
|
+
));
|
|
947
954
|
|
|
948
955
|
}
|
|
949
956
|
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
if (request.fetchDependencies & eFetchDependencies.REFERENCES) {
|
|
957
|
+
await Promise.all(apiRequestPromises)
|
|
953
958
|
|
|
954
|
-
|
|
959
|
+
request.fetchDependencies = apiRequestPromises
|
|
955
960
|
|
|
956
|
-
console.
|
|
961
|
+
console.trace();
|
|
957
962
|
|
|
958
|
-
|
|
963
|
+
console.groupEnd()
|
|
959
964
|
|
|
960
|
-
dependencies.push(await fetchDependencies(references, request.fetchDependencies))
|
|
961
|
-
|
|
962
|
-
}
|
|
963
965
|
}
|
|
964
966
|
|
|
965
|
-
dependencies = dependencies.flat(Infinity)
|
|
966
|
-
|
|
967
|
-
request.fetchDependencies = dependencies;
|
|
968
|
-
|
|
969
|
-
await Promise.all(dependencies)
|
|
970
|
-
|
|
971
|
-
console.trace();
|
|
972
|
-
|
|
973
|
-
console.groupEnd()
|
|
974
967
|
|
|
975
968
|
}
|
|
976
969
|
|
|
970
|
+
if (request.debug && isLocal) {
|
|
977
971
|
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
if (request.debug && isLocal) {
|
|
981
|
-
|
|
982
|
-
toast.success("DEVS: (" + requestMethod + ") request complete.", toastOptionsDevs);
|
|
972
|
+
toast.success("DEVS: (" + requestMethod + ") request complete.", toastOptionsDevs);
|
|
983
973
|
|
|
984
|
-
|
|
974
|
+
}
|
|
985
975
|
|
|
986
|
-
|
|
976
|
+
return response;
|
|
987
977
|
|
|
988
|
-
|
|
978
|
+
});
|
|
989
979
|
|
|
990
980
|
} catch (error) {
|
|
991
981
|
|