@carbonorm/carbonnode 1.0.0 → 1.0.2

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.
@@ -4,7 +4,7 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.carbonNode = {}, global["react-toastify"]));
5
5
  })(this, (function (exports, reactToastify) { 'use strict';
6
6
 
7
- const C6 = {
7
+ var C6 = {
8
8
  // try to 1=1 match the Rest abstract class
9
9
  ADDDATE: 'ADDDATE',
10
10
  ADDTIME: 'ADDTIME',
@@ -130,13 +130,47 @@
130
130
  VALIDATE_C6_ENTITY_ID_REGEX: '#^([a-fA-F0-9]{20,35})$#',
131
131
  };
132
132
 
133
- function convertForRequestBody (restfulObject, tableName, regexErrorHandler = alert) {
134
- let payload = {};
135
- const tableNames = Array.isArray(tableName) ? tableName : [tableName];
136
- tableNames.forEach((table) => {
137
- Object.keys(restfulObject).map(value => {
133
+ /******************************************************************************
134
+ Copyright (c) Microsoft Corporation.
135
+
136
+ Permission to use, copy, modify, and/or distribute this software for any
137
+ purpose with or without fee is hereby granted.
138
+
139
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
140
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
141
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
142
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
143
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
144
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
145
+ PERFORMANCE OF THIS SOFTWARE.
146
+ ***************************************************************************** */
147
+ /* global Reflect, Promise, SuppressedError, Symbol */
148
+
149
+
150
+ var __assign = function() {
151
+ __assign = Object.assign || function __assign(t) {
152
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
153
+ s = arguments[i];
154
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
155
+ }
156
+ return t;
157
+ };
158
+ return __assign.apply(this, arguments);
159
+ };
160
+
161
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
162
+ var e = new Error(message);
163
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
164
+ };
165
+
166
+ function convertForRequestBody (restfulObject, tableName, regexErrorHandler) {
167
+ if (regexErrorHandler === void 0) { regexErrorHandler = alert; }
168
+ var payload = {};
169
+ var tableNames = Array.isArray(tableName) ? tableName : [tableName];
170
+ tableNames.forEach(function (table) {
171
+ Object.keys(restfulObject).map(function (value) {
138
172
  var _a;
139
- let shortReference = value.toUpperCase();
173
+ var shortReference = value.toUpperCase();
140
174
  switch (value) {
141
175
  case C6.GET:
142
176
  case C6.POST:
@@ -152,25 +186,28 @@
152
186
  else if (typeof restfulObject[value] === 'object' && restfulObject[value] !== null) {
153
187
  payload[value] = Object.keys(restfulObject[value])
154
188
  .sort()
155
- .reduce((acc, key) => (Object.assign(Object.assign({}, acc), { [key]: restfulObject[value][key] })), {});
189
+ .reduce(function (acc, key) {
190
+ var _a;
191
+ return (__assign(__assign({}, acc), (_a = {}, _a[key] = restfulObject[value][key], _a)));
192
+ }, {});
156
193
  }
157
194
  return;
158
195
  }
159
196
  if (shortReference in C6[table]) {
160
- const longName = C6[table][shortReference];
161
- payload[longName] = restfulObject[value];
162
- const regexValidations = C6[table].REGEX_VALIDATION[longName];
163
- if (regexValidations instanceof RegExp) {
164
- if (false === regexValidations.test(restfulObject[value])) {
165
- regexErrorHandler('Failed to match regex (' + regexValidations + ') for column (' + longName + ')');
166
- throw Error('Failed to match regex (' + regexValidations + ') for column (' + longName + ')');
197
+ var longName_1 = C6[table][shortReference];
198
+ payload[longName_1] = restfulObject[value];
199
+ var regexValidations_1 = C6[table].REGEX_VALIDATION[longName_1];
200
+ if (regexValidations_1 instanceof RegExp) {
201
+ if (false === regexValidations_1.test(restfulObject[value])) {
202
+ regexErrorHandler('Failed to match regex (' + regexValidations_1 + ') for column (' + longName_1 + ')');
203
+ throw Error('Failed to match regex (' + regexValidations_1 + ') for column (' + longName_1 + ')');
167
204
  }
168
205
  }
169
- else if (typeof regexValidations === 'object' && regexValidations !== null) {
170
- (_a = Object.keys(regexValidations)) === null || _a === void 0 ? void 0 : _a.map((errorMessage) => {
171
- const regex = regexValidations[errorMessage];
206
+ else if (typeof regexValidations_1 === 'object' && regexValidations_1 !== null) {
207
+ (_a = Object.keys(regexValidations_1)) === null || _a === void 0 ? void 0 : _a.map(function (errorMessage) {
208
+ var regex = regexValidations_1[errorMessage];
172
209
  if (false === regex.test(restfulObject[value])) {
173
- const devErrorMessage = 'Failed to match regex (' + regex + ') for column (' + longName + ')';
210
+ var devErrorMessage = 'Failed to match regex (' + regex + ') for column (' + longName_1 + ')';
174
211
  regexErrorHandler(errorMessage !== null && errorMessage !== void 0 ? errorMessage : devErrorMessage);
175
212
  throw Error(devErrorMessage);
176
213
  }
@@ -182,16 +219,19 @@
182
219
  });
183
220
  return Object.keys(payload)
184
221
  .sort()
185
- .reduce((acc, key) => (Object.assign(Object.assign({}, acc), { [key]: payload[key] })), {});
222
+ .reduce(function (acc, key) {
223
+ var _a;
224
+ return (__assign(__assign({}, acc), (_a = {}, _a[key] = payload[key], _a)));
225
+ }, {});
186
226
  }
187
227
 
188
228
  var isLocal = process.env.NODE_ENV === 'development';
189
229
 
190
- const isTest = process.env.JEST_WORKER_ID !== undefined || process.env.NODE_ENV === 'test';
230
+ var isTest = process.env.JEST_WORKER_ID !== undefined || process.env.NODE_ENV === 'test';
191
231
 
192
- const isVerbose = process.env.REACT_APP_VERBOSE === 'true';
232
+ var isVerbose = process.env.REACT_APP_VERBOSE === 'true';
193
233
 
194
- const toastOptions = {
234
+ var toastOptions = {
195
235
  position: "bottom-left",
196
236
  autoClose: 10000,
197
237
  hideProgressBar: false,
@@ -200,7 +240,7 @@
200
240
  draggable: true,
201
241
  theme: "dark",
202
242
  };
203
- const toastOptionsDevs = {
243
+ var toastOptionsDevs = {
204
244
  position: "bottom-right",
205
245
  autoClose: 3000,
206
246
  hideProgressBar: false,
@@ -219,28 +259,28 @@
219
259
  || undefined !== ((_c = response.data) === null || _c === void 0 ? void 0 : _c.created)
220
260
  || undefined !== ((_d = response.data) === null || _d === void 0 ? void 0 : _d.updated)
221
261
  || undefined !== ((_e = response.data) === null || _e === void 0 ? void 0 : _e.deleted))) {
222
- let successReturn = 'function' === typeof success ? success === null || success === void 0 ? void 0 : success(response) : success;
262
+ var successReturn = 'function' === typeof success ? success === null || success === void 0 ? void 0 : success(response) : success;
223
263
  if (typeof successReturn === 'string') {
224
264
  reactToastify.toast.success(successReturn, toastOptions);
225
265
  }
226
266
  // this could end up with bad results for deleting id's === 0
227
267
  return (_h = (_g = (_f = response.data.created) !== null && _f !== void 0 ? _f : response.data.updated) !== null && _g !== void 0 ? _g : response.data.deleted) !== null && _h !== void 0 ? _h : true;
228
268
  }
229
- let errorReturn = 'function' === typeof error ? error === null || error === void 0 ? void 0 : error(response) : error;
269
+ var errorReturn = 'function' === typeof error ? error === null || error === void 0 ? void 0 : error(response) : error;
230
270
  if (typeof errorReturn === 'string') {
231
271
  reactToastify.toast.error(errorReturn, toastOptions);
232
272
  }
233
273
  return false;
234
274
  }
235
275
  function removeInvalidKeys(request, c6Tables) {
236
- let intersection = {};
237
- let restfulObjectKeys = [];
238
- c6Tables.forEach(table => Object.values(table.COLUMNS).forEach(column => {
276
+ var intersection = {};
277
+ var restfulObjectKeys = [];
278
+ c6Tables.forEach(function (table) { return Object.values(table.COLUMNS).forEach(function (column) {
239
279
  if (false === restfulObjectKeys.includes(column)) {
240
280
  restfulObjectKeys.push(column);
241
281
  }
242
- }));
243
- Object.keys(request).forEach(key => {
282
+ }); });
283
+ Object.keys(request).forEach(function (key) {
244
284
  if (restfulObjectKeys.includes(key)) {
245
285
  intersection[key] = request[key];
246
286
  }
@@ -250,17 +290,17 @@
250
290
  }
251
291
  // do not remove entries from this array. It is used to track the progress of API requests.
252
292
  // position in array is important. Do not sort. To not add to begging.
253
- let apiRequestCache = [];
254
- let userCustomClearCache = [];
293
+ var apiRequestCache = [];
294
+ var userCustomClearCache = [];
255
295
  function checkAllRequestsComplete() {
256
- const stillRunning = apiRequestCache.filter((cache) => undefined === cache.response);
296
+ var stillRunning = apiRequestCache.filter(function (cache) { return undefined === cache.response; });
257
297
  if (stillRunning.length !== 0) {
258
298
  if (document === null || document === undefined) {
259
299
  throw new Error('document is undefined while waiting for API requests to complete (' + JSON.stringify(apiRequestCache) + ')');
260
300
  }
261
301
  // when requests return emtpy sets in full renders, it may not be possible to track their progress.
262
302
  console.warn('stillRunning...', stillRunning);
263
- return stillRunning.map((cache) => cache.requestArgumentsSerialized);
303
+ return stillRunning.map(function (cache) { return cache.requestArgumentsSerialized; });
264
304
  }
265
305
  return true;
266
306
  }
@@ -286,7 +326,7 @@
286
326
  return false;
287
327
  }
288
328
  function sortAndSerializeQueryObject(tables, query) {
289
- const orderedQuery = Object.keys(query).sort().reduce((obj, key) => {
329
+ var orderedQuery = Object.keys(query).sort().reduce(function (obj, key) {
290
330
  obj[key] = query[key];
291
331
  return obj;
292
332
  }, {});
@@ -296,7 +336,7 @@
296
336
  if (false === (props === null || props === void 0 ? void 0 : props.ignoreWarning)) {
297
337
  console.warn('The rest api clearCache should only be used with extreme care! Avoid using this in favor of using `cacheResults : boolean`.');
298
338
  }
299
- userCustomClearCache.map((f) => 'function' === typeof f && f());
339
+ userCustomClearCache.map(function (f) { return 'function' === typeof f && f(); });
300
340
  userCustomClearCache = apiRequestCache = [];
301
341
  }
302
342
  /**
@@ -309,20 +349,21 @@
309
349
  * For POST, PUT, and DELETE requests one can expect the primary key of the new or modified index, or a boolean success
310
350
  * indication if no primary key exists.
311
351
  **/
312
- const POST = 'POST';
313
- const PUT = 'PUT';
314
- const GET = 'GET';
315
- const DELETE = 'DELETE';
352
+ var POST = 'POST';
353
+ var PUT = 'PUT';
354
+ var GET = 'GET';
355
+ var DELETE = 'DELETE';
316
356
  function isPromise(x) {
317
357
  return Object(x).constructor === Promise;
318
358
  }
319
359
  function extendedTypeHints() {
320
- return (argv) => restApi(argv);
360
+ return function (argv) { return restApi(argv); };
321
361
  }
322
- function restApi({ C6, axios, restURL, tableName, requestMethod = GET, queryCallback = {}, responseCallback, skipPrimaryCheck = false, clearCache = undefined }) {
323
- const fullTableList = Array.isArray(tableName) ? tableName : [tableName];
324
- const operatingTable = fullTableList[0];
325
- const tables = fullTableList.join(',');
362
+ function restApi(_a) {
363
+ var C6 = _a.C6, axios = _a.axios, restURL = _a.restURL, tableName = _a.tableName, _b = _a.requestMethod, requestMethod = _b === void 0 ? GET : _b, _c = _a.queryCallback, queryCallback = _c === void 0 ? {} : _c, responseCallback = _a.responseCallback, _d = _a.skipPrimaryCheck, skipPrimaryCheck = _d === void 0 ? false : _d, _e = _a.clearCache, clearCache = _e === void 0 ? undefined : _e;
364
+ var fullTableList = Array.isArray(tableName) ? tableName : [tableName];
365
+ var operatingTable = fullTableList[0];
366
+ var tables = fullTableList.join(',');
326
367
  switch (requestMethod) {
327
368
  case GET:
328
369
  case POST:
@@ -335,10 +376,11 @@
335
376
  if (null !== clearCache || undefined !== clearCache) {
336
377
  userCustomClearCache[tables + requestMethod] = clearCache;
337
378
  }
338
- return (request = {}) => {
379
+ return function (request) {
380
+ if (request === void 0) { request = {}; }
339
381
  // an undefined query would indicate queryCallback returned undefined,
340
382
  // thus the request shouldn't fire as is in custom cache
341
- let query;
383
+ var query;
342
384
  if ('function' === typeof queryCallback) {
343
385
  query = queryCallback(request); // obj or obj[]
344
386
  }
@@ -377,9 +419,9 @@
377
419
  // The problem with creating cache keys with a stringified object is the order of keys matters and it's possible for the same query to be stringified differently.
378
420
  // Here we ensure the key order will be identical between two of the same requests. https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key
379
421
  // literally impossible for query to be undefined or null here but the editor is too busy licking windows to understand that
380
- let querySerialized = sortAndSerializeQueryObject(tables, query !== null && query !== void 0 ? query : {});
381
- let cacheResult = apiRequestCache.find(cache => cache.requestArgumentsSerialized === querySerialized);
382
- let cachingConfirmed = false;
422
+ var querySerialized = sortAndSerializeQueryObject(tables, query !== null && query !== void 0 ? query : {});
423
+ var cacheResult = apiRequestCache.find(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
424
+ var cachingConfirmed = false;
383
425
  // determine if we need to paginate.
384
426
  if (requestMethod === C6.GET) {
385
427
  if (undefined === (query === null || query === void 0 ? void 0 : query[C6.PAGINATION])) {
@@ -395,7 +437,7 @@
395
437
  // just find the next, non-fetched, page and return a function to request it
396
438
  if (undefined !== cacheResult) {
397
439
  do {
398
- const cacheCheck = checkCache(cacheResult, requestMethod, tableName, request);
440
+ var cacheCheck = checkCache(cacheResult, requestMethod, tableName, request);
399
441
  if (false !== cacheCheck) {
400
442
  return cacheCheck;
401
443
  }
@@ -403,10 +445,10 @@
403
445
  ++query[C6.PAGINATION][C6.PAGE];
404
446
  // this json stringify is to capture the new page number
405
447
  querySerialized = sortAndSerializeQueryObject(tables, query !== null && query !== void 0 ? query : {});
406
- cacheResult = apiRequestCache.find(cache => cache.requestArgumentsSerialized === querySerialized);
448
+ cacheResult = apiRequestCache.find(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
407
449
  } while (undefined !== cacheResult);
408
450
  if (request.debug && isLocal) {
409
- reactToastify.toast.warning("DEVS: Request in cache. (" + apiRequestCache.findIndex(cache => cache.requestArgumentsSerialized === querySerialized) + "). Returning function to request page (" + query[C6.PAGINATION][C6.PAGE] + ")", toastOptionsDevs);
451
+ reactToastify.toast.warning("DEVS: Request in cache. (" + apiRequestCache.findIndex(function (cache) { return cache.requestArgumentsSerialized === querySerialized; }) + "). Returning function to request page (" + query[C6.PAGINATION][C6.PAGE] + ")", toastOptionsDevs);
410
452
  }
411
453
  // @ts-ignore - this is an incorrect warning on TS, it's well typed
412
454
  return apiRequest;
@@ -424,7 +466,7 @@
424
466
  }
425
467
  else if (request.cacheResults) { // if we are not getting, we are updating, deleting, or inserting
426
468
  if (cacheResult) {
427
- const cacheCheck = checkCache(cacheResult, requestMethod, tableName, request);
469
+ var cacheCheck = checkCache(cacheResult, requestMethod, tableName, request);
428
470
  if (false !== cacheCheck) {
429
471
  return cacheCheck;
430
472
  }
@@ -432,16 +474,16 @@
432
474
  cachingConfirmed = true;
433
475
  // push to cache so we do not repeat the request
434
476
  }
435
- let addBackPK;
436
- let apiResponse;
437
- let returnGetNextPageFunction = false;
438
- let restRequestUri = restURL + operatingTable + '/';
439
- const needsConditionOrPrimaryCheck = (PUT === requestMethod || DELETE === requestMethod)
477
+ var addBackPK;
478
+ var apiResponse;
479
+ var returnGetNextPageFunction = false;
480
+ var restRequestUri = restURL + operatingTable + '/';
481
+ var needsConditionOrPrimaryCheck = (PUT === requestMethod || DELETE === requestMethod)
440
482
  && false === skipPrimaryCheck;
441
- const TABLES = C6.TABLES;
483
+ var TABLES = C6.TABLES;
442
484
  // todo - aggregate primary key check with condition check
443
485
  // check if PK exists in query, clone so pop does not affect the real data
444
- const primaryKey = (_f = (_e = (_d = structuredClone((_c = TABLES[operatingTable]) === null || _c === void 0 ? void 0 : _c.PRIMARY)) === null || _d === void 0 ? void 0 : _d.pop()) === null || _e === void 0 ? void 0 : _e.split('.')) === null || _f === void 0 ? void 0 : _f.pop();
486
+ var primaryKey = (_f = (_e = (_d = structuredClone((_c = TABLES[operatingTable]) === null || _c === void 0 ? void 0 : _c.PRIMARY)) === null || _d === void 0 ? void 0 : _d.pop()) === null || _e === void 0 ? void 0 : _e.split('.')) === null || _f === void 0 ? void 0 : _f.pop();
445
487
  if (needsConditionOrPrimaryCheck) {
446
488
  if (undefined === primaryKey) {
447
489
  if (null === query
@@ -476,10 +518,10 @@
476
518
  && undefined !== primaryKey
477
519
  && primaryKey in query) {
478
520
  restRequestUri += query[primaryKey] + '/';
479
- const removedPkValue = query[primaryKey];
480
- addBackPK = () => {
521
+ var removedPkValue_1 = query[primaryKey];
522
+ addBackPK = function () {
481
523
  query !== null && query !== void 0 ? query : (query = {});
482
- query[primaryKey] = removedPkValue;
524
+ query[primaryKey] = removedPkValue_1;
483
525
  };
484
526
  delete query[primaryKey];
485
527
  }
@@ -490,7 +532,7 @@
490
532
  console.log('%c Remember undefined indicated the request has not fired, null indicates the request is firing, an empty array would signal no data was returned for the sql stmt.', 'color: #A020F0');
491
533
  console.trace();
492
534
  console.groupEnd();
493
- const axiosActiveRequest = axios[requestMethod.toLowerCase()](restRequestUri, (() => {
535
+ var axiosActiveRequest = axios[requestMethod.toLowerCase()](restRequestUri, (function () {
494
536
  // we had removed the value from the request to add to the URI.
495
537
  addBackPK === null || addBackPK === void 0 ? void 0 : addBackPK(); // adding back so post-processing methods work
496
538
  if (requestMethod === GET) {
@@ -500,16 +542,18 @@
500
542
  }
501
543
  else if (requestMethod === POST) {
502
544
  if (undefined !== (request === null || request === void 0 ? void 0 : request.dataInsertMultipleRows)) {
503
- return request.dataInsertMultipleRows.map(data => convertForRequestBody(data, fullTableList, (message) => reactToastify.toast.error(message, toastOptions)));
545
+ return request.dataInsertMultipleRows.map(function (data) {
546
+ return convertForRequestBody(data, fullTableList, function (message) { return reactToastify.toast.error(message, toastOptions); });
547
+ });
504
548
  }
505
- return convertForRequestBody(query, fullTableList, (message) => reactToastify.toast.error(message, toastOptions));
549
+ return convertForRequestBody(query, fullTableList, function (message) { return reactToastify.toast.error(message, toastOptions); });
506
550
  }
507
551
  else if (requestMethod === PUT) {
508
- return convertForRequestBody(query, fullTableList, (message) => reactToastify.toast.error(message, toastOptions));
552
+ return convertForRequestBody(query, fullTableList, function (message) { return reactToastify.toast.error(message, toastOptions); });
509
553
  }
510
554
  else if (requestMethod === DELETE) {
511
555
  return {
512
- data: convertForRequestBody(query, fullTableList, (message) => reactToastify.toast.error(message, toastOptions))
556
+ data: convertForRequestBody(query, fullTableList, function (message) { return reactToastify.toast.error(message, toastOptions); })
513
557
  };
514
558
  }
515
559
  else {
@@ -524,7 +568,7 @@
524
568
  });
525
569
  }
526
570
  // https://rapidapi.com/guides/axios-async-await
527
- return axiosActiveRequest.then(response => {
571
+ return axiosActiveRequest.then(function (response) {
528
572
  var _a, _b, _c, _d, _e, _f, _g, _h;
529
573
  if (typeof response.data === 'string') {
530
574
  if (isTest) {
@@ -537,7 +581,7 @@
537
581
  if (false !== apiResponse) {
538
582
  responseCallback(response, request, apiResponse);
539
583
  if (C6.GET === requestMethod) {
540
- const responseData = response.data;
584
+ var responseData = response.data;
541
585
  // @ts-ignore
542
586
  returnGetNextPageFunction = 1 !== ((_b = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _b === void 0 ? void 0 : _b[C6.LIMIT]) &&
543
587
  ((_c = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _c === void 0 ? void 0 : _c[C6.LIMIT]) === responseData.rest.length;
@@ -558,7 +602,7 @@
558
602
  }
559
603
  }
560
604
  if (cachingConfirmed) {
561
- const cacheIndex = apiRequestCache.findIndex(cache => cache.requestArgumentsSerialized === querySerialized);
605
+ var cacheIndex = apiRequestCache.findIndex(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
562
606
  apiRequestCache[cacheIndex].final = false === returnGetNextPageFunction;
563
607
  // only cache get method requests
564
608
  apiRequestCache[cacheIndex].response = response;