@sdflc/backend-helpers 1.0.111 → 1.0.112

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/build/BaseCore.js CHANGED
@@ -11,6 +11,7 @@ const lodash_1 = require("lodash");
11
11
  const commonValidators_1 = require("./commonValidators");
12
12
  const constants_1 = require("./constants");
13
13
  const userToken_1 = require("./userToken");
14
+ const utils_2 = require("./utils");
14
15
  const defaultValidate = ( /*args: any, opt?: BaseCoreActionsInterface*/) => {
15
16
  return [true, {}];
16
17
  };
@@ -29,13 +30,17 @@ const defaultValidators = {
29
30
  remove: commonValidators_1.validateRemove,
30
31
  removeMany: commonValidators_1.validateRemoveMany,
31
32
  };
33
+ const localLogger = new utils_1.Logger({ level: utils_1.LoggerLevels.ERROR });
32
34
  class BaseCore {
33
35
  measureTool = new utils_1.MeasureTool();
34
36
  config;
35
37
  logger;
36
38
  constructor(props) {
37
39
  this.config = props || {};
38
- this.logger = this.config.logger instanceof utils_1.Logger ? this.config.logger : new utils_1.Logger({ level: utils_1.LoggerLevels.ERROR });
40
+ this.logger = localLogger;
41
+ if ((0, utils_2.isLogger)(this.config.logger)) {
42
+ this.logger = this.config.logger;
43
+ }
39
44
  this.config.doAuthOnAction = {
40
45
  list: props?.doAuthOnAction?.list != null ? props?.doAuthOnAction?.list : props?.doAuth,
41
46
  get: props?.doAuthOnAction?.get != null ? props?.doAuthOnAction?.get : props?.doAuth,
@@ -94,7 +99,7 @@ class BaseCore {
94
99
  }
95
100
  validationFailure(ex) {
96
101
  const result = new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED });
97
- if (ex instanceof api_helpers_1.OpResult) {
102
+ if ((0, utils_2.isOpResult)(ex)) {
98
103
  return result.toJS();
99
104
  }
100
105
  Object.keys(ex?.errors || {}).forEach((key) => {
@@ -291,7 +296,7 @@ class BaseCore {
291
296
  accessRights: [utils_1.ACCESS_RIGHTS.LIST],
292
297
  action: async (args, opt) => {
293
298
  const object = await this.beforeList(args, opt);
294
- if (object instanceof api_helpers_1.OpResult) {
299
+ if ((0, utils_2.isOpResult)(object)) {
295
300
  return object;
296
301
  }
297
302
  const items = await this.getContext().gateways[this.config.gatewayName]?.list(object);
@@ -312,7 +317,7 @@ class BaseCore {
312
317
  accessRights: [utils_1.ACCESS_RIGHTS.GET],
313
318
  action: async (args, opt) => {
314
319
  const args2use = await this.beforeGet(args, opt);
315
- if (args2use instanceof api_helpers_1.OpResult) {
320
+ if ((0, utils_2.isOpResult)(args2use)) {
316
321
  return args2use;
317
322
  }
318
323
  if (args2use?.id == null) {
@@ -338,7 +343,7 @@ class BaseCore {
338
343
  accessRights: [utils_1.ACCESS_RIGHTS.GET],
339
344
  action: async (args, opt) => {
340
345
  const args2use = await this.beforeGetMany(args, opt);
341
- if (args2use instanceof api_helpers_1.OpResult) {
346
+ if ((0, utils_2.isOpResult)(args2use)) {
342
347
  return args2use;
343
348
  }
344
349
  if (!Array.isArray(args2use?.ids)) {
@@ -375,7 +380,7 @@ class BaseCore {
375
380
  });
376
381
  }
377
382
  const object = await this.beforeCreate(params, opt);
378
- if (object instanceof api_helpers_1.OpResult) {
383
+ if ((0, utils_2.isOpResult)(object)) {
379
384
  return object;
380
385
  }
381
386
  const items = await this.getContext().gateways[this.config.gatewayName]?.create(object);
@@ -396,7 +401,7 @@ class BaseCore {
396
401
  action: async (args, opt) => {
397
402
  const { params } = args || {};
398
403
  const objects = await this.beforeCreateMany(params, opt);
399
- if (objects instanceof api_helpers_1.OpResult) {
404
+ if ((0, utils_2.isOpResult)(objects)) {
400
405
  return objects;
401
406
  }
402
407
  const items = await this.getContext().gateways[this.config.gatewayName]?.create(objects);
@@ -417,7 +422,7 @@ class BaseCore {
417
422
  action: async (args, opt) => {
418
423
  const { where, params } = args || {};
419
424
  const object = await this.beforeUpdate(params, opt);
420
- if (object instanceof api_helpers_1.OpResult) {
425
+ if ((0, utils_2.isOpResult)(object)) {
421
426
  return object;
422
427
  }
423
428
  const items = await this.getContext().gateways[this.config.gatewayName]?.update(where, object);
@@ -438,7 +443,7 @@ class BaseCore {
438
443
  action: async (args, opt) => {
439
444
  const { params } = args || {};
440
445
  const object = await this.beforeUpdateMany(params, opt);
441
- if (object instanceof api_helpers_1.OpResult) {
446
+ if ((0, utils_2.isOpResult)(object)) {
442
447
  return object;
443
448
  }
444
449
  const items = await this.getContext().gateways[this.config.gatewayName]?.updateMany(object);
@@ -459,7 +464,7 @@ class BaseCore {
459
464
  action: async (args, opt /* { setStep, validatedItems, sameRequest } */) => {
460
465
  const { params } = args || {};
461
466
  const object = await this.beforeSet(params, opt);
462
- if (object instanceof api_helpers_1.OpResult) {
467
+ if ((0, utils_2.isOpResult)(object)) {
463
468
  return object;
464
469
  }
465
470
  const items = await this.getContext().gateways[this.config.gatewayName]?.set(object);
@@ -480,7 +485,7 @@ class BaseCore {
480
485
  action: async (args, opt) => {
481
486
  const { where } = args || {};
482
487
  const object = await this.beforeRemove(where, opt);
483
- if (object instanceof api_helpers_1.OpResult) {
488
+ if ((0, utils_2.isOpResult)(object)) {
484
489
  return object;
485
490
  }
486
491
  const items = await this.getContext().gateways[this.config.gatewayName]?.remove(object);
@@ -501,7 +506,7 @@ class BaseCore {
501
506
  action: async (args, opt) => {
502
507
  const { where } = args || {};
503
508
  const object = await this.beforeRemoveMany(where, opt);
504
- if (object instanceof api_helpers_1.OpResult) {
509
+ if ((0, utils_2.isOpResult)(object)) {
505
510
  return object;
506
511
  }
507
512
  const items = await this.getContext().gateways[this.config.gatewayName]?.remove(object);
@@ -685,6 +690,7 @@ class BaseCore {
685
690
  else if (space?.apiKey && space?.apiKey !== context.apiKey) {
686
691
  return [false, 'API Key is invalid'];
687
692
  }
693
+ return [false, 'No authentication provided'];
688
694
  }
689
695
  return [true, ''];
690
696
  }
@@ -7,6 +7,7 @@ exports.BaseController = void 0;
7
7
  const check_types_1 = __importDefault(require("check-types"));
8
8
  const api_helpers_1 = require("@sdflc/api-helpers");
9
9
  const utils_1 = require("@sdflc/utils");
10
+ const utils_2 = require("./utils");
10
11
  /**
11
12
  * Base class for REST API controllers.
12
13
  */
@@ -38,7 +39,7 @@ class BaseController {
38
39
  return async (req, res, next) => {
39
40
  try {
40
41
  const result = await method(req, res, next);
41
- if (result instanceof api_helpers_1.OpResult) {
42
+ if ((0, utils_2.isOpResult)(result)) {
42
43
  res.status(result.getHttpStatus()).json(result.toJS());
43
44
  }
44
45
  else if (result?.redirectUrl) {
@@ -5,6 +5,7 @@ import { castArray } from 'lodash';
5
5
  import { validateGet, validateGetMany, validateRemove, validateRemoveMany } from './commonValidators';
6
6
  import { ACTIONS, DEFAULT_ORDER_NO, DEFAULT_ORDER_NO_STEP, FIELDS, HEADERS, ID_TYPES, ORDER_BYS } from './constants';
7
7
  import { getDefaultJwtAlgorithm, getDefaultJwtIssuer, verifyUserToken } from './userToken';
8
+ import { isOpResult, isLogger } from './utils';
8
9
  const defaultValidate = ( /*args: any, opt?: BaseCoreActionsInterface*/) => {
9
10
  return [true, {}];
10
11
  };
@@ -23,13 +24,17 @@ const defaultValidators = {
23
24
  remove: validateRemove,
24
25
  removeMany: validateRemoveMany,
25
26
  };
27
+ const localLogger = new Logger({ level: LoggerLevels.ERROR });
26
28
  class BaseCore {
27
29
  measureTool = new MeasureTool();
28
30
  config;
29
31
  logger;
30
32
  constructor(props) {
31
33
  this.config = props || {};
32
- this.logger = this.config.logger instanceof Logger ? this.config.logger : new Logger({ level: LoggerLevels.ERROR });
34
+ this.logger = localLogger;
35
+ if (isLogger(this.config.logger)) {
36
+ this.logger = this.config.logger;
37
+ }
33
38
  this.config.doAuthOnAction = {
34
39
  list: props?.doAuthOnAction?.list != null ? props?.doAuthOnAction?.list : props?.doAuth,
35
40
  get: props?.doAuthOnAction?.get != null ? props?.doAuthOnAction?.get : props?.doAuth,
@@ -88,7 +93,7 @@ class BaseCore {
88
93
  }
89
94
  validationFailure(ex) {
90
95
  const result = new OpResult({ code: OP_RESULT_CODES.VALIDATION_FAILED });
91
- if (ex instanceof OpResult) {
96
+ if (isOpResult(ex)) {
92
97
  return result.toJS();
93
98
  }
94
99
  Object.keys(ex?.errors || {}).forEach((key) => {
@@ -285,7 +290,7 @@ class BaseCore {
285
290
  accessRights: [ACCESS_RIGHTS.LIST],
286
291
  action: async (args, opt) => {
287
292
  const object = await this.beforeList(args, opt);
288
- if (object instanceof OpResult) {
293
+ if (isOpResult(object)) {
289
294
  return object;
290
295
  }
291
296
  const items = await this.getContext().gateways[this.config.gatewayName]?.list(object);
@@ -306,7 +311,7 @@ class BaseCore {
306
311
  accessRights: [ACCESS_RIGHTS.GET],
307
312
  action: async (args, opt) => {
308
313
  const args2use = await this.beforeGet(args, opt);
309
- if (args2use instanceof OpResult) {
314
+ if (isOpResult(args2use)) {
310
315
  return args2use;
311
316
  }
312
317
  if (args2use?.id == null) {
@@ -332,7 +337,7 @@ class BaseCore {
332
337
  accessRights: [ACCESS_RIGHTS.GET],
333
338
  action: async (args, opt) => {
334
339
  const args2use = await this.beforeGetMany(args, opt);
335
- if (args2use instanceof OpResult) {
340
+ if (isOpResult(args2use)) {
336
341
  return args2use;
337
342
  }
338
343
  if (!Array.isArray(args2use?.ids)) {
@@ -369,7 +374,7 @@ class BaseCore {
369
374
  });
370
375
  }
371
376
  const object = await this.beforeCreate(params, opt);
372
- if (object instanceof OpResult) {
377
+ if (isOpResult(object)) {
373
378
  return object;
374
379
  }
375
380
  const items = await this.getContext().gateways[this.config.gatewayName]?.create(object);
@@ -390,7 +395,7 @@ class BaseCore {
390
395
  action: async (args, opt) => {
391
396
  const { params } = args || {};
392
397
  const objects = await this.beforeCreateMany(params, opt);
393
- if (objects instanceof OpResult) {
398
+ if (isOpResult(objects)) {
394
399
  return objects;
395
400
  }
396
401
  const items = await this.getContext().gateways[this.config.gatewayName]?.create(objects);
@@ -411,7 +416,7 @@ class BaseCore {
411
416
  action: async (args, opt) => {
412
417
  const { where, params } = args || {};
413
418
  const object = await this.beforeUpdate(params, opt);
414
- if (object instanceof OpResult) {
419
+ if (isOpResult(object)) {
415
420
  return object;
416
421
  }
417
422
  const items = await this.getContext().gateways[this.config.gatewayName]?.update(where, object);
@@ -432,7 +437,7 @@ class BaseCore {
432
437
  action: async (args, opt) => {
433
438
  const { params } = args || {};
434
439
  const object = await this.beforeUpdateMany(params, opt);
435
- if (object instanceof OpResult) {
440
+ if (isOpResult(object)) {
436
441
  return object;
437
442
  }
438
443
  const items = await this.getContext().gateways[this.config.gatewayName]?.updateMany(object);
@@ -453,7 +458,7 @@ class BaseCore {
453
458
  action: async (args, opt /* { setStep, validatedItems, sameRequest } */) => {
454
459
  const { params } = args || {};
455
460
  const object = await this.beforeSet(params, opt);
456
- if (object instanceof OpResult) {
461
+ if (isOpResult(object)) {
457
462
  return object;
458
463
  }
459
464
  const items = await this.getContext().gateways[this.config.gatewayName]?.set(object);
@@ -474,7 +479,7 @@ class BaseCore {
474
479
  action: async (args, opt) => {
475
480
  const { where } = args || {};
476
481
  const object = await this.beforeRemove(where, opt);
477
- if (object instanceof OpResult) {
482
+ if (isOpResult(object)) {
478
483
  return object;
479
484
  }
480
485
  const items = await this.getContext().gateways[this.config.gatewayName]?.remove(object);
@@ -495,7 +500,7 @@ class BaseCore {
495
500
  action: async (args, opt) => {
496
501
  const { where } = args || {};
497
502
  const object = await this.beforeRemoveMany(where, opt);
498
- if (object instanceof OpResult) {
503
+ if (isOpResult(object)) {
499
504
  return object;
500
505
  }
501
506
  const items = await this.getContext().gateways[this.config.gatewayName]?.remove(object);
@@ -679,6 +684,7 @@ class BaseCore {
679
684
  else if (space?.apiKey && space?.apiKey !== context.apiKey) {
680
685
  return [false, 'API Key is invalid'];
681
686
  }
687
+ return [false, 'No authentication provided'];
682
688
  }
683
689
  return [true, ''];
684
690
  }
@@ -1,6 +1,7 @@
1
1
  import check from 'check-types';
2
2
  import { OpResult } from '@sdflc/api-helpers';
3
3
  import { Logger, LoggerLevels } from '@sdflc/utils';
4
+ import { isOpResult } from './utils';
4
5
  /**
5
6
  * Base class for REST API controllers.
6
7
  */
@@ -32,7 +33,7 @@ class BaseController {
32
33
  return async (req, res, next) => {
33
34
  try {
34
35
  const result = await method(req, res, next);
35
- if (result instanceof OpResult) {
36
+ if (isOpResult(result)) {
36
37
  res.status(result.getHttpStatus()).json(result.toJS());
37
38
  }
38
39
  else if (result?.redirectUrl) {
@@ -1,2 +1,4 @@
1
1
  export declare const setFunction: (func: any, defaultFunc: () => any) => any;
2
2
  export declare const setFunctions: (obj: any, defaultFuncs?: any) => any;
3
+ export declare const isOpResult: (result: any) => any;
4
+ export declare const isLogger: (obj: any) => boolean;
@@ -11,3 +11,15 @@ export const setFunctions = (obj, defaultFuncs = {}) => {
11
11
  return acc;
12
12
  }, {});
13
13
  };
14
+ export const isOpResult = (result) => {
15
+ return result &&
16
+ typeof result.code === 'number' &&
17
+ typeof result.didFail === 'function';
18
+ };
19
+ export const isLogger = (obj) => {
20
+ return obj &&
21
+ typeof obj.warn === 'function' &&
22
+ typeof obj.error === 'function' &&
23
+ typeof obj.debug === 'function' &&
24
+ typeof obj.log === 'function';
25
+ };
package/build/utils.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export declare const setFunction: (func: any, defaultFunc: () => any) => any;
2
2
  export declare const setFunctions: (obj: any, defaultFuncs?: any) => any;
3
+ export declare const isOpResult: (result: any) => any;
4
+ export declare const isLogger: (obj: any) => boolean;
package/build/utils.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.setFunctions = exports.setFunction = void 0;
6
+ exports.isLogger = exports.isOpResult = exports.setFunctions = exports.setFunction = void 0;
7
7
  const check_types_1 = __importDefault(require("check-types"));
8
8
  const setFunction = (func, defaultFunc) => {
9
9
  return check_types_1.default.function(func) ? func : defaultFunc;
@@ -19,3 +19,17 @@ const setFunctions = (obj, defaultFuncs = {}) => {
19
19
  }, {});
20
20
  };
21
21
  exports.setFunctions = setFunctions;
22
+ const isOpResult = (result) => {
23
+ return result &&
24
+ typeof result.code === 'number' &&
25
+ typeof result.didFail === 'function';
26
+ };
27
+ exports.isOpResult = isOpResult;
28
+ const isLogger = (obj) => {
29
+ return obj &&
30
+ typeof obj.warn === 'function' &&
31
+ typeof obj.error === 'function' &&
32
+ typeof obj.debug === 'function' &&
33
+ typeof obj.log === 'function';
34
+ };
35
+ exports.isLogger = isLogger;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.111",
3
+ "version": "1.0.112",
4
4
  "description": "Set of helper functions and classes to simplify building backend apps within @sdflc ecosystem",
5
5
  "type": "commonjs",
6
6
  "exports": {
@@ -58,7 +58,7 @@
58
58
  },
59
59
  "dependencies": {
60
60
  "@sdflc/api-helpers": "^1.1.13",
61
- "@sdflc/utils": "^1.0.24",
61
+ "@sdflc/utils": "^1.0.25",
62
62
  "bcrypt": "^6.0.0",
63
63
  "bluebird": "^3.7.2",
64
64
  "check-types": "^11.2.3",
@@ -71,7 +71,7 @@
71
71
  "jsonwebtoken": "^9.0.3",
72
72
  "knex": "^3.1.0",
73
73
  "lodash": "^4.17.21",
74
- "pg": "^8.16.3",
74
+ "pg": "^8.17.1",
75
75
  "redis": "^5.10.0",
76
76
  "uuid": "^13.0.0"
77
77
  }