@sdflc/backend-helpers 1.0.79 → 1.0.80

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.
Files changed (37) hide show
  1. package/build/BaseCore.js +496 -600
  2. package/build/BaseGateway.js +331 -377
  3. package/build/DataCache.js +16 -19
  4. package/build/Database.js +55 -74
  5. package/build/DbMigratorAndSeeder.js +88 -114
  6. package/build/RedisDataLoader.js +153 -184
  7. package/build/apiContext.js +35 -33
  8. package/build/baseController.js +9 -16
  9. package/build/commonValidators.js +22 -31
  10. package/build/configBulder.js +5 -1
  11. package/build/dbHelpers.js +19 -29
  12. package/build/dbSeeder.js +11 -20
  13. package/build/dbTableHelpers.js +10 -10
  14. package/build/downloadHelpers.js +15 -25
  15. package/build/esm/BaseCore.js +496 -600
  16. package/build/esm/BaseGateway.js +331 -377
  17. package/build/esm/DataCache.js +16 -19
  18. package/build/esm/Database.js +55 -74
  19. package/build/esm/DbMigratorAndSeeder.js +88 -114
  20. package/build/esm/RedisDataLoader.js +153 -184
  21. package/build/esm/apiContext.js +35 -33
  22. package/build/esm/baseController.js +9 -16
  23. package/build/esm/commonValidators.js +22 -31
  24. package/build/esm/configBulder.js +5 -1
  25. package/build/esm/dbHelpers.js +19 -29
  26. package/build/esm/dbSeeder.js +11 -20
  27. package/build/esm/dbTableHelpers.js +10 -10
  28. package/build/esm/downloadHelpers.js +15 -25
  29. package/build/esm/knexConfigBuilder.js +17 -7
  30. package/build/esm/passwordHelpers.js +7 -16
  31. package/build/esm/resolversHelpers.js +11 -4
  32. package/build/esm/userToken.js +2 -2
  33. package/build/knexConfigBuilder.js +17 -7
  34. package/build/passwordHelpers.js +7 -16
  35. package/build/resolversHelpers.js +11 -4
  36. package/build/userToken.js +2 -2
  37. package/package.json +2 -3
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -19,17 +10,32 @@ const json_stable_stringify_1 = __importDefault(require("json-stable-stringify")
19
10
  const customOptions = ['expire', 'serialize', 'deserialize', 'cacheKeyFn'];
20
11
  const mapPromise = (promise, fn) => Promise.all(promise.map(fn));
21
12
  class RedisDataLoader {
13
+ opt;
14
+ dataLoaderOpt;
15
+ keySpace;
16
+ loader;
17
+ redisClient;
18
+ static messages = {
19
+ keyRequired: 'The "key" parameter is required',
20
+ keysArrayRequired: 'The "keys" parameter is required and should be an array',
21
+ valueRequired: 'The "value" parameter is required',
22
+ valueObjectOrNull: 'The "value" parameter must be an object or null',
23
+ keysValuesLengths: 'The "keys" and "values" are expected to be the same',
24
+ };
22
25
  constructor(redisClient, keySpace, userLoader, opt) {
23
26
  this.redisClient = redisClient;
24
27
  this.opt = (0, lodash_1.pick)(opt, customOptions) || {};
25
- this.dataLoaderOpt = Object.assign(Object.assign({}, (0, lodash_1.omit)(opt, customOptions)), { cacheKeyFn: this.opt.cacheKeyFn });
28
+ this.dataLoaderOpt = {
29
+ ...(0, lodash_1.omit)(opt, customOptions),
30
+ cacheKeyFn: this.opt.cacheKeyFn,
31
+ };
26
32
  this.opt.cacheKeyFn =
27
33
  typeof this.opt.cacheKeyFn === 'function'
28
34
  ? this.opt.cacheKeyFn
29
35
  : (key) => ((0, lodash_1.isObject)(key) ? (0, json_stable_stringify_1.default)(key) : key);
30
36
  this.keySpace = keySpace ? keySpace + ':' : '';
31
- this.loader = new dataloader_1.default((keys) => __awaiter(this, void 0, void 0, function* () {
32
- const results = yield this.rMGet(keys);
37
+ this.loader = new dataloader_1.default(async (keys) => {
38
+ const results = await this.rMGet(keys);
33
39
  const keysToRead = [];
34
40
  const keysIdxsToRead = [];
35
41
  keys.forEach((key, idx) => {
@@ -39,14 +45,14 @@ class RedisDataLoader {
39
45
  }
40
46
  });
41
47
  if (keysToRead.length > 0) {
42
- const loadedData = yield userLoader.load(keysToRead);
43
- const data = yield this.rMSetAndGet(keysToRead, loadedData);
48
+ const loadedData = await userLoader.load(keysToRead);
49
+ const data = await this.rMSetAndGet(keysToRead, loadedData);
44
50
  keysIdxsToRead.forEach((keyIdx, idx) => {
45
51
  results[keyIdx] = data[idx];
46
52
  });
47
53
  }
48
54
  return results;
49
- }), this.dataLoaderOpt);
55
+ }, this.dataLoaderOpt);
50
56
  }
51
57
  /**
52
58
  * Adds a prefix to the passed key
@@ -61,183 +67,146 @@ class RedisDataLoader {
61
67
  * @param str String to deserialize
62
68
  * @returns
63
69
  */
64
- parse(str) {
65
- return __awaiter(this, void 0, void 0, function* () {
66
- if (str === '' || str === null) {
67
- return str;
68
- }
69
- else if (typeof this.opt.deserialize === 'function') {
70
- return yield this.opt.deserialize(str);
71
- }
72
- else {
73
- return JSON.parse(str);
74
- }
75
- });
70
+ async parse(str) {
71
+ if (str === '' || str === null) {
72
+ return str;
73
+ }
74
+ else if (typeof this.opt.deserialize === 'function') {
75
+ return await this.opt.deserialize(str);
76
+ }
77
+ else {
78
+ return JSON.parse(str);
79
+ }
76
80
  }
77
81
  /**
78
82
  * Serializes passed value to a string
79
83
  * @param value A value to be serialized
80
84
  * @returns serialized value in the form of string
81
85
  */
82
- toString(value) {
83
- return __awaiter(this, void 0, void 0, function* () {
84
- if (value === null) {
85
- return '';
86
- }
87
- else if (typeof this.opt.serialize === 'function') {
88
- return yield this.opt.serialize(value);
89
- }
90
- else if ((0, lodash_1.isObject)(value)) {
91
- return JSON.stringify(value);
92
- }
93
- else {
94
- throw new Error(RedisDataLoader.messages.valueObjectOrNull);
95
- }
96
- });
97
- }
98
- rSetAndGet(key, rawValue) {
99
- return __awaiter(this, void 0, void 0, function* () {
100
- const value = yield this.toString(rawValue);
101
- const fullKey = this.makeKey(key);
102
- const multi = this.redisClient.multi();
103
- multi.set(fullKey, value);
104
- if (this.opt.expire) {
86
+ async toString(value) {
87
+ if (value === null) {
88
+ return '';
89
+ }
90
+ else if (typeof this.opt.serialize === 'function') {
91
+ return await this.opt.serialize(value);
92
+ }
93
+ else if ((0, lodash_1.isObject)(value)) {
94
+ return JSON.stringify(value);
95
+ }
96
+ else {
97
+ throw new Error(RedisDataLoader.messages.valueObjectOrNull);
98
+ }
99
+ }
100
+ async rSetAndGet(key, rawValue) {
101
+ const value = await this.toString(rawValue);
102
+ const fullKey = this.makeKey(key);
103
+ const multi = this.redisClient.multi();
104
+ multi.set(fullKey, value);
105
+ if (this.opt.expire) {
106
+ multi.expire(fullKey, this.opt.expire);
107
+ }
108
+ multi.get(fullKey);
109
+ const replies = await multi.exec();
110
+ const lastReply = (0, lodash_1.last)(replies);
111
+ const result = await this.parse(lastReply);
112
+ return result;
113
+ }
114
+ async rMSetAndGet(keys, rawValues) {
115
+ const multi = this.redisClient.multi();
116
+ const fullKeys = await mapPromise(keys, (key) => this.makeKey(key));
117
+ const values = await mapPromise(rawValues, (rawValue) => this.toString(rawValue));
118
+ multi.mSet(fullKeys.reduce((acc, fullKey, idx) => {
119
+ acc.push(fullKey);
120
+ acc.push(values[idx]);
121
+ return acc;
122
+ }, []));
123
+ if (this.opt.expire) {
124
+ for (const fullKey of fullKeys) {
105
125
  multi.expire(fullKey, this.opt.expire);
106
126
  }
107
- multi.get(fullKey);
108
- const replies = yield multi.exec();
109
- const lastReply = (0, lodash_1.last)(replies);
110
- const result = yield this.parse(lastReply);
111
- return result;
112
- });
113
- }
114
- rMSetAndGet(keys, rawValues) {
115
- return __awaiter(this, void 0, void 0, function* () {
116
- const multi = this.redisClient.multi();
117
- const fullKeys = yield mapPromise(keys, (key) => this.makeKey(key));
118
- const values = yield mapPromise(rawValues, (rawValue) => this.toString(rawValue));
119
- multi.mSet(fullKeys.reduce((acc, fullKey, idx) => {
120
- acc.push(fullKey);
121
- acc.push(values[idx]);
122
- return acc;
123
- }, []));
124
- if (this.opt.expire) {
125
- for (const fullKey of fullKeys) {
126
- multi.expire(fullKey, this.opt.expire);
127
- }
128
- }
129
- multi.mGet(fullKeys);
130
- const replies = yield multi.exec();
131
- const lastReplies = (0, lodash_1.last)(replies);
132
- const result = yield mapPromise(lastReplies, (lastReply) => this.parse(lastReply));
133
- return result;
134
- });
135
- }
136
- rGet(key) {
137
- return __awaiter(this, void 0, void 0, function* () {
138
- const reply = yield this.redisClient.get(this.makeKey(key));
139
- const result = yield this.parse(reply);
140
- return result;
141
- });
142
- }
143
- rMGet(keys) {
144
- return __awaiter(this, void 0, void 0, function* () {
145
- const replies = yield this.redisClient.mGet((0, lodash_1.map)(keys, (key) => this.makeKey(key)));
146
- const results = yield mapPromise(replies, (reply) => this.parse(reply));
147
- return results;
148
- });
149
- }
150
- rSet(key, rawValue) {
151
- return __awaiter(this, void 0, void 0, function* () {
152
- const value = yield this.toString(rawValue);
153
- const reply = yield this.redisClient.set(this.makeKey(key), value);
154
- const result = yield this.parse(reply);
155
- // result is expected to be OK
156
- return result;
157
- });
158
- }
159
- rMSet(keys, values) {
160
- return __awaiter(this, void 0, void 0, function* () {
161
- if (keys.length != values.length) {
162
- throw new Error(RedisDataLoader.messages.keysValuesLengths);
163
- }
164
- const multi = this.redisClient.multi();
165
- multi.mSet(keys.reduce((acc, key, idx) => {
166
- acc.push(this.makeKey(key));
167
- acc.push(values[idx]);
168
- return acc;
169
- }, []));
170
- if (this.opt.expire) {
171
- for (const key of keys) {
172
- multi.expire(key, this.opt.expire);
173
- }
174
- }
175
- // The reply is expected to be "OK"
176
- const reply = yield multi.exec();
177
- return reply;
178
- });
179
- }
180
- rDel(key) {
181
- return __awaiter(this, void 0, void 0, function* () {
182
- return yield this.redisClient.del(this.makeKey(key));
183
- });
184
- }
185
- load(key) {
186
- return __awaiter(this, void 0, void 0, function* () {
187
- if (!key) {
188
- throw new TypeError(RedisDataLoader.messages.keyRequired);
189
- }
190
- return yield this.loader.load(key);
191
- });
192
- }
193
- loadMany(keys) {
194
- return __awaiter(this, void 0, void 0, function* () {
195
- if (!Array.isArray(keys)) {
196
- throw new TypeError(RedisDataLoader.messages.keysArrayRequired);
197
- }
198
- return yield this.loader.loadMany(keys);
199
- });
200
- }
201
- prime(key, value) {
202
- return __awaiter(this, void 0, void 0, function* () {
203
- if (!key) {
204
- throw new TypeError(RedisDataLoader.messages.keyRequired);
205
- }
206
- else if (value === undefined) {
207
- throw new TypeError(RedisDataLoader.messages.valueRequired);
208
- }
209
- else {
210
- const result = yield this.rSetAndGet(key, value);
211
- return yield this.loader.clear(key).prime(key, result === '' ? null : result);
212
- }
213
- });
214
- }
215
- clear(key) {
216
- return __awaiter(this, void 0, void 0, function* () {
217
- if (!key) {
218
- throw new TypeError(RedisDataLoader.messages.keyRequired);
219
- }
220
- const resp = yield this.rDel(key);
221
- yield this.loader.clear(key);
222
- return resp;
223
- });
224
- }
225
- clearAllLocal() {
226
- return __awaiter(this, void 0, void 0, function* () {
227
- return yield this.loader.clearAll();
228
- });
229
- }
230
- clearLocal(key) {
231
- return __awaiter(this, void 0, void 0, function* () {
232
- return yield this.loader.clear(key);
233
- });
127
+ }
128
+ multi.mGet(fullKeys);
129
+ const replies = await multi.exec();
130
+ const lastReplies = (0, lodash_1.last)(replies);
131
+ const result = await mapPromise(lastReplies, (lastReply) => this.parse(lastReply));
132
+ return result;
133
+ }
134
+ async rGet(key) {
135
+ const reply = await this.redisClient.get(this.makeKey(key));
136
+ const result = await this.parse(reply);
137
+ return result;
138
+ }
139
+ async rMGet(keys) {
140
+ const replies = await this.redisClient.mGet((0, lodash_1.map)(keys, (key) => this.makeKey(key)));
141
+ const results = await mapPromise(replies, (reply) => this.parse(reply));
142
+ return results;
143
+ }
144
+ async rSet(key, rawValue) {
145
+ const value = await this.toString(rawValue);
146
+ const reply = await this.redisClient.set(this.makeKey(key), value);
147
+ const result = await this.parse(reply);
148
+ // result is expected to be OK
149
+ return result;
150
+ }
151
+ async rMSet(keys, values) {
152
+ if (keys.length != values.length) {
153
+ throw new Error(RedisDataLoader.messages.keysValuesLengths);
154
+ }
155
+ const multi = this.redisClient.multi();
156
+ multi.mSet(keys.reduce((acc, key, idx) => {
157
+ acc.push(this.makeKey(key));
158
+ acc.push(values[idx]);
159
+ return acc;
160
+ }, []));
161
+ if (this.opt.expire) {
162
+ for (const key of keys) {
163
+ multi.expire(key, this.opt.expire);
164
+ }
165
+ }
166
+ // The reply is expected to be "OK"
167
+ const reply = await multi.exec();
168
+ return reply;
169
+ }
170
+ async rDel(key) {
171
+ return await this.redisClient.del(this.makeKey(key));
172
+ }
173
+ async load(key) {
174
+ if (!key) {
175
+ throw new TypeError(RedisDataLoader.messages.keyRequired);
176
+ }
177
+ return await this.loader.load(key);
178
+ }
179
+ async loadMany(keys) {
180
+ if (!Array.isArray(keys)) {
181
+ throw new TypeError(RedisDataLoader.messages.keysArrayRequired);
182
+ }
183
+ return await this.loader.loadMany(keys);
184
+ }
185
+ async prime(key, value) {
186
+ if (!key) {
187
+ throw new TypeError(RedisDataLoader.messages.keyRequired);
188
+ }
189
+ else if (value === undefined) {
190
+ throw new TypeError(RedisDataLoader.messages.valueRequired);
191
+ }
192
+ else {
193
+ const result = await this.rSetAndGet(key, value);
194
+ return await this.loader.clear(key).prime(key, result === '' ? null : result);
195
+ }
196
+ }
197
+ async clear(key) {
198
+ if (!key) {
199
+ throw new TypeError(RedisDataLoader.messages.keyRequired);
200
+ }
201
+ const resp = await this.rDel(key);
202
+ await this.loader.clear(key);
203
+ return resp;
204
+ }
205
+ async clearAllLocal() {
206
+ return await this.loader.clearAll();
207
+ }
208
+ async clearLocal(key) {
209
+ return await this.loader.clear(key);
234
210
  }
235
211
  }
236
212
  exports.RedisDataLoader = RedisDataLoader;
237
- RedisDataLoader.messages = {
238
- keyRequired: 'The "key" parameter is required',
239
- keysArrayRequired: 'The "keys" parameter is required and should be an array',
240
- valueRequired: 'The "value" parameter is required',
241
- valueObjectOrNull: 'The "value" parameter must be an object or null',
242
- keysValuesLengths: 'The "keys" and "values" are expected to be the same',
243
- };
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -23,11 +14,11 @@ const constants_1 = require("./constants");
23
14
  * Creates a GraphQL context object with all available cores.
24
15
  * @returns {object} A context object with all available cores
25
16
  */
26
- const createApiContext = (args) => __awaiter(void 0, void 0, void 0, function* () {
17
+ const createApiContext = async (args) => {
27
18
  const { cores, gateways, req, res, getCoreContext, getGatewayContext, extendContext, requestId: inRequestId, } = args || {};
28
19
  const requestId = inRequestId || (0, uuid_1.v4)();
29
- const coreContext = check_types_1.default.function(getCoreContext) ? yield getCoreContext({ req }) : {};
30
- const gatewayContext = check_types_1.default.function(getGatewayContext) ? yield getGatewayContext({ req }) : {};
20
+ const coreContext = check_types_1.default.function(getCoreContext) ? await getCoreContext({ req }) : {};
21
+ const gatewayContext = check_types_1.default.function(getGatewayContext) ? await getGatewayContext({ req }) : {};
31
22
  const context = {
32
23
  requestId,
33
24
  cores: {},
@@ -37,24 +28,27 @@ const createApiContext = (args) => __awaiter(void 0, void 0, void 0, function* (
37
28
  res,
38
29
  };
39
30
  context.cores = Object.keys(cores || {}).reduce((acc, key) => {
40
- acc[(0, lodash_1.camelCase)(key)] = new cores[key](Object.assign(Object.assign({}, coreContext), { context }));
31
+ acc[(0, lodash_1.camelCase)(key)] = new cores[key]({ ...coreContext, context });
41
32
  return acc;
42
33
  }, {});
43
34
  context.gateways = Object.keys(gateways || {}).reduce((acc, key) => {
44
- acc[(0, lodash_1.camelCase)(key)] = new gateways[key](Object.assign(Object.assign({}, gatewayContext), { context }));
35
+ acc[(0, lodash_1.camelCase)(key)] = new gateways[key]({
36
+ ...gatewayContext,
37
+ context,
38
+ });
45
39
  return acc;
46
40
  }, {});
47
41
  if (check_types_1.default.function(extendContext)) {
48
- yield extendContext({ context, req });
42
+ await extendContext({ context, req });
49
43
  }
50
44
  return context;
51
- });
45
+ };
52
46
  exports.createApiContext = createApiContext;
53
47
  /**
54
48
  * Creates default a GraphQL context object with all avilable cores.
55
49
  * @returns {object} A context object with all available cores
56
50
  */
57
- const createContextDefault = (args) => __awaiter(void 0, void 0, void 0, function* () {
51
+ const createContextDefault = async (args) => {
58
52
  const { knexConfig, isProduction, logger, doAuth, spaceGw, interserviceApiKey, jwtSecret, verifyTokenArgs, dbSchema, cores, gateways, req, res, extendContext, getCoreContext, getGatewayContext, authorizationToken, db: inDb, } = args || {};
59
53
  const headers = req.headers || { noHeadersSetup: true };
60
54
  const cookies = req.cookies || { noCookiesSetup: true };
@@ -68,7 +62,7 @@ const createContextDefault = (args) => __awaiter(void 0, void 0, void 0, functio
68
62
  const ipAddress = headers[constants_1.HEADERS.IP_ADDRESS] || req.connection.remoteAddress;
69
63
  const acceptLanguage = headers[constants_1.HEADERS.ACCEPT_LANGUAGE];
70
64
  const languages = (0, utils_1.extractLanguages)(acceptLanguage);
71
- const requestedLang = headers[constants_1.HEADERS.REQUESTED_LANGUAGE] || (languages === null || languages === void 0 ? void 0 : languages[0]) || 'en';
65
+ const requestedLang = headers[constants_1.HEADERS.REQUESTED_LANGUAGE] || languages?.[0] || 'en';
72
66
  // const accountId = headers[HEADERS.ACCOUNT_ID];
73
67
  // const roleId = headers[HEADERS.ROLE_ID];
74
68
  // const userId = headers[HEADERS.USER_ID];
@@ -102,42 +96,50 @@ const createContextDefault = (args) => __awaiter(void 0, void 0, void 0, functio
102
96
  authorizationToken,
103
97
  isExternalDb: !!inDb,
104
98
  };
105
- return yield (0, exports.createApiContext)({
99
+ return await (0, exports.createApiContext)({
106
100
  cores,
107
101
  gateways,
108
102
  req,
109
103
  res,
110
104
  requestId,
111
- extendContext: (_a) => __awaiter(void 0, [_a], void 0, function* ({ context, req }) {
105
+ extendContext: async ({ context, req }) => {
112
106
  context.db = db;
113
107
  Object.assign(context, sharedContext);
114
108
  if (check_types_1.default.function(extendContext)) {
115
- yield extendContext({ context, req });
109
+ await extendContext({ context, req });
116
110
  }
117
- }),
118
- getCoreContext: (args) => __awaiter(void 0, void 0, void 0, function* () {
111
+ },
112
+ getCoreContext: async (args) => {
119
113
  // TODO: const { req } = args;
120
114
  let extendCoreConext = {};
121
115
  if (check_types_1.default.function(getCoreContext)) {
122
- extendCoreConext = yield getCoreContext(args);
116
+ extendCoreConext = await getCoreContext(args);
123
117
  }
124
- return Object.assign({ db, gatewayName: '', isProduction,
118
+ return {
119
+ db,
120
+ gatewayName: '',
121
+ isProduction,
125
122
  logger,
126
123
  doAuth,
127
124
  spaceGw,
128
125
  interserviceApiKey,
129
126
  jwtSecret,
130
- verifyTokenArgs }, (extendCoreConext || {}));
131
- }),
132
- getGatewayContext: (args) => __awaiter(void 0, void 0, void 0, function* () {
127
+ verifyTokenArgs,
128
+ ...(extendCoreConext || {}),
129
+ };
130
+ },
131
+ getGatewayContext: async (args) => {
133
132
  let extendGatewayConext = {};
134
133
  if (check_types_1.default.function(getGatewayContext)) {
135
- extendGatewayConext = yield getGatewayContext(args);
134
+ extendGatewayConext = await getGatewayContext(args);
136
135
  }
137
- return Object.assign({ db,
136
+ return {
137
+ db,
138
138
  isProduction,
139
- logger }, (extendGatewayConext || {}));
140
- }),
139
+ logger,
140
+ ...(extendGatewayConext || {}),
141
+ };
142
+ },
141
143
  });
142
- });
144
+ };
143
145
  exports.createContextDefault = createContextDefault;
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -20,16 +11,18 @@ const utils_1 = require("@sdflc/utils");
20
11
  * Base class for REST API controllers.
21
12
  */
22
13
  class BaseController {
14
+ onResponse;
15
+ logger;
23
16
  constructor(props) {
24
- if (check_types_1.default.function(props === null || props === void 0 ? void 0 : props.onResponse)) {
25
- this.onResponse = props === null || props === void 0 ? void 0 : props.onResponse;
17
+ if (check_types_1.default.function(props?.onResponse)) {
18
+ this.onResponse = props?.onResponse;
26
19
  }
27
20
  else {
28
21
  this.onResponse = () => {
29
22
  /* TODO */
30
23
  };
31
24
  }
32
- this.logger = (props === null || props === void 0 ? void 0 : props.logger) instanceof utils_1.Logger ? props.logger : new utils_1.Logger({ level: utils_1.LoggerLevels.ERROR });
25
+ this.logger = props?.logger instanceof utils_1.Logger ? props.logger : new utils_1.Logger({ level: utils_1.LoggerLevels.ERROR });
33
26
  }
34
27
  init(props) {
35
28
  // const { app } = props;
@@ -42,9 +35,9 @@ class BaseController {
42
35
  * @returns HTTP response
43
36
  */
44
37
  json(method) {
45
- return (req, res, next) => __awaiter(this, void 0, void 0, function* () {
38
+ return async (req, res, next) => {
46
39
  try {
47
- const result = yield method(req, res, next);
40
+ const result = await method(req, res, next);
48
41
  if (result instanceof api_helpers_1.OpResult) {
49
42
  res.status(result.getHttpStatus()).json(result.toJS());
50
43
  }
@@ -54,13 +47,13 @@ class BaseController {
54
47
  else {
55
48
  res.status(204).json(new api_helpers_1.OpResult());
56
49
  }
57
- yield this.onResponse(req, res, next);
50
+ await this.onResponse(req, res, next);
58
51
  }
59
52
  catch (ex) {
60
53
  this.logger.log(`An exception has occured when processing REST API request on the entry point '${req.method} ${req.originalUrl}' from ${req.ip}`);
61
54
  next(ex);
62
55
  }
63
- });
56
+ };
64
57
  }
65
58
  }
66
59
  exports.BaseController = BaseController;