@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.
- package/build/BaseCore.js +496 -600
- package/build/BaseGateway.js +331 -377
- package/build/DataCache.js +16 -19
- package/build/Database.js +55 -74
- package/build/DbMigratorAndSeeder.js +88 -114
- package/build/RedisDataLoader.js +153 -184
- package/build/apiContext.js +35 -33
- package/build/baseController.js +9 -16
- package/build/commonValidators.js +22 -31
- package/build/configBulder.js +5 -1
- package/build/dbHelpers.js +19 -29
- package/build/dbSeeder.js +11 -20
- package/build/dbTableHelpers.js +10 -10
- package/build/downloadHelpers.js +15 -25
- package/build/esm/BaseCore.js +496 -600
- package/build/esm/BaseGateway.js +331 -377
- package/build/esm/DataCache.js +16 -19
- package/build/esm/Database.js +55 -74
- package/build/esm/DbMigratorAndSeeder.js +88 -114
- package/build/esm/RedisDataLoader.js +153 -184
- package/build/esm/apiContext.js +35 -33
- package/build/esm/baseController.js +9 -16
- package/build/esm/commonValidators.js +22 -31
- package/build/esm/configBulder.js +5 -1
- package/build/esm/dbHelpers.js +19 -29
- package/build/esm/dbSeeder.js +11 -20
- package/build/esm/dbTableHelpers.js +10 -10
- package/build/esm/downloadHelpers.js +15 -25
- package/build/esm/knexConfigBuilder.js +17 -7
- package/build/esm/passwordHelpers.js +7 -16
- package/build/esm/resolversHelpers.js +11 -4
- package/build/esm/userToken.js +2 -2
- package/build/knexConfigBuilder.js +17 -7
- package/build/passwordHelpers.js +7 -16
- package/build/resolversHelpers.js +11 -4
- package/build/userToken.js +2 -2
- package/package.json +2 -3
package/build/RedisDataLoader.js
CHANGED
|
@@ -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 =
|
|
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) =>
|
|
32
|
-
const results =
|
|
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 =
|
|
43
|
-
const data =
|
|
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
|
-
}
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return
|
|
187
|
-
|
|
188
|
-
|
|
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
|
-
};
|
package/build/apiContext.js
CHANGED
|
@@ -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) =>
|
|
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) ?
|
|
30
|
-
const gatewayContext = check_types_1.default.function(getGatewayContext) ?
|
|
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](
|
|
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](
|
|
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
|
-
|
|
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) =>
|
|
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] ||
|
|
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
|
|
99
|
+
return await (0, exports.createApiContext)({
|
|
106
100
|
cores,
|
|
107
101
|
gateways,
|
|
108
102
|
req,
|
|
109
103
|
res,
|
|
110
104
|
requestId,
|
|
111
|
-
extendContext:
|
|
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
|
-
|
|
109
|
+
await extendContext({ context, req });
|
|
116
110
|
}
|
|
117
|
-
}
|
|
118
|
-
getCoreContext: (args) =>
|
|
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 =
|
|
116
|
+
extendCoreConext = await getCoreContext(args);
|
|
123
117
|
}
|
|
124
|
-
return
|
|
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
|
|
131
|
-
|
|
132
|
-
|
|
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 =
|
|
134
|
+
extendGatewayConext = await getGatewayContext(args);
|
|
136
135
|
}
|
|
137
|
-
return
|
|
136
|
+
return {
|
|
137
|
+
db,
|
|
138
138
|
isProduction,
|
|
139
|
-
logger
|
|
140
|
-
|
|
139
|
+
logger,
|
|
140
|
+
...(extendGatewayConext || {}),
|
|
141
|
+
};
|
|
142
|
+
},
|
|
141
143
|
});
|
|
142
|
-
}
|
|
144
|
+
};
|
|
143
145
|
exports.createContextDefault = createContextDefault;
|
package/build/baseController.js
CHANGED
|
@@ -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
|
|
25
|
-
this.onResponse = props
|
|
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 =
|
|
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) =>
|
|
38
|
+
return async (req, res, next) => {
|
|
46
39
|
try {
|
|
47
|
-
const result =
|
|
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
|
-
|
|
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;
|