@sdflc/backend-helpers 1.0.27 → 1.0.28

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/README.md CHANGED
@@ -24,7 +24,7 @@ Here are the props to be passed to the BaseCore constructor:
24
24
  - doAuth: boolean - call authorize method or not an incoming request
25
25
  - doingWhat?: BaseCoreDoingWhatInterface - an object with text descriptions of each action, used to generate generic error message sent to user on an exception
26
26
  - validators?: BaseCoreValidatorsInterface - an object with validator functions to be used to validate incoming data
27
- - gwSpace?: BaseGateway - gateway to find a space, i.e. configration of the app. Each app can have several configurations to be used as one instance for different applications
27
+ - spaceGw?: BaseGateway - gateway to find a space, i.e. configration of the app. Each app can have several configurations to be used as one instance for different applications
28
28
  - interserviceApiKey?: string - API key to be used to authenticate incoming request from another microservice, optional
29
29
  - jwtSecret?: string - secret to be used to validate Authorize Bearer token on incoming request, optional
30
30
  - verifytokenArgs?: VerifyUserTokenArgs - optional parameters for JWT token validation, like algorithm, etc
package/build/BaseCore.js CHANGED
@@ -734,17 +734,31 @@ var BaseCore = /** @class */ (function () {
734
734
  });
735
735
  };
736
736
  BaseCore.prototype.authenticate = function (params, opt) {
737
- var _a;
738
737
  return __awaiter(this, void 0, void 0, function () {
739
- var context, authByToken, _b, jwtSecret, verifyTokenArgs, verifyResult, token, space;
740
- return __generator(this, function (_c) {
741
- switch (_c.label) {
738
+ var context, space, authByToken, _a, jwtSecret, verifyTokenArgs, verifyResult, token;
739
+ return __generator(this, function (_b) {
740
+ switch (_b.label) {
742
741
  case 0:
743
742
  context = this.config.context;
743
+ space = null;
744
+ if (!this.config.spaceGw) return [3 /*break*/, 2];
745
+ if (!context.spaceId) {
746
+ return [2 /*return*/, [false, 'Space not specified']];
747
+ }
748
+ return [4 /*yield*/, this.config.spaceGw.get(context.spaceId || '')];
749
+ case 1:
750
+ // Lookup space in case we have gateway
751
+ space = _b.sent();
752
+ if (!space) {
753
+ return [2 /*return*/, [false, 'Space not found']];
754
+ }
755
+ context.space = space;
756
+ _b.label = 2;
757
+ case 2:
744
758
  authByToken = false;
745
759
  return [4 /*yield*/, this.getJwtTokenSecretAndSettings(params)];
746
- case 1:
747
- _b = _c.sent(), jwtSecret = _b.jwtSecret, verifyTokenArgs = _b.verifyTokenArgs;
760
+ case 3:
761
+ _a = _b.sent(), jwtSecret = _a.jwtSecret, verifyTokenArgs = _a.verifyTokenArgs;
748
762
  if (context.authorization && jwtSecret) {
749
763
  verifyResult = (0, userToken_1.verifyUserToken)(context.authorization, jwtSecret, verifyTokenArgs);
750
764
  if (verifyResult.didFail()) {
@@ -757,21 +771,6 @@ var BaseCore = /** @class */ (function () {
757
771
  context.spaceId = token.spaceId || context.spaceId;
758
772
  authByToken = true;
759
773
  }
760
- space = null;
761
- if (!this.config.gwSpace) return [3 /*break*/, 3];
762
- if (!context.spaceId) {
763
- return [2 /*return*/, [false, 'Space not specified']];
764
- }
765
- return [4 /*yield*/, ((_a = this.config.gwSpace) === null || _a === void 0 ? void 0 : _a.get(context.spaceId || ''))];
766
- case 2:
767
- // Lookup space in case we have gateway
768
- space = _c.sent();
769
- if (!space) {
770
- return [2 /*return*/, [false, 'Space not found']];
771
- }
772
- context.space = space;
773
- _c.label = 3;
774
- case 3:
775
774
  if (!authByToken) {
776
775
  if (this.config.interserviceApiKey && context.apiKey === this.config.interserviceApiKey) {
777
776
  return [2 /*return*/, [true, '']];
@@ -80,22 +80,25 @@ var BaseGateway = /** @class */ (function () {
80
80
  };
81
81
  BaseGateway.prototype.onListFilter = function (query, filterParams) {
82
82
  return __awaiter(this, void 0, void 0, function () {
83
- var _a, hasStatus, hasRemovedAt, hasSpace, activeStatuses, fieldForKeyword;
83
+ var _a, hasStatus, hasRemovedAt, hasSpace, activeStatuses, hasLang, fieldForKeyword;
84
84
  var _b;
85
85
  return __generator(this, function (_c) {
86
- _a = this.config || {}, hasStatus = _a.hasStatus, hasRemovedAt = _a.hasRemovedAt, hasSpace = _a.hasSpace, activeStatuses = _a.activeStatuses, fieldForKeyword = _a.fieldForKeyword;
86
+ _a = this.config || {}, hasStatus = _a.hasStatus, hasRemovedAt = _a.hasRemovedAt, hasSpace = _a.hasSpace, activeStatuses = _a.activeStatuses, hasLang = _a.hasLang, fieldForKeyword = _a.fieldForKeyword;
87
87
  // This function should be overriden
88
88
  if (hasStatus) {
89
- query.whereIn(constants_1.FIELDS.STATUS, Array.isArray(filterParams.status) ? filterParams.status : (0, lodash_1.castArray)(activeStatuses));
89
+ query.whereIn(constants_1.FIELDS.STATUS, Array.isArray(filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.STATUS]) ? filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.STATUS] : (0, lodash_1.castArray)(activeStatuses));
90
90
  }
91
- if (hasRemovedAt === true) {
91
+ if (hasRemovedAt) {
92
92
  query.whereNull(constants_1.FIELDS.REMOVED_AT);
93
93
  }
94
94
  if (hasSpace) {
95
- query.where((_b = {}, _b[constants_1.FIELDS.SPACE_ID] = filterParams === null || filterParams === void 0 ? void 0 : filterParams.spaceId, _b));
95
+ query.where((_b = {}, _b[constants_1.FIELDS.SPACE_ID] = filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.SPACE_ID], _b));
96
+ }
97
+ if (hasLang && (filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.LANG])) {
98
+ query.whereIn(constants_1.FIELDS.LANG, (0, lodash_1.castArray)(filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.LANG]));
96
99
  }
97
- if (filterParams === null || filterParams === void 0 ? void 0 : filterParams.id) {
98
- query.whereIn(constants_1.FIELDS.ID, (0, lodash_1.castArray)(filterParams === null || filterParams === void 0 ? void 0 : filterParams.id));
100
+ if (filterParams === null || filterParams === void 0 ? void 0 : filterParams[this.getIdField()]) {
101
+ query.whereIn(constants_1.FIELDS.ID, (0, lodash_1.castArray)(filterParams === null || filterParams === void 0 ? void 0 : filterParams[this.getIdField()]));
99
102
  }
100
103
  if (fieldForKeyword && filterParams.keyword) {
101
104
  query.whereRaw("".concat(fieldForKeyword, " ILIKE ?"), "%".concat(filterParams.keyword, "%"));
@@ -109,13 +112,20 @@ var BaseGateway = /** @class */ (function () {
109
112
  var _a, hasStatus, hasVersion, idName;
110
113
  return __generator(this, function (_b) {
111
114
  _a = this.config, hasStatus = _a.hasStatus, hasVersion = _a.hasVersion;
112
- idName = this.config.idField || constants_1.ID_FIELD;
113
- query.where(idName, filterParams[idName]);
115
+ idName = this.getIdField();
116
+ if (filterParams[idName]) {
117
+ query.where(idName, filterParams[idName]);
118
+ }
114
119
  if (hasVersion && filterParams[constants_1.FIELDS.VERSION]) {
115
120
  query.where(constants_1.FIELDS.VERSION, filterParams[constants_1.FIELDS.VERSION]);
116
121
  }
117
122
  if (hasStatus) {
118
- query.where(constants_1.FIELDS.STATUS, '!=', utils_1.STATUSES.REMOVED);
123
+ if (Array.isArray(filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.STATUS])) {
124
+ query.whereIn(constants_1.FIELDS.STATUS, filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.STATUS]);
125
+ }
126
+ else {
127
+ query.where(constants_1.FIELDS.STATUS, '!=', utils_1.STATUSES.REMOVED);
128
+ }
119
129
  }
120
130
  return [2 /*return*/];
121
131
  });
@@ -113,11 +113,11 @@ exports.createApiContext = createApiContext;
113
113
  * @returns {object} A context object with all available cores
114
114
  */
115
115
  var createContextDefault = function (args) { return __awaiter(void 0, void 0, void 0, function () {
116
- var _a, knexConfig, isProduction, logger, doAuth, gwSpace, interserviceApiKey, jwtSecret, verifyTokenArgs, dbSchema, cores, gateways, req, extendContext, getCoreContext, getGatewayContext, authorization, spaceId, apiKey, managementApiKey, accountId, roleId, userId, userAgent, referer, ipAddress, acceptLanguage, languages, requestedLang, requestId, db, sharedContext;
116
+ var _a, knexConfig, isProduction, logger, doAuth, spaceGw, interserviceApiKey, jwtSecret, verifyTokenArgs, dbSchema, cores, gateways, req, extendContext, getCoreContext, getGatewayContext, authorization, spaceId, apiKey, managementApiKey, accountId, roleId, userId, userAgent, referer, ipAddress, acceptLanguage, languages, requestedLang, requestId, db, sharedContext;
117
117
  return __generator(this, function (_b) {
118
118
  switch (_b.label) {
119
119
  case 0:
120
- _a = args || {}, knexConfig = _a.knexConfig, isProduction = _a.isProduction, logger = _a.logger, doAuth = _a.doAuth, gwSpace = _a.gwSpace, interserviceApiKey = _a.interserviceApiKey, jwtSecret = _a.jwtSecret, verifyTokenArgs = _a.verifyTokenArgs, dbSchema = _a.dbSchema, cores = _a.cores, gateways = _a.gateways, req = _a.req, extendContext = _a.extendContext, getCoreContext = _a.getCoreContext, getGatewayContext = _a.getGatewayContext;
120
+ _a = args || {}, knexConfig = _a.knexConfig, isProduction = _a.isProduction, logger = _a.logger, doAuth = _a.doAuth, spaceGw = _a.spaceGw, interserviceApiKey = _a.interserviceApiKey, jwtSecret = _a.jwtSecret, verifyTokenArgs = _a.verifyTokenArgs, dbSchema = _a.dbSchema, cores = _a.cores, gateways = _a.gateways, req = _a.req, extendContext = _a.extendContext, getCoreContext = _a.getCoreContext, getGatewayContext = _a.getGatewayContext;
121
121
  authorization = req.headers[constants_1.HEADERS.AUTHORIZATION]
122
122
  ? req.headers[constants_1.HEADERS.AUTHORIZATION].replace('Bearer ', '')
123
123
  : undefined;
@@ -176,7 +176,7 @@ var createContextDefault = function (args) { return __awaiter(void 0, void 0, vo
176
176
  case 1:
177
177
  extendCoreConext = _a.sent();
178
178
  _a.label = 2;
179
- case 2: return [2 /*return*/, __assign(__assign({ db: db, gatewayName: '', isProduction: isProduction, logger: logger, doAuth: doAuth, gwSpace: gwSpace, interserviceApiKey: interserviceApiKey, jwtSecret: jwtSecret, verifyTokenArgs: verifyTokenArgs }, (0, lodash_1.omit)(extendCoreConext, ['context'])), { context: __assign(__assign({}, sharedContext), (extendCoreConext.context || {})) })];
179
+ case 2: return [2 /*return*/, __assign(__assign({ db: db, gatewayName: '', isProduction: isProduction, logger: logger, doAuth: doAuth, spaceGw: spaceGw, interserviceApiKey: interserviceApiKey, jwtSecret: jwtSecret, verifyTokenArgs: verifyTokenArgs }, (0, lodash_1.omit)(extendCoreConext, ['context'])), { context: __assign(__assign({}, sharedContext), (extendCoreConext.context || {})) })];
180
180
  }
181
181
  });
182
182
  }); },
@@ -24,7 +24,7 @@ export interface BaseCorePropsInterface {
24
24
  doAuth: boolean;
25
25
  doingWhat?: BaseCoreDoingWhatInterface;
26
26
  validators?: BaseCoreValidatorsInterface;
27
- gwSpace?: BaseGateway;
27
+ spaceGw?: BaseGateway;
28
28
  interserviceApiKey?: string;
29
29
  jwtSecret?: string;
30
30
  verifyTokenArgs?: VerifyUserTokenArgs;
@@ -16,6 +16,7 @@ export interface BaseGatewayPropsInterface extends CacheInterface {
16
16
  hasRemovedAtStr?: boolean;
17
17
  hasSpace?: boolean;
18
18
  hasVersion?: boolean;
19
+ hasLang?: boolean;
19
20
  defaultSorting?: SortItemInterface[];
20
21
  fieldForKeyword?: string;
21
22
  insertChunkSize?: number;
@@ -5,5 +5,5 @@ export interface CoreActionInterface {
5
5
  validate?(args?: any, gateways?: any, opt?: any): Promise<any[]> | any[];
6
6
  action(args?: any, opt?: any): any;
7
7
  hasTransaction: boolean;
8
- doingWhat: string;
8
+ doingWhat?: string;
9
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.27",
3
+ "version": "1.0.28",
4
4
  "description": "Set of helper functions and classes to simplify building backend apps within @sdflc ecosystem",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",