@sdflc/backend-helpers 1.0.22 → 1.0.25

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.
@@ -3,9 +3,9 @@ import { ApiContextInterface } from './interfaces';
3
3
  * Creates a GraphQL context object with all available cores.
4
4
  * @returns {object} A context object with all available cores
5
5
  */
6
- export declare const createApiContext: (args: ApiContextInterface) => any;
6
+ export declare const createApiContext: (args: ApiContextInterface) => Promise<any>;
7
7
  /**
8
8
  * Creates default a GraphQL context object with all avilable cores.
9
9
  * @returns {object} A context object with all available cores
10
10
  */
11
- export declare const createContextDefault: (args: any) => any;
11
+ export declare const createContextDefault: (args: any) => Promise<any>;
@@ -10,6 +10,42 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
13
49
  Object.defineProperty(exports, "__esModule", { value: true });
14
50
  exports.createContextDefault = exports.createApiContext = void 0;
15
51
  var lodash_1 = require("lodash");
@@ -20,75 +56,96 @@ var database_1 = require("./database");
20
56
  * Creates a GraphQL context object with all available cores.
21
57
  * @returns {object} A context object with all available cores
22
58
  */
23
- var createApiContext = function (args) {
24
- var _a = args || {}, cores = _a.cores, req = _a.req, getCoreContext = _a.getCoreContext, extendContext = _a.extendContext, inRequestId = _a.requestId;
25
- var keys = Object.keys(cores || {});
26
- var requestId = inRequestId || uuid_1.v4();
27
- var coreContext = typeof getCoreContext === 'function' ? getCoreContext({ req: req }) : {};
28
- var context = keys.reduce(function (acc, key) {
29
- acc[lodash_1.camelCase(key)] = new cores[key](__assign(__assign({}, coreContext), { requestId: requestId }));
30
- return acc;
31
- }, {});
32
- context.requestId = requestId;
33
- if (typeof extendContext === 'function') {
34
- extendContext({ context: context, req: req });
35
- }
36
- return context;
37
- };
59
+ var createApiContext = function (args) { return __awaiter(void 0, void 0, void 0, function () {
60
+ var _a, cores, req, getCoreContext, extendContext, inRequestId, keys, requestId, coreContext, _b, context;
61
+ return __generator(this, function (_c) {
62
+ switch (_c.label) {
63
+ case 0:
64
+ _a = args || {}, cores = _a.cores, req = _a.req, getCoreContext = _a.getCoreContext, extendContext = _a.extendContext, inRequestId = _a.requestId;
65
+ keys = Object.keys(cores || {});
66
+ requestId = inRequestId || (0, uuid_1.v4)();
67
+ if (!(typeof getCoreContext === 'function')) return [3 /*break*/, 2];
68
+ return [4 /*yield*/, getCoreContext({ req: req })];
69
+ case 1:
70
+ _b = _c.sent();
71
+ return [3 /*break*/, 3];
72
+ case 2:
73
+ _b = {};
74
+ _c.label = 3;
75
+ case 3:
76
+ coreContext = _b;
77
+ context = keys.reduce(function (acc, key) {
78
+ acc[(0, lodash_1.camelCase)(key)] = new cores[key](__assign(__assign({}, coreContext), { requestId: requestId }));
79
+ return acc;
80
+ }, {});
81
+ context.requestId = requestId;
82
+ if (!(typeof extendContext === 'function')) return [3 /*break*/, 5];
83
+ return [4 /*yield*/, extendContext({ context: context, req: req })];
84
+ case 4:
85
+ _c.sent();
86
+ _c.label = 5;
87
+ case 5: return [2 /*return*/, context];
88
+ }
89
+ });
90
+ }); };
38
91
  exports.createApiContext = createApiContext;
39
92
  /**
40
93
  * Creates default a GraphQL context object with all avilable cores.
41
94
  * @returns {object} A context object with all available cores
42
95
  */
43
- var createContextDefault = function (args) {
44
- var _a = args || {}, knexConfig = _a.knexConfig, dbSchema = _a.dbSchema, cores = _a.cores, req = _a.req;
45
- var authorization = req.headers['authorization'];
46
- var spaceId = req.headers['x-space-id'];
47
- var apiKey = req.headers['x-api-key'];
48
- var managementApiKey = req.headers['x-management-api-key'];
49
- var accountId = req.headers['x-account-id'];
50
- var roleId = req.headers['x-role-id'];
51
- var userId = req.headers['x-user-id'];
52
- var userAgent = req.headers['user-agent'];
53
- var referer = req.headers['referer'];
54
- var ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
55
- var acceptLanguage = req.headers['accept-language'];
56
- var languages = utils_1.extractLanguages(acceptLanguage);
57
- var requestedLang = req.query['lang'];
58
- var requestId = uuid_1.v4();
59
- var db = new database_1.Database({
60
- knexConfig: knexConfig,
61
- dbSchema: dbSchema,
62
- });
63
- return exports.createApiContext({
64
- cores: cores,
65
- req: req,
66
- requestId: requestId,
67
- extendContext: function (_a) {
68
- var context = _a.context /*, req */;
69
- context.db = db;
70
- },
71
- getCoreContext: function ( /*args: any*/) {
72
- // TODO: const { req } = args;
73
- return {
74
- db: db,
75
- context: {
76
- spaceId: spaceId,
77
- apiKey: apiKey,
78
- managementApiKey: managementApiKey,
79
- userAgent: userAgent,
80
- referer: referer,
81
- ipAddress: ipAddress,
82
- acceptLanguage: acceptLanguage,
83
- languages: languages,
84
- requestedLang: requestedLang,
85
- authorization: authorization,
86
- accountId: accountId,
87
- roleId: roleId,
88
- userId: userId,
89
- },
90
- };
91
- },
96
+ var createContextDefault = function (args) { return __awaiter(void 0, void 0, void 0, function () {
97
+ var _a, knexConfig, dbSchema, cores, req, extendContext, getCoreContext, authorization, spaceId, apiKey, managementApiKey, accountId, roleId, userId, userAgent, referer, ipAddress, acceptLanguage, languages, requestedLang, requestId, db;
98
+ return __generator(this, function (_b) {
99
+ switch (_b.label) {
100
+ case 0:
101
+ _a = args || {}, knexConfig = _a.knexConfig, dbSchema = _a.dbSchema, cores = _a.cores, req = _a.req, extendContext = _a.extendContext, getCoreContext = _a.getCoreContext;
102
+ authorization = req.headers['authorization'] ? req.headers['authorization'].replace('Bearer ', '') : undefined;
103
+ spaceId = req.headers['x-space-id'];
104
+ apiKey = req.headers['x-api-key'];
105
+ managementApiKey = req.headers['x-management-api-key'];
106
+ accountId = req.headers['x-account-id'];
107
+ roleId = req.headers['x-role-id'];
108
+ userId = req.headers['x-user-id'];
109
+ userAgent = req.headers['user-agent'];
110
+ referer = req.headers['referer'];
111
+ ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
112
+ acceptLanguage = req.headers['accept-language'];
113
+ languages = (0, utils_1.extractLanguages)(acceptLanguage);
114
+ requestedLang = req.query['lang'];
115
+ requestId = (0, uuid_1.v4)();
116
+ db = new database_1.Database({
117
+ knexConfig: knexConfig,
118
+ dbSchema: dbSchema,
119
+ });
120
+ return [4 /*yield*/, (0, exports.createApiContext)({
121
+ cores: cores,
122
+ req: req,
123
+ requestId: requestId,
124
+ extendContext: function (_a) {
125
+ var context = _a.context, req = _a.req;
126
+ context.db = db;
127
+ if (typeof extendContext === 'function') {
128
+ extendContext({ context: context, req: req });
129
+ }
130
+ },
131
+ getCoreContext: function (args) { return __awaiter(void 0, void 0, void 0, function () {
132
+ var extendCoreConext;
133
+ return __generator(this, function (_a) {
134
+ switch (_a.label) {
135
+ case 0:
136
+ extendCoreConext = {};
137
+ if (!(typeof getCoreContext === 'function')) return [3 /*break*/, 2];
138
+ return [4 /*yield*/, getCoreContext(args)];
139
+ case 1:
140
+ extendCoreConext = _a.sent();
141
+ _a.label = 2;
142
+ case 2: return [2 /*return*/, __assign(__assign({ db: db }, extendCoreConext), { context: __assign({ spaceId: spaceId, apiKey: apiKey, managementApiKey: managementApiKey, userAgent: userAgent, referer: referer, ipAddress: ipAddress, acceptLanguage: acceptLanguage, languages: languages, requestedLang: requestedLang, authorization: authorization, accountId: accountId, roleId: roleId, userId: userId }, (extendCoreConext.context || {})) })];
143
+ }
144
+ });
145
+ }); },
146
+ })];
147
+ case 1: return [2 /*return*/, _b.sent()];
148
+ }
92
149
  });
93
- };
150
+ }); };
94
151
  exports.createContextDefault = createContextDefault;
@@ -1,16 +1,20 @@
1
1
  import { Logger } from '@sdflc/utils';
2
2
  import { BaseCoreInterface, BaseCoreDoingWhatInterface, BaseCoreActionsInterface, CoreActionInterface, BaseCoreValidatorsInterface } from './interfaces';
3
- import { BaseGateway } from './baseGateway';
3
+ import { VerifyUserTokenArgs } from './interfaces/VerifyUserTokenArgs';
4
4
  import { Database } from './database';
5
5
  declare class BaseCore {
6
6
  protected props: BaseCoreInterface;
7
7
  protected db: Database;
8
8
  protected context: any;
9
- protected gateway?: BaseGateway;
10
9
  protected doingWhat?: BaseCoreDoingWhatInterface;
11
10
  protected logger: Logger;
12
11
  protected name?: string;
12
+ protected gateway?: any;
13
13
  protected gateways?: any;
14
+ protected gwSpace?: any;
15
+ protected interserviceApiKey?: string;
16
+ protected jwtSecret: string;
17
+ protected verifytokenArgs?: VerifyUserTokenArgs;
14
18
  protected requestId: string;
15
19
  protected validators: BaseCoreValidatorsInterface;
16
20
  protected doAuth: boolean;
@@ -52,6 +56,6 @@ declare class BaseCore {
52
56
  beforeRemoveMany(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
53
57
  afterRemoveMany(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
54
58
  onException(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
55
- authenticate(params: any, opt?: BaseCoreActionsInterface): Promise<any>;
59
+ authenticate(params: any, opt?: BaseCoreActionsInterface): Promise<any[]>;
56
60
  }
57
61
  export { BaseCore };
package/build/baseCore.js CHANGED
@@ -46,16 +46,13 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
46
46
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
47
  }
48
48
  };
49
- var __importDefault = (this && this.__importDefault) || function (mod) {
50
- return (mod && mod.__esModule) ? mod : { "default": mod };
51
- };
52
49
  Object.defineProperty(exports, "__esModule", { value: true });
53
50
  exports.BaseCore = void 0;
54
51
  var api_helpers_1 = require("@sdflc/api-helpers");
55
52
  var utils_1 = require("@sdflc/utils");
53
+ var userToken_1 = require("./userToken");
54
+ var baseGateway_1 = require("./baseGateway");
56
55
  var utils_2 = require("./utils");
57
- var constants_1 = __importDefault(require("./constants"));
58
- var ACCESS_RIGHTS = constants_1.default.ACCESS_RIGHTS;
59
56
  var defaultAfterAndBefore = function (params, opt) {
60
57
  return params;
61
58
  };
@@ -74,21 +71,32 @@ var defaultValidators = {
74
71
  remove: defaultValidate,
75
72
  removeMany: defaultValidate,
76
73
  };
77
- var defaultAuth = function (params) {
78
- return [true, ''];
79
- };
80
74
  var BaseCore = /** @class */ (function () {
81
75
  function BaseCore(props) {
76
+ var _this = this;
82
77
  this.validators = defaultValidators;
83
78
  this.props = props || {};
84
79
  this.db = props.db;
85
80
  this.name = props.name || '';
86
81
  this.context = props.context || {};
87
- this.gateway = props.gateway;
88
82
  this.doingWhat = props.doingWhat;
89
83
  this.logger = props.logger || new utils_1.Logger({ level: props.logLevel });
84
+ this.gateway = props.gateway;
90
85
  this.gateways = __assign({ gateway: this.gateway }, (props.gateways || {}));
91
- this.validators = utils_2.setFunctions((props === null || props === void 0 ? void 0 : props.validators) || {}, this.validators);
86
+ if (this.gateway instanceof baseGateway_1.BaseGateway) {
87
+ this.gateway.setCore(this);
88
+ }
89
+ Object.keys(this.gateways).forEach(function (gatewayName) {
90
+ var gateway = _this.gateways[gatewayName];
91
+ if (gateway instanceof baseGateway_1.BaseGateway) {
92
+ gateway.setCore(_this);
93
+ }
94
+ });
95
+ this.gwSpace = props.gwSpace;
96
+ this.interserviceApiKey = props.interserviceApiKey;
97
+ this.jwtSecret = props.jwtSecret || '';
98
+ this.verifytokenArgs = props.verifytokenArgs;
99
+ this.validators = (0, utils_2.setFunctions)((props === null || props === void 0 ? void 0 : props.validators) || {}, this.validators);
92
100
  this.doAuth = props.doAuth === true;
93
101
  this.requestId = props.requestId || '';
94
102
  }
@@ -137,7 +145,7 @@ var BaseCore = /** @class */ (function () {
137
145
  var _a = this.getDb().isDbError(ex), isDbError = _a[0], dbErrorMsg = _a[1];
138
146
  var isOpResult = typeof ex.getErrorSummary === 'function';
139
147
  var message = isOpResult ? ex.getErrorSummary() : ex.message;
140
- var sysMsg = "An exception has occurred on " + doingWhat + " with message " + message + ":";
148
+ var sysMsg = "An exception has occurred on ".concat(doingWhat, " with message ").concat(message, ":");
141
149
  var msg = '';
142
150
  if (isDbError) {
143
151
  msg = dbErrorMsg;
@@ -147,7 +155,7 @@ var BaseCore = /** @class */ (function () {
147
155
  msg = doingWhat['message'];
148
156
  }
149
157
  else {
150
- msg = "Sorry, something went wrong on " + doingWhat + "." + (isOpResult ? " Reason: " + ex.getErrorSummary() : '');
158
+ msg = isOpResult ? ex.getErrorSummary() : "Sorry, something went wrong on ".concat(doingWhat);
151
159
  }
152
160
  this.logger.error(sysMsg, ex);
153
161
  }
@@ -256,7 +264,7 @@ var BaseCore = /** @class */ (function () {
256
264
  what = doingWhat;
257
265
  if (doingWhat !== step) {
258
266
  what = {
259
- message: step + " failed on " + doingWhat,
267
+ message: "".concat(step, " failed on ").concat(doingWhat),
260
268
  };
261
269
  }
262
270
  return [2 /*return*/, this.exceptionFailure(ex_1, what)];
@@ -273,14 +281,14 @@ var BaseCore = /** @class */ (function () {
273
281
  return [2 /*return*/, this.runAction({
274
282
  args: args,
275
283
  validate: this.validators.list,
276
- accessRights: [ACCESS_RIGHTS.LIST],
284
+ accessRights: [utils_1.ACCESS_RIGHTS.LIST],
277
285
  action: function (args, opt) { return __awaiter(_this, void 0, void 0, function () {
278
286
  var object, items, result;
279
287
  var _a;
280
288
  return __generator(this, function (_b) {
281
289
  switch (_b.label) {
282
290
  case 0:
283
- this.logger.log("Requesting a list for " + this.name + ":", args);
291
+ this.logger.log("Requesting a list for ".concat(this.name, ":"), args);
284
292
  return [4 /*yield*/, this.beforeList(args, opt)];
285
293
  case 1:
286
294
  object = _b.sent();
@@ -309,7 +317,7 @@ var BaseCore = /** @class */ (function () {
309
317
  return [2 /*return*/, this.runAction({
310
318
  args: args,
311
319
  validate: this.validators.get,
312
- accessRights: [ACCESS_RIGHTS.GET],
320
+ accessRights: [utils_1.ACCESS_RIGHTS.GET],
313
321
  action: function (args) { return __awaiter(_this, void 0, void 0, function () {
314
322
  var id, item;
315
323
  var _a;
@@ -317,7 +325,7 @@ var BaseCore = /** @class */ (function () {
317
325
  switch (_b.label) {
318
326
  case 0:
319
327
  id = (args || {}).id;
320
- this.logger.log("Requesting an item for " + this.name + ":", args);
328
+ this.logger.log("Requesting an item for ".concat(this.name, ":"), args);
321
329
  return [4 /*yield*/, ((_a = this.gateway) === null || _a === void 0 ? void 0 : _a.get(id))];
322
330
  case 1:
323
331
  item = _b.sent();
@@ -339,7 +347,7 @@ var BaseCore = /** @class */ (function () {
339
347
  return [2 /*return*/, this.runAction({
340
348
  args: args,
341
349
  validate: this.validators.getMany,
342
- accessRights: [ACCESS_RIGHTS.GET],
350
+ accessRights: [utils_1.ACCESS_RIGHTS.GET],
343
351
  action: function (args) { return __awaiter(_this, void 0, void 0, function () {
344
352
  var ids, items;
345
353
  var _a;
@@ -347,7 +355,7 @@ var BaseCore = /** @class */ (function () {
347
355
  switch (_b.label) {
348
356
  case 0:
349
357
  ids = (args || {}).ids;
350
- this.logger.log("Requesting multiple items for " + this.name + ":", args);
358
+ this.logger.log("Requesting multiple items for ".concat(this.name, ":"), args);
351
359
  return [4 /*yield*/, ((_a = this.gateway) === null || _a === void 0 ? void 0 : _a.getMany(ids))];
352
360
  case 1:
353
361
  items = _b.sent();
@@ -369,7 +377,7 @@ var BaseCore = /** @class */ (function () {
369
377
  return [2 /*return*/, this.runAction({
370
378
  args: args,
371
379
  validate: this.validators.create,
372
- accessRights: [ACCESS_RIGHTS.CREATE],
380
+ accessRights: [utils_1.ACCESS_RIGHTS.CREATE],
373
381
  action: function (args, opt) { return __awaiter(_this, void 0, void 0, function () {
374
382
  var params, object, item, result;
375
383
  var _a;
@@ -377,7 +385,7 @@ var BaseCore = /** @class */ (function () {
377
385
  switch (_b.label) {
378
386
  case 0:
379
387
  params = (args || {}).params;
380
- this.logger.log("Requesting creation of an item for " + this.name + ":", args);
388
+ this.logger.log("Requesting creation of an item for ".concat(this.name, ":"), args);
381
389
  return [4 /*yield*/, this.beforeCreate(params, opt)];
382
390
  case 1:
383
391
  object = _b.sent();
@@ -405,7 +413,7 @@ var BaseCore = /** @class */ (function () {
405
413
  return [2 /*return*/, this.runAction({
406
414
  args: args,
407
415
  validate: this.validators.createMany,
408
- accessRights: [ACCESS_RIGHTS.CREATE],
416
+ accessRights: [utils_1.ACCESS_RIGHTS.CREATE],
409
417
  action: function (args, opt) { return __awaiter(_this, void 0, void 0, function () {
410
418
  var params, objects, items, result;
411
419
  var _a;
@@ -413,7 +421,7 @@ var BaseCore = /** @class */ (function () {
413
421
  switch (_b.label) {
414
422
  case 0:
415
423
  params = (args || {}).params;
416
- this.logger.log("Requesting creation of multiple items for " + this.name + ":", args);
424
+ this.logger.log("Requesting creation of multiple items for ".concat(this.name, ":"), args);
417
425
  return [4 /*yield*/, this.beforeCreateMany(params, opt)];
418
426
  case 1:
419
427
  objects = _b.sent();
@@ -441,7 +449,7 @@ var BaseCore = /** @class */ (function () {
441
449
  return [2 /*return*/, this.runAction({
442
450
  args: args,
443
451
  validate: this.validators.update,
444
- accessRights: [ACCESS_RIGHTS.UPDATE],
452
+ accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
445
453
  action: function (args, opt) { return __awaiter(_this, void 0, void 0, function () {
446
454
  var _a, where, params, object, item, result;
447
455
  var _b;
@@ -449,7 +457,7 @@ var BaseCore = /** @class */ (function () {
449
457
  switch (_c.label) {
450
458
  case 0:
451
459
  _a = args || {}, where = _a.where, params = _a.params;
452
- this.logger.log("Requesting update of an item for " + this.name + ":", args);
460
+ this.logger.log("Requesting update of an item for ".concat(this.name, ":"), args);
453
461
  return [4 /*yield*/, this.beforeUpdate(params, opt)];
454
462
  case 1:
455
463
  object = _c.sent();
@@ -477,7 +485,7 @@ var BaseCore = /** @class */ (function () {
477
485
  return [2 /*return*/, this.runAction({
478
486
  args: args,
479
487
  validate: this.validators.updateMany,
480
- accessRights: [ACCESS_RIGHTS.UPDATE],
488
+ accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
481
489
  action: function (args, opt) { return __awaiter(_this, void 0, void 0, function () {
482
490
  var params, object, items, result;
483
491
  var _a;
@@ -485,7 +493,7 @@ var BaseCore = /** @class */ (function () {
485
493
  switch (_b.label) {
486
494
  case 0:
487
495
  params = (args || {}).params;
488
- this.logger.log("Requesting update of an item for " + this.name + ":", args);
496
+ this.logger.log("Requesting update of an item for ".concat(this.name, ":"), args);
489
497
  return [4 /*yield*/, this.beforeUpdateMany(params, opt)];
490
498
  case 1:
491
499
  object = _b.sent();
@@ -513,7 +521,7 @@ var BaseCore = /** @class */ (function () {
513
521
  return [2 /*return*/, this.runAction({
514
522
  args: args,
515
523
  validate: this.validators.set,
516
- accessRights: [ACCESS_RIGHTS.CREATE, ACCESS_RIGHTS.UPDATE],
524
+ accessRights: [utils_1.ACCESS_RIGHTS.CREATE, utils_1.ACCESS_RIGHTS.UPDATE],
517
525
  action: function (args, opt /* { setStep, validatedItems, sameRequest } */) { return __awaiter(_this, void 0, void 0, function () {
518
526
  var params, object, items, result;
519
527
  var _a;
@@ -521,7 +529,7 @@ var BaseCore = /** @class */ (function () {
521
529
  switch (_b.label) {
522
530
  case 0:
523
531
  params = (args || {}).params;
524
- this.logger.log("Requesting setting of item(s) for " + this.name + ":", args);
532
+ this.logger.log("Requesting setting of item(s) for ".concat(this.name, ":"), args);
525
533
  return [4 /*yield*/, this.beforeSet(params, opt)];
526
534
  case 1:
527
535
  object = _b.sent();
@@ -549,7 +557,7 @@ var BaseCore = /** @class */ (function () {
549
557
  return [2 /*return*/, this.runAction({
550
558
  args: args,
551
559
  validate: this.validators.remove,
552
- accessRights: [ACCESS_RIGHTS.REMOVE],
560
+ accessRights: [utils_1.ACCESS_RIGHTS.REMOVE],
553
561
  action: function (args, opt) { return __awaiter(_this, void 0, void 0, function () {
554
562
  var where, object, items, result;
555
563
  var _a;
@@ -557,7 +565,7 @@ var BaseCore = /** @class */ (function () {
557
565
  switch (_b.label) {
558
566
  case 0:
559
567
  where = (args || {}).where;
560
- this.logger.log("Requesting removal of an item for " + this.name + ":", args);
568
+ this.logger.log("Requesting removal of an item for ".concat(this.name, ":"), args);
561
569
  return [4 /*yield*/, this.beforeRemove(where, opt)];
562
570
  case 1:
563
571
  object = _b.sent();
@@ -585,7 +593,7 @@ var BaseCore = /** @class */ (function () {
585
593
  return [2 /*return*/, this.runAction({
586
594
  args: args,
587
595
  validate: this.validators.removeMany,
588
- accessRights: [ACCESS_RIGHTS.REMOVE],
596
+ accessRights: [utils_1.ACCESS_RIGHTS.REMOVE],
589
597
  action: function (args, opt) { return __awaiter(_this, void 0, void 0, function () {
590
598
  var where, object, items, result;
591
599
  var _a;
@@ -593,7 +601,7 @@ var BaseCore = /** @class */ (function () {
593
601
  switch (_b.label) {
594
602
  case 0:
595
603
  where = (args || {}).where;
596
- this.logger.log("Requesting removal of multiple items for " + this.name + ":", args);
604
+ this.logger.log("Requesting removal of multiple items for ".concat(this.name, ":"), args);
597
605
  return [4 /*yield*/, this.beforeRemoveMany(where, opt)];
598
606
  case 1:
599
607
  object = _b.sent();
@@ -767,11 +775,54 @@ var BaseCore = /** @class */ (function () {
767
775
  });
768
776
  };
769
777
  BaseCore.prototype.authenticate = function (params, opt) {
778
+ var _a;
770
779
  return __awaiter(this, void 0, void 0, function () {
771
- var fn;
772
- return __generator(this, function (_a) {
773
- fn = typeof this.props.authenticate === 'function' ? this.props.authenticate : defaultAuth;
774
- return [2 /*return*/, fn(params, opt)];
780
+ var context, authByToken, verifyResult, token, space;
781
+ return __generator(this, function (_b) {
782
+ switch (_b.label) {
783
+ case 0:
784
+ if (!(typeof this.props.authenticate === 'function')) return [3 /*break*/, 2];
785
+ return [4 /*yield*/, this.props.authenticate(params, opt)];
786
+ case 1:
787
+ // if we have an auth function passed to constructor then use it for the authentication
788
+ return [2 /*return*/, _b.sent()];
789
+ case 2:
790
+ context = this.context;
791
+ authByToken = false;
792
+ if (context.authorization && this.jwtSecret) {
793
+ verifyResult = (0, userToken_1.verifyUserToken)(context.authorization, this.jwtSecret, this.verifytokenArgs);
794
+ if (verifyResult.didFail()) {
795
+ return [2 /*return*/, [false, verifyResult.getErrorSummary('')]];
796
+ }
797
+ token = verifyResult.getDataFirst();
798
+ context.accountId = token.accountId;
799
+ context.roleId = token.roleId;
800
+ context.userId = token.userId;
801
+ context.spaceId = token.spaceId || context.spaceId;
802
+ authByToken = true;
803
+ }
804
+ space = null;
805
+ if (!this.gwSpace) return [3 /*break*/, 4];
806
+ return [4 /*yield*/, ((_a = this.gwSpace) === null || _a === void 0 ? void 0 : _a.get(context.spaceId || ''))];
807
+ case 3:
808
+ // Lookup space in case we have gateway
809
+ space = _b.sent();
810
+ if (!space) {
811
+ return [2 /*return*/, [false, 'Space Not Found']];
812
+ }
813
+ context.space = space;
814
+ _b.label = 4;
815
+ case 4:
816
+ if (!authByToken) {
817
+ if (this.interserviceApiKey && context.apiKey === this.interserviceApiKey) {
818
+ return [2 /*return*/, [true, '']];
819
+ }
820
+ else if ((space === null || space === void 0 ? void 0 : space.apiKey) && (space === null || space === void 0 ? void 0 : space.apiKey) !== context.apiKey) {
821
+ return [2 /*return*/, [false, 'API Key is invalid']];
822
+ }
823
+ }
824
+ return [2 /*return*/, [true, '']];
825
+ }
775
826
  });
776
827
  });
777
828
  };
@@ -1,5 +1,5 @@
1
1
  import { BaseGatewayInterface, ListArgsInterface } from './interfaces';
2
- import { BaseCore } from '.';
2
+ import { BaseCore } from './baseCore';
3
3
  declare class BaseGateway {
4
4
  protected config: BaseGatewayInterface;
5
5
  protected loader: any;
@@ -14,8 +14,8 @@ declare class BaseGateway {
14
14
  transformOnRemove(args?: any): Promise<any>;
15
15
  getDb(): import("./database").Database;
16
16
  getBuilder(): any;
17
- getIdField(): string;
18
- getCore(): BaseCore;
17
+ getIdField(): any;
18
+ getCore(): BaseCore | undefined;
19
19
  setCore(core: BaseCore): void;
20
20
  get(id: any): Promise<any>;
21
21
  getMany(ids: any[]): Promise<any>;
@@ -51,9 +51,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
51
51
  };
52
52
  Object.defineProperty(exports, "__esModule", { value: true });
53
53
  exports.BaseGateway = void 0;
54
+ var lodash_1 = require("lodash");
54
55
  var dataCache_1 = require("./dataCache");
55
56
  var constants_1 = __importDefault(require("./constants"));
56
- var lodash_1 = require("lodash");
57
57
  var ID_FIELD = constants_1.default.ID_FIELD;
58
58
  var defaultUpdateRemoveFilter = function (config) { return function (query, params) {
59
59
  var idName = config.idField || ID_FIELD;
@@ -394,7 +394,7 @@ var BaseGateway = /** @class */ (function () {
394
394
  results = [];
395
395
  idField = this.getIdField();
396
396
  return [4 /*yield*/, this.getBuilder()
397
- .insert(lodash_1.castArray(params))
397
+ .insert((0, lodash_1.castArray)(params))
398
398
  .returning(this.returnFields)
399
399
  .onConflict(this.config.setFields || idField)
400
400
  .merge()];