@carbonorm/carbonnode 3.0.3 → 3.0.5

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.
@@ -5,18 +5,16 @@ import isTest from "../../variables/isTest";
5
5
  import isVerbose from "../../variables/isVerbose";
6
6
  import convertForRequestBody from "../convertForRequestBody";
7
7
  import {eFetchDependencies} from "../types/dynamicFetching";
8
- import {Modify} from "../types/modifyTypes";
9
8
  import {
10
9
  apiReturn,
11
10
  DELETE, DetermineResponseDataType,
12
- GET, iAPI,
11
+ GET,
13
12
  iCacheAPI,
14
13
  iConstraint,
15
14
  iGetC6RestResponse,
16
15
  iRestMethods,
17
16
  POST,
18
- PUT,
19
- RequestQueryBody
17
+ PUT, RequestQueryBody
20
18
  } from "../types/ormInterfaces";
21
19
  import {removeInvalidKeys, removePrefixIfExists, TestRestfulResponse} from "../utils/apiHelpers";
22
20
  import {apiRequestCache, checkCache, userCustomClearCache} from "../utils/cacheManager";
@@ -43,7 +41,12 @@ export class HttpExecutor<
43
41
 
44
42
  public putState(
45
43
  response: AxiosResponse<DetermineResponseDataType<RequestMethod, RestTableInterface>>,
46
- request: iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields,
44
+ request: RequestQueryBody<
45
+ RequestMethod,
46
+ RestTableInterface,
47
+ CustomAndRequiredFields,
48
+ RequestTableOverrides
49
+ >,
47
50
  callback: () => void
48
51
  ) {
49
52
  this.config.reactBootstrap?.updateRestfulObjectArrays<RestTableInterface>({
@@ -61,7 +64,12 @@ export class HttpExecutor<
61
64
 
62
65
  public postState(
63
66
  response: AxiosResponse<DetermineResponseDataType<RequestMethod, RestTableInterface>>,
64
- request: iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields,
67
+ request: RequestQueryBody<
68
+ RequestMethod,
69
+ RestTableInterface,
70
+ CustomAndRequiredFields,
71
+ RequestTableOverrides
72
+ >,
65
73
  callback: () => void
66
74
  ) {
67
75
 
@@ -101,7 +109,12 @@ export class HttpExecutor<
101
109
 
102
110
  public deleteState(
103
111
  _response: AxiosResponse<DetermineResponseDataType<RequestMethod, RestTableInterface>>,
104
- request: iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields,
112
+ request: RequestQueryBody<
113
+ RequestMethod,
114
+ RestTableInterface,
115
+ CustomAndRequiredFields,
116
+ RequestTableOverrides
117
+ >,
105
118
  callback: () => void
106
119
  ) {
107
120
  this.config.reactBootstrap?.deleteRestfulObjectArrays<RestTableInterface>({
@@ -172,17 +185,11 @@ export class HttpExecutor<
172
185
 
173
186
  // an undefined query would indicate queryCallback returned undefined,
174
187
  // thus the request shouldn't fire as is in custom cache
175
- let query: RequestQueryBody<Modify<RestTableInterface, RequestTableOverrides>> | undefined | null;
188
+ if (undefined === this.request || null === this.request) {
176
189
 
177
- if (undefined === query || null === query) {
190
+ console.groupCollapsed('%c API: (' + requestMethod + ') Request Query for (' + tableName + ') undefined, returning null (will not fire)!', 'color: #c00')
178
191
 
179
- if (this.request.debug && isLocal) {
180
-
181
- toast.warning("DEV: queryCallback returned undefined, signaling in Custom Cache. (returning null)", toastOptionsDevs)
182
-
183
- }
184
-
185
- console.groupCollapsed('%c API: (' + requestMethod + ') Request Query for (' + tableName + ') undefined, returning null (will not fire ajax)!', 'color: #c00')
192
+ console.log('request', this.request)
186
193
 
187
194
  console.log('%c Returning (undefined|null) for a query would indicate a custom cache hit (outside API.tsx), thus the request should not fire.', 'color: #c00')
188
195
 
@@ -194,6 +201,8 @@ export class HttpExecutor<
194
201
 
195
202
  }
196
203
 
204
+ let query = this.request;
205
+
197
206
  if (C6.GET === requestMethod) {
198
207
 
199
208
  if (undefined === query[C6.PAGINATION]) {
@@ -251,7 +260,12 @@ export class HttpExecutor<
251
260
 
252
261
  if (undefined === query || null === query) {
253
262
 
254
- query = {}
263
+ query = {} as RequestQueryBody<
264
+ RequestMethod,
265
+ RestTableInterface,
266
+ CustomAndRequiredFields,
267
+ RequestTableOverrides
268
+ >
255
269
 
256
270
  }
257
271
 
@@ -412,8 +426,13 @@ export class HttpExecutor<
412
426
  const removedPkValue = query[primaryKey];
413
427
 
414
428
  addBackPK = () => {
415
- query ??= {}
416
- query[primaryKey] = removedPkValue
429
+ query ??= {} as RequestQueryBody<
430
+ RequestMethod,
431
+ RestTableInterface,
432
+ CustomAndRequiredFields,
433
+ RequestTableOverrides
434
+ >;
435
+ query[primaryKey] = removedPkValue;
417
436
  }
418
437
 
419
438
  delete query[primaryKey]
@@ -448,62 +467,56 @@ export class HttpExecutor<
448
467
 
449
468
  const axiosActiveRequest: AxiosPromise<ResponseDataType> = axios![requestMethod.toLowerCase()]<ResponseDataType>(
450
469
  restRequestUri,
451
- ...((() => {
452
-
453
- // @link - https://axios-http.com/docs/instance
454
- // How configuration vs data is passed is variable, use documentation above for reference
455
- if (requestMethod === GET) {
456
-
457
- return [{
458
- withCredentials: withCredentials,
459
- params: query
460
- }]
461
-
462
- } else if (requestMethod === POST) {
463
-
464
- if (undefined !== dataInsertMultipleRows) {
465
-
466
- return [
467
- dataInsertMultipleRows.map(data =>
468
- convertForRequestBody<typeof data>(data, fullTableList, C6, (message) => toast.error(message, toastOptions))),
469
- {
470
- withCredentials: withCredentials,
471
- }
472
- ]
473
-
474
- }
475
-
476
- return [
477
- convertForRequestBody<RestTableInterface>(query as RestTableInterface, fullTableList, C6, (message) => toast.error(message, toastOptions)),
478
- {
479
- withCredentials: withCredentials,
480
- }
481
- ]
482
-
483
- } else if (requestMethod === PUT) {
484
-
485
- return [
486
- convertForRequestBody<RestTableInterface>(query as RestTableInterface, fullTableList, C6, (message) => toast.error(message, toastOptions)),
487
- {
488
- withCredentials: withCredentials,
470
+ ...(() => {
471
+ const convert = (data: any) =>
472
+ convertForRequestBody<
473
+ RequestMethod,
474
+ RestTableInterface,
475
+ CustomAndRequiredFields,
476
+ RequestTableOverrides
477
+ >(
478
+ data,
479
+ fullTableList,
480
+ C6,
481
+ (message) => toast.error(message, toastOptions)
482
+ );
483
+
484
+ const baseConfig = {
485
+ withCredentials: withCredentials,
486
+ };
487
+
488
+ switch (requestMethod) {
489
+ case GET:
490
+ return [{
491
+ ...baseConfig,
492
+ params: query
493
+ }];
494
+
495
+ case POST:
496
+ if (dataInsertMultipleRows !== undefined) {
497
+ return [
498
+ dataInsertMultipleRows.map(convert),
499
+ baseConfig
500
+ ];
489
501
  }
490
- ]
491
- } else if (requestMethod === DELETE) {
492
-
493
- return [{
494
- withCredentials: withCredentials,
495
- data: convertForRequestBody<RestTableInterface>(query as RestTableInterface, fullTableList, C6, (message) => toast.error(message, toastOptions))
496
- }]
502
+ return [convert(query), baseConfig];
497
503
 
498
- } else {
504
+ case PUT:
505
+ return [convert(query), baseConfig];
499
506
 
500
- throw new Error('The request method (' + requestMethod + ') was not recognized.')
507
+ case DELETE:
508
+ return [{
509
+ ...baseConfig,
510
+ data: convert(query)
511
+ }];
501
512
 
513
+ default:
514
+ throw new Error(`The request method (${requestMethod}) was not recognized.`);
502
515
  }
503
-
504
- })())
516
+ })()
505
517
  );
506
518
 
519
+
507
520
  if (cachingConfirmed) {
508
521
 
509
522
  // push to cache so we do not repeat the request
@@ -1,9 +1,7 @@
1
1
  import isNode from '../variables/isNode';
2
- import {Modify} from "./types/modifyTypes";
3
2
  import {
4
3
  apiReturn, DetermineResponseDataType,
5
- iAPI,
6
- iRest, iRestMethods
4
+ iRest, iRestMethods, RequestQueryBody
7
5
  } from "./types/ormInterfaces";
8
6
 
9
7
  /**
@@ -24,7 +22,12 @@ export default function restRequest<
24
22
  >
25
23
  ) {
26
24
  return async (
27
- request: iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields = {} as iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields
25
+ request: RequestQueryBody<
26
+ RequestMethod,
27
+ RestTableInterface,
28
+ CustomAndRequiredFields,
29
+ RequestTableOverrides
30
+ >,
28
31
  ): Promise<apiReturn<DetermineResponseDataType<RequestMethod, RestTableInterface>>> => {
29
32
 
30
33
  // SQL path if on Node with a provided pool