@sdflc/backend-helpers 1.0.48 → 1.0.51
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/BaseController.js +21 -62
- package/build/BaseCore.d.ts +2 -2
- package/build/BaseCore.js +425 -690
- package/build/BaseGateway.js +358 -554
- package/build/DataCache.d.ts +1 -1
- package/build/DataCache.js +55 -104
- package/build/Database.js +104 -177
- package/build/DbMigratorAndSeeder.js +131 -238
- package/build/RedisDataLoader.d.ts +41 -0
- package/build/RedisDataLoader.js +248 -0
- package/build/apiContext.js +103 -191
- package/build/checkItHelpers.js +10 -10
- package/build/commonValidators.js +49 -91
- package/build/configBulder.js +8 -19
- package/build/constants.js +3 -3
- package/build/dbHelpers.js +72 -126
- package/build/dbSeeder.js +38 -90
- package/build/dbTableHelpers.js +25 -26
- package/build/interfaces/BaseGatewayPropsInterface.d.ts +1 -0
- package/build/interfaces/CoreActionInterface.d.ts +2 -1
- package/build/knexConfigBuilder.js +17 -28
- package/build/passwordHelpers.js +13 -59
- package/build/redisClient.d.ts +6 -0
- package/build/redisClient.js +87 -88
- package/build/resolversHelpers.js +17 -27
- package/build/userToken.js +24 -24
- package/build/utils.js +4 -5
- package/package.json +33 -34
|
@@ -0,0 +1,248 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.RedisDataLoader = void 0;
|
|
16
|
+
const lodash_1 = require("lodash");
|
|
17
|
+
const dataloader_1 = __importDefault(require("dataloader"));
|
|
18
|
+
const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
|
|
19
|
+
const customOptions = ['expire', 'serialize', 'deserialize', 'cacheKeyFn'];
|
|
20
|
+
const mapPromise = (promise, fn) => Promise.all(promise.map(fn));
|
|
21
|
+
class RedisDataLoader {
|
|
22
|
+
constructor(redisClient, keySpace, userLoader, opt) {
|
|
23
|
+
this.redisClient = redisClient;
|
|
24
|
+
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 });
|
|
26
|
+
this.opt.cacheKeyFn =
|
|
27
|
+
typeof this.opt.cacheKeyFn === 'function'
|
|
28
|
+
? this.opt.cacheKeyFn
|
|
29
|
+
: (key) => ((0, lodash_1.isObject)(key) ? (0, json_stable_stringify_1.default)(key) : key);
|
|
30
|
+
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);
|
|
33
|
+
const keysToRead = [];
|
|
34
|
+
const keysIdxsToRead = [];
|
|
35
|
+
keys.forEach((key, idx) => {
|
|
36
|
+
if (results[idx] == null) {
|
|
37
|
+
keysToRead.push(key);
|
|
38
|
+
keysIdxsToRead.push(idx);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
if (keysToRead.length > 0) {
|
|
42
|
+
const loadedData = yield userLoader.load(keysToRead);
|
|
43
|
+
const data = yield this.rMSetAndGet(keysToRead, loadedData);
|
|
44
|
+
keysIdxsToRead.forEach((keyIdx, idx) => {
|
|
45
|
+
results[keyIdx] = data[idx];
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return results;
|
|
49
|
+
}), this.dataLoaderOpt);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Adds a prefix to the passed key
|
|
53
|
+
* @param key Key value
|
|
54
|
+
* @returns Trasnformed key value that includes key space
|
|
55
|
+
*/
|
|
56
|
+
makeKey(key) {
|
|
57
|
+
return `${this.keySpace}${this.opt.cacheKeyFn(key)}`;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Deserializes response from Readis
|
|
61
|
+
* @param str String to deserialize
|
|
62
|
+
* @returns
|
|
63
|
+
*/
|
|
64
|
+
parse(str) {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
+
try {
|
|
67
|
+
if (str === '' || str === null) {
|
|
68
|
+
return str;
|
|
69
|
+
}
|
|
70
|
+
else if (typeof this.opt.deserialize === 'function') {
|
|
71
|
+
return yield this.opt.deserialize(str);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
return JSON.parse(str);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch (ex) {
|
|
78
|
+
throw ex;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Serializes passed value to a string
|
|
84
|
+
* @param value A value to be serialized
|
|
85
|
+
* @returns serialized value in the form of string
|
|
86
|
+
*/
|
|
87
|
+
toString(value) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
if (value === null) {
|
|
90
|
+
return '';
|
|
91
|
+
}
|
|
92
|
+
else if (typeof this.opt.serialize === 'function') {
|
|
93
|
+
return yield this.opt.serialize(value);
|
|
94
|
+
}
|
|
95
|
+
else if ((0, lodash_1.isObject)(value)) {
|
|
96
|
+
return JSON.stringify(value);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
throw new Error(RedisDataLoader.messages.valueObjectOrNull);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
rSetAndGet(key, rawValue) {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
const value = yield this.toString(rawValue);
|
|
106
|
+
const fullKey = this.makeKey(key);
|
|
107
|
+
const multi = this.redisClient.multi();
|
|
108
|
+
multi.set(fullKey, value);
|
|
109
|
+
if (this.opt.expire) {
|
|
110
|
+
multi.expire(fullKey, this.opt.expire);
|
|
111
|
+
}
|
|
112
|
+
multi.get(fullKey);
|
|
113
|
+
const replies = yield multi.exec();
|
|
114
|
+
const lastReply = (0, lodash_1.last)(replies);
|
|
115
|
+
const result = yield this.parse(lastReply);
|
|
116
|
+
return result;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
rMSetAndGet(keys, rawValues) {
|
|
120
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
+
const multi = this.redisClient.multi();
|
|
122
|
+
const fullKeys = yield mapPromise(keys, (key) => this.makeKey(key));
|
|
123
|
+
const values = yield mapPromise(rawValues, (rawValue) => this.toString(rawValue));
|
|
124
|
+
multi.mSet(fullKeys.reduce((acc, fullKey, idx) => {
|
|
125
|
+
acc.push(fullKey);
|
|
126
|
+
acc.push(values[idx]);
|
|
127
|
+
return acc;
|
|
128
|
+
}, []));
|
|
129
|
+
if (this.opt.expire) {
|
|
130
|
+
for (const fullKey of fullKeys) {
|
|
131
|
+
multi.expire(fullKey, this.opt.expire);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
multi.mGet(fullKeys);
|
|
135
|
+
const replies = yield multi.exec();
|
|
136
|
+
const lastReplies = (0, lodash_1.last)(replies);
|
|
137
|
+
const result = yield mapPromise(lastReplies, (lastReply) => this.parse(lastReply));
|
|
138
|
+
return result;
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
rGet(key) {
|
|
142
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
+
const reply = yield this.redisClient.get(this.makeKey(key));
|
|
144
|
+
const result = yield this.parse(reply);
|
|
145
|
+
return result;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
rMGet(keys) {
|
|
149
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
150
|
+
const replies = yield this.redisClient.mGet((0, lodash_1.map)(keys, (key) => this.makeKey(key)));
|
|
151
|
+
const results = yield mapPromise(replies, (reply) => this.parse(reply));
|
|
152
|
+
return results;
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
rSet(key, rawValue) {
|
|
156
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
+
const value = yield this.toString(rawValue);
|
|
158
|
+
const reply = yield this.redisClient.set(this.makeKey(key), value);
|
|
159
|
+
const result = yield this.parse(reply);
|
|
160
|
+
// result is expected to be OK
|
|
161
|
+
return result;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
rMSet(keys, values) {
|
|
165
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
166
|
+
if (keys.length != values.length) {
|
|
167
|
+
throw new Error(RedisDataLoader.messages.keysValuesLengths);
|
|
168
|
+
}
|
|
169
|
+
const multi = this.redisClient.multi();
|
|
170
|
+
multi.mSet(keys.reduce((acc, key, idx) => {
|
|
171
|
+
acc.push(this.makeKey(key));
|
|
172
|
+
acc.push(values[idx]);
|
|
173
|
+
return acc;
|
|
174
|
+
}, []));
|
|
175
|
+
if (this.opt.expire) {
|
|
176
|
+
for (const key of keys) {
|
|
177
|
+
multi.expire(key, this.opt.expire);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
// The reply is expected to be "OK"
|
|
181
|
+
const reply = yield multi.exec();
|
|
182
|
+
return reply;
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
rDel(key) {
|
|
186
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
187
|
+
return yield this.redisClient.del(this.makeKey(key));
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
load(key) {
|
|
191
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
+
if (!key) {
|
|
193
|
+
throw new TypeError(RedisDataLoader.messages.keyRequired);
|
|
194
|
+
}
|
|
195
|
+
return yield this.loader.load(key);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
loadMany(keys) {
|
|
199
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
200
|
+
if (!Array.isArray(keys)) {
|
|
201
|
+
throw new TypeError(RedisDataLoader.messages.keysArrayRequired);
|
|
202
|
+
}
|
|
203
|
+
return yield this.loader.loadMany(keys);
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
prime(key, value) {
|
|
207
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
208
|
+
if (!key) {
|
|
209
|
+
throw new TypeError(RedisDataLoader.messages.keyRequired);
|
|
210
|
+
}
|
|
211
|
+
else if (value === undefined) {
|
|
212
|
+
throw new TypeError(RedisDataLoader.messages.valueRequired);
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
const result = yield this.rSetAndGet(key, value);
|
|
216
|
+
return yield this.loader.clear(key).prime(key, result === '' ? null : result);
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
clear(key) {
|
|
221
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
222
|
+
if (!key) {
|
|
223
|
+
throw new TypeError(RedisDataLoader.messages.keyRequired);
|
|
224
|
+
}
|
|
225
|
+
const resp = yield this.rDel(key);
|
|
226
|
+
yield this.loader.clear(key);
|
|
227
|
+
return resp;
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
clearAllLocal() {
|
|
231
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
232
|
+
return yield this.loader.clearAll();
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
clearLocal(key) {
|
|
236
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
237
|
+
return yield this.loader.clear(key);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
exports.RedisDataLoader = RedisDataLoader;
|
|
242
|
+
RedisDataLoader.messages = {
|
|
243
|
+
keyRequired: 'The "key" parameter is required',
|
|
244
|
+
keysArrayRequired: 'The "keys" parameter is required and should be an array',
|
|
245
|
+
valueRequired: 'The "value" parameter is required',
|
|
246
|
+
valueObjectOrNull: 'The "value" parameter must be an object or null',
|
|
247
|
+
keysValuesLengths: 'The "keys" and "values" are expected to be the same',
|
|
248
|
+
};
|
package/build/apiContext.js
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -19,196 +8,119 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
19
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
9
|
});
|
|
21
10
|
};
|
|
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
|
-
};
|
|
49
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
12
|
exports.createContextDefault = exports.createApiContext = void 0;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
13
|
+
const lodash_1 = require("lodash");
|
|
14
|
+
const uuid_1 = require("uuid");
|
|
15
|
+
const utils_1 = require("@sdflc/utils");
|
|
16
|
+
const Database_1 = require("./Database");
|
|
17
|
+
const constants_1 = require("./constants");
|
|
56
18
|
/**
|
|
57
19
|
* Creates a GraphQL context object with all available cores.
|
|
58
20
|
* @returns {object} A context object with all available cores
|
|
59
21
|
*/
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
case 6:
|
|
86
|
-
gatewayContext = _c;
|
|
87
|
-
context = {
|
|
88
|
-
requestId: requestId,
|
|
89
|
-
cores: {},
|
|
90
|
-
gateways: {},
|
|
91
|
-
req: req,
|
|
92
|
-
};
|
|
93
|
-
context.cores = Object.keys(cores || {}).reduce(function (acc, key) {
|
|
94
|
-
acc[(0, lodash_1.camelCase)(key)] = new cores[key](__assign(__assign({}, coreContext), { context: context }));
|
|
95
|
-
return acc;
|
|
96
|
-
}, {});
|
|
97
|
-
context.gateways = Object.keys(gateways || {}).reduce(function (acc, key) {
|
|
98
|
-
acc[(0, lodash_1.camelCase)(key)] = new gateways[key](__assign(__assign({}, gatewayContext), { context: context }));
|
|
99
|
-
return acc;
|
|
100
|
-
}, {});
|
|
101
|
-
if (!(typeof extendContext === 'function')) return [3 /*break*/, 8];
|
|
102
|
-
return [4 /*yield*/, extendContext({ context: context, req: req })];
|
|
103
|
-
case 7:
|
|
104
|
-
_d.sent();
|
|
105
|
-
_d.label = 8;
|
|
106
|
-
case 8: return [2 /*return*/, context];
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
}); };
|
|
22
|
+
const createApiContext = (args) => __awaiter(void 0, void 0, void 0, function* () {
|
|
23
|
+
const { cores, gateways, req, getCoreContext, getGatewayContext, extendContext, requestId: inRequestId } = args || {};
|
|
24
|
+
const requestId = inRequestId || (0, uuid_1.v4)();
|
|
25
|
+
const coreContext = typeof getCoreContext === 'function' ? yield getCoreContext({ req }) : {};
|
|
26
|
+
const gatewayContext = typeof getGatewayContext === 'function' ? yield getGatewayContext({ req }) : {};
|
|
27
|
+
const context = {
|
|
28
|
+
requestId,
|
|
29
|
+
cores: {},
|
|
30
|
+
gateways: {},
|
|
31
|
+
usedKeyPrefixes: {},
|
|
32
|
+
req,
|
|
33
|
+
};
|
|
34
|
+
context.cores = Object.keys(cores || {}).reduce((acc, key) => {
|
|
35
|
+
acc[(0, lodash_1.camelCase)(key)] = new cores[key](Object.assign(Object.assign({}, coreContext), { context }));
|
|
36
|
+
return acc;
|
|
37
|
+
}, {});
|
|
38
|
+
context.gateways = Object.keys(gateways || {}).reduce((acc, key) => {
|
|
39
|
+
acc[(0, lodash_1.camelCase)(key)] = new gateways[key](Object.assign(Object.assign({}, gatewayContext), { context }));
|
|
40
|
+
return acc;
|
|
41
|
+
}, {});
|
|
42
|
+
if (typeof extendContext === 'function') {
|
|
43
|
+
yield extendContext({ context, req });
|
|
44
|
+
}
|
|
45
|
+
return context;
|
|
46
|
+
});
|
|
110
47
|
exports.createApiContext = createApiContext;
|
|
111
48
|
/**
|
|
112
49
|
* Creates default a GraphQL context object with all avilable cores.
|
|
113
50
|
* @returns {object} A context object with all available cores
|
|
114
51
|
*/
|
|
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
|
-
|
|
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
|
-
|
|
187
|
-
case 1:
|
|
188
|
-
extendCoreConext = _a.sent();
|
|
189
|
-
_a.label = 2;
|
|
190
|
-
case 2: return [2 /*return*/, __assign({ db: db, gatewayName: '', isProduction: isProduction, logger: logger, doAuth: doAuth, spaceGw: spaceGw, interserviceApiKey: interserviceApiKey, jwtSecret: jwtSecret, verifyTokenArgs: verifyTokenArgs }, (extendCoreConext || {}))];
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
}); },
|
|
194
|
-
getGatewayContext: function (args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
195
|
-
var extendGatewayConext;
|
|
196
|
-
return __generator(this, function (_a) {
|
|
197
|
-
switch (_a.label) {
|
|
198
|
-
case 0:
|
|
199
|
-
extendGatewayConext = {};
|
|
200
|
-
if (!(typeof getGatewayContext === 'function')) return [3 /*break*/, 2];
|
|
201
|
-
return [4 /*yield*/, getGatewayContext(args)];
|
|
202
|
-
case 1:
|
|
203
|
-
extendGatewayConext = _a.sent();
|
|
204
|
-
_a.label = 2;
|
|
205
|
-
case 2: return [2 /*return*/, __assign({ db: db, isProduction: isProduction, logger: logger }, (extendGatewayConext || {}))];
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
}); },
|
|
209
|
-
})];
|
|
210
|
-
case 1: return [2 /*return*/, _b.sent()];
|
|
211
|
-
}
|
|
52
|
+
const createContextDefault = (args) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
const { knexConfig, isProduction, logger, doAuth, spaceGw, interserviceApiKey, jwtSecret, verifyTokenArgs, dbSchema, cores, gateways, req, extendContext, getCoreContext, getGatewayContext, } = args || {};
|
|
54
|
+
const authorization = req.headers[constants_1.HEADERS.AUTHORIZATION]
|
|
55
|
+
? req.headers[constants_1.HEADERS.AUTHORIZATION].replace('Bearer ', '')
|
|
56
|
+
: undefined;
|
|
57
|
+
const spaceId = req.headers[constants_1.HEADERS.SPACE_ID];
|
|
58
|
+
const apiKey = req.headers[constants_1.HEADERS.API_KEY];
|
|
59
|
+
const managementApiKey = req.headers[constants_1.HEADERS.MANAGEMENT_API_KEY];
|
|
60
|
+
const accountId = req.headers[constants_1.HEADERS.ACCOUNT_ID];
|
|
61
|
+
const roleId = req.headers[constants_1.HEADERS.ROLE_ID];
|
|
62
|
+
const userId = req.headers[constants_1.HEADERS.USER_ID];
|
|
63
|
+
const userAgent = req.headers[constants_1.HEADERS.USER_AGENT];
|
|
64
|
+
const referer = req.headers[constants_1.HEADERS.REFERER];
|
|
65
|
+
const ipAddress = req.headers[constants_1.HEADERS.IP_ADDRESS] || req.connection.remoteAddress;
|
|
66
|
+
const acceptLanguage = req.headers[constants_1.HEADERS.ACCEPT_LANGUAGE];
|
|
67
|
+
const languages = (0, utils_1.extractLanguages)(acceptLanguage);
|
|
68
|
+
const requestedLang = req.headers[constants_1.HEADERS.REQUESTED_LANGUAGE] || (languages === null || languages === void 0 ? void 0 : languages[0]) || 'en';
|
|
69
|
+
const requestId = (0, uuid_1.v4)();
|
|
70
|
+
const db = new Database_1.Database({
|
|
71
|
+
knexConfig,
|
|
72
|
+
dbSchema,
|
|
73
|
+
});
|
|
74
|
+
const sharedContext = {
|
|
75
|
+
spaceId,
|
|
76
|
+
apiKey,
|
|
77
|
+
managementApiKey,
|
|
78
|
+
userAgent,
|
|
79
|
+
referer,
|
|
80
|
+
ipAddress,
|
|
81
|
+
acceptLanguage,
|
|
82
|
+
languages,
|
|
83
|
+
requestedLang,
|
|
84
|
+
authorization,
|
|
85
|
+
accountId,
|
|
86
|
+
roleId,
|
|
87
|
+
userId,
|
|
88
|
+
};
|
|
89
|
+
return yield (0, exports.createApiContext)({
|
|
90
|
+
cores,
|
|
91
|
+
gateways,
|
|
92
|
+
req,
|
|
93
|
+
requestId,
|
|
94
|
+
extendContext: ({ context, req }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
95
|
+
context.db = db;
|
|
96
|
+
Object.assign(context, sharedContext);
|
|
97
|
+
if (typeof extendContext === 'function') {
|
|
98
|
+
yield extendContext({ context, req });
|
|
99
|
+
}
|
|
100
|
+
}),
|
|
101
|
+
getCoreContext: (args) => __awaiter(void 0, void 0, void 0, function* () {
|
|
102
|
+
// TODO: const { req } = args;
|
|
103
|
+
let extendCoreConext = {};
|
|
104
|
+
if (typeof getCoreContext === 'function') {
|
|
105
|
+
extendCoreConext = yield getCoreContext(args);
|
|
106
|
+
}
|
|
107
|
+
return Object.assign({ db, gatewayName: '', isProduction,
|
|
108
|
+
logger,
|
|
109
|
+
doAuth,
|
|
110
|
+
spaceGw,
|
|
111
|
+
interserviceApiKey,
|
|
112
|
+
jwtSecret,
|
|
113
|
+
verifyTokenArgs }, (extendCoreConext || {}));
|
|
114
|
+
}),
|
|
115
|
+
getGatewayContext: (args) => __awaiter(void 0, void 0, void 0, function* () {
|
|
116
|
+
let extendGatewayConext = {};
|
|
117
|
+
if (typeof getGatewayContext === 'function') {
|
|
118
|
+
extendGatewayConext = yield getGatewayContext(args);
|
|
119
|
+
}
|
|
120
|
+
return Object.assign({ db,
|
|
121
|
+
isProduction,
|
|
122
|
+
logger }, (extendGatewayConext || {}));
|
|
123
|
+
}),
|
|
212
124
|
});
|
|
213
|
-
});
|
|
125
|
+
});
|
|
214
126
|
exports.createContextDefault = createContextDefault;
|
package/build/checkItHelpers.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createErrorOnCheckError = exports.checkArray = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
const api_helpers_1 = require("@sdflc/api-helpers");
|
|
5
|
+
const checkArray = (arr, validator, prefix) => {
|
|
6
|
+
const result = new api_helpers_1.OpResult();
|
|
7
7
|
if (!Array.isArray(arr)) {
|
|
8
8
|
return result.addError(prefix || '', 'The value should be an array');
|
|
9
9
|
}
|
|
10
|
-
arr.forEach(
|
|
11
|
-
|
|
10
|
+
arr.forEach((item, idx) => {
|
|
11
|
+
const [itemResult] = validator.validateSync(item);
|
|
12
12
|
if (itemResult) {
|
|
13
|
-
Object.keys(itemResult.errors).forEach(
|
|
14
|
-
result.addError(
|
|
13
|
+
Object.keys(itemResult.errors).forEach((key) => {
|
|
14
|
+
result.addError(`${prefix || ''}[${idx}].${key}`, itemResult.errors[key].message);
|
|
15
15
|
});
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
18
|
return result;
|
|
19
19
|
};
|
|
20
20
|
exports.checkArray = checkArray;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
Object.keys(checkItResult.errors).forEach(
|
|
21
|
+
const createErrorOnCheckError = (checkItResult) => {
|
|
22
|
+
const result = new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED });
|
|
23
|
+
Object.keys(checkItResult.errors).forEach((key) => {
|
|
24
24
|
result.addError(key, checkItResult.errors[key].message);
|
|
25
25
|
});
|
|
26
26
|
return result;
|