evaengine 1.0.14 → 2.0.0

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 (199) hide show
  1. package/README.md +15 -1
  2. package/bin/engine +2 -31
  3. package/dist/bin.d.ts +1 -0
  4. package/dist/bin.js +40 -0
  5. package/dist/bin.js.map +1 -0
  6. package/{src/commands/index.js → dist/commands/index.d.ts} +1 -8
  7. package/dist/commands/index.js +6 -0
  8. package/dist/commands/index.js.map +1 -0
  9. package/dist/commands/interface.d.ts +18 -0
  10. package/dist/commands/interface.js +40 -0
  11. package/dist/commands/interface.js.map +1 -0
  12. package/dist/commands/make_entity.d.ts +47 -0
  13. package/dist/commands/make_entity.js +410 -0
  14. package/dist/commands/make_entity.js.map +1 -0
  15. package/dist/commands/tramp.d.ts +7 -0
  16. package/dist/commands/tramp.js +29 -0
  17. package/dist/commands/tramp.js.map +1 -0
  18. package/dist/config/index.d.ts +92 -0
  19. package/dist/config/index.js +96 -0
  20. package/dist/config/index.js.map +1 -0
  21. package/dist/di.d.ts +73 -0
  22. package/dist/di.js +76 -0
  23. package/dist/di.js.map +1 -0
  24. package/dist/engine.d.ts +85 -0
  25. package/dist/engine.js +404 -0
  26. package/dist/engine.js.map +1 -0
  27. package/dist/entities/index.d.ts +54 -0
  28. package/dist/entities/index.js +228 -0
  29. package/dist/entities/index.js.map +1 -0
  30. package/dist/exceptions/index.d.ts +153 -0
  31. package/dist/exceptions/index.js +439 -0
  32. package/dist/exceptions/index.js.map +1 -0
  33. package/dist/index.d.ts +45 -0
  34. package/dist/index.js +45 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/middlewares/auth.d.ts +5 -0
  37. package/dist/middlewares/auth.js +63 -0
  38. package/dist/middlewares/auth.js.map +1 -0
  39. package/dist/middlewares/auth_kong.d.ts +2 -0
  40. package/dist/middlewares/auth_kong.js +24 -0
  41. package/dist/middlewares/auth_kong.js.map +1 -0
  42. package/dist/middlewares/debug.d.ts +3 -0
  43. package/dist/middlewares/debug.js +50 -0
  44. package/dist/middlewares/debug.js.map +1 -0
  45. package/{src/middlewares/index.js → dist/middlewares/index.d.ts} +1 -11
  46. package/dist/middlewares/index.js +9 -0
  47. package/dist/middlewares/index.js.map +1 -0
  48. package/dist/middlewares/providers.d.ts +25 -0
  49. package/dist/middlewares/providers.js +63 -0
  50. package/dist/middlewares/providers.js.map +1 -0
  51. package/dist/middlewares/session.d.ts +6 -0
  52. package/dist/middlewares/session.js +51 -0
  53. package/dist/middlewares/session.js.map +1 -0
  54. package/dist/middlewares/trace.d.ts +52 -0
  55. package/dist/middlewares/trace.js +225 -0
  56. package/dist/middlewares/trace.js.map +1 -0
  57. package/dist/middlewares/validator.d.ts +10 -0
  58. package/dist/middlewares/validator.js +43 -0
  59. package/dist/middlewares/validator.js.map +1 -0
  60. package/dist/middlewares/view_cache.d.ts +14 -0
  61. package/dist/middlewares/view_cache.js +102 -0
  62. package/dist/middlewares/view_cache.js.map +1 -0
  63. package/dist/services/cache.d.ts +66 -0
  64. package/dist/services/cache.js +207 -0
  65. package/dist/services/cache.js.map +1 -0
  66. package/dist/services/config.d.ts +25 -0
  67. package/dist/services/config.js +88 -0
  68. package/dist/services/config.js.map +1 -0
  69. package/dist/services/env.d.ts +12 -0
  70. package/dist/services/env.js +24 -0
  71. package/dist/services/env.js.map +1 -0
  72. package/dist/services/event_manager.d.ts +29 -0
  73. package/dist/services/event_manager.js +75 -0
  74. package/dist/services/event_manager.js.map +1 -0
  75. package/dist/services/http_client.d.ts +66 -0
  76. package/dist/services/http_client.js +62 -0
  77. package/dist/services/http_client.js.map +1 -0
  78. package/{src/services/index.js → dist/services/index.d.ts} +1 -15
  79. package/dist/services/index.js +14 -0
  80. package/dist/services/index.js.map +1 -0
  81. package/{src/services/interface.js → dist/services/interface.d.ts} +1 -3
  82. package/dist/services/interface.js +6 -0
  83. package/dist/services/interface.js.map +1 -0
  84. package/{src/services/joi.js → dist/services/joi.d.ts} +2 -9
  85. package/dist/services/joi.js +11 -0
  86. package/dist/services/joi.js.map +1 -0
  87. package/dist/services/jwt_token.d.ts +24 -0
  88. package/dist/services/jwt_token.js +77 -0
  89. package/dist/services/jwt_token.js.map +1 -0
  90. package/dist/services/jwt_token_kong.d.ts +25 -0
  91. package/dist/services/jwt_token_kong.js +65 -0
  92. package/dist/services/jwt_token_kong.js.map +1 -0
  93. package/dist/services/logger.d.ts +30 -0
  94. package/dist/services/logger.js +126 -0
  95. package/dist/services/logger.js.map +1 -0
  96. package/dist/services/namespace.d.ts +55 -0
  97. package/dist/services/namespace.js +157 -0
  98. package/dist/services/namespace.js.map +1 -0
  99. package/dist/services/now.d.ts +14 -0
  100. package/dist/services/now.js +43 -0
  101. package/dist/services/now.js.map +1 -0
  102. package/dist/services/providers.d.ts +55 -0
  103. package/dist/services/providers.js +134 -0
  104. package/dist/services/providers.js.map +1 -0
  105. package/dist/services/redis.d.ts +18 -0
  106. package/dist/services/redis.js +53 -0
  107. package/dist/services/redis.js.map +1 -0
  108. package/dist/services/rest_client.d.ts +48 -0
  109. package/dist/services/rest_client.js +96 -0
  110. package/dist/services/rest_client.js.map +1 -0
  111. package/dist/swagger/index.d.ts +223 -0
  112. package/dist/swagger/index.js +564 -0
  113. package/dist/swagger/index.js.map +1 -0
  114. package/dist/types/express.d.ts +14 -0
  115. package/dist/types/vendor.d.ts +102 -0
  116. package/dist/utils/api_scaffold.d.ts +74 -0
  117. package/dist/utils/api_scaffold.js +220 -0
  118. package/dist/utils/api_scaffold.js.map +1 -0
  119. package/dist/utils/case_converter.d.ts +7 -0
  120. package/dist/utils/case_converter.js +29 -0
  121. package/dist/utils/case_converter.js.map +1 -0
  122. package/dist/utils/crc32.d.ts +2 -0
  123. package/dist/utils/crc32.js +43 -0
  124. package/dist/utils/crc32.js.map +1 -0
  125. package/dist/utils/cron.d.ts +27 -0
  126. package/dist/utils/cron.js +186 -0
  127. package/dist/utils/cron.js.map +1 -0
  128. package/dist/utils/datetime.d.ts +4 -0
  129. package/dist/utils/datetime.js +6 -0
  130. package/dist/utils/datetime.js.map +1 -0
  131. package/dist/utils/host.d.ts +18 -0
  132. package/dist/utils/host.js +33 -0
  133. package/dist/utils/host.js.map +1 -0
  134. package/{src/utils/index.js → dist/utils/index.d.ts} +2 -26
  135. package/dist/utils/index.js +12 -0
  136. package/dist/utils/index.js.map +1 -0
  137. package/dist/utils/pagination.d.ts +150 -0
  138. package/dist/utils/pagination.js +242 -0
  139. package/dist/utils/pagination.js.map +1 -0
  140. package/dist/utils/random.d.ts +3 -0
  141. package/dist/utils/random.js +18 -0
  142. package/dist/utils/random.js.map +1 -0
  143. package/dist/utils/request_client.d.ts +33 -0
  144. package/dist/utils/request_client.js +153 -0
  145. package/dist/utils/request_client.js.map +1 -0
  146. package/dist/utils/smart_query.d.ts +144 -0
  147. package/dist/utils/smart_query.js +310 -0
  148. package/dist/utils/smart_query.js.map +1 -0
  149. package/dist/utils/test.d.ts +14 -0
  150. package/dist/utils/test.js +37 -0
  151. package/dist/utils/test.js.map +1 -0
  152. package/dist/utils/wrapper.d.ts +3 -0
  153. package/dist/utils/wrapper.js +5 -0
  154. package/dist/utils/wrapper.js.map +1 -0
  155. package/package.json +24 -10
  156. package/index.js +0 -2
  157. package/src/commands/interface.js +0 -45
  158. package/src/commands/make_entity.js +0 -490
  159. package/src/commands/tramp.js +0 -34
  160. package/src/config/index.js +0 -95
  161. package/src/di.js +0 -96
  162. package/src/engine.js +0 -556
  163. package/src/entities/index.js +0 -272
  164. package/src/exceptions/index.js +0 -501
  165. package/src/index.js +0 -55
  166. package/src/middlewares/auth.js +0 -67
  167. package/src/middlewares/auth_kong.js +0 -28
  168. package/src/middlewares/debug.js +0 -88
  169. package/src/middlewares/providers.js +0 -73
  170. package/src/middlewares/session.js +0 -63
  171. package/src/middlewares/trace.js +0 -254
  172. package/src/middlewares/validator.js +0 -50
  173. package/src/middlewares/view_cache.js +0 -127
  174. package/src/services/cache.js +0 -223
  175. package/src/services/config.js +0 -115
  176. package/src/services/env.js +0 -28
  177. package/src/services/event_manager.js +0 -84
  178. package/src/services/http_client.js +0 -69
  179. package/src/services/jwt_token.js +0 -89
  180. package/src/services/jwt_token_kong.js +0 -75
  181. package/src/services/logger.js +0 -150
  182. package/src/services/namespace.js +0 -176
  183. package/src/services/now.js +0 -49
  184. package/src/services/providers.js +0 -158
  185. package/src/services/redis.js +0 -63
  186. package/src/services/rest_client.js +0 -112
  187. package/src/swagger/index.js +0 -665
  188. package/src/utils/api_scaffold.js +0 -240
  189. package/src/utils/case_converter.js +0 -33
  190. package/src/utils/crc32.js +0 -44
  191. package/src/utils/cron.js +0 -208
  192. package/src/utils/datetime.js +0 -13
  193. package/src/utils/host.js +0 -39
  194. package/src/utils/pagination.js +0 -260
  195. package/src/utils/random.js +0 -23
  196. package/src/utils/request_client.js +0 -179
  197. package/src/utils/smart_query.js +0 -352
  198. package/src/utils/test.js +0 -48
  199. package/src/utils/wrapper.js +0 -8
@@ -1,501 +0,0 @@
1
- import * as appRoot from 'app-root-path';
2
- import path from 'path';
3
- import { format } from 'util';
4
- import crc32 from '../utils/crc32.js';
5
-
6
-
7
- /**
8
- * Make Error be able to work with JSON.stringify()
9
- */
10
- if (!('toJSON' in Error.prototype)) {
11
- Object.defineProperty(Error.prototype, 'toJSON', {
12
- value: function () {
13
- const alt = {};
14
- Object.getOwnPropertyNames(this).forEach(function (key) {
15
- alt[key] = this[key];
16
- }, this);
17
- return alt;
18
- },
19
- configurable: true,
20
- writable: true
21
- });
22
- }
23
-
24
-
25
- let i18nHandler = format;
26
-
27
- let factory = () => {
28
- };
29
-
30
- /**
31
- * Exception interface
32
- * Support usages:
33
- * throw new Exception();
34
- * throw new Exception('Something');
35
- * throw new Exception(new Error());
36
- * throw new Exception(new Exception());
37
- * throw (new Exception()).i18n('some %d', 123)
38
- */
39
- export class StandardException extends Error {
40
- /**
41
- * Generate string hash by crc32
42
- * @param {string} str
43
- * @param {string} padstr
44
- * @returns {string}
45
- */
46
- static hash(str, padstr = '0000000000') {
47
- const crcstr = crc32(str).toString();
48
- return padstr.substring(0, padstr.length - crcstr.length) + crcstr;
49
- }
50
-
51
- /**
52
- * Hash an exception into an 18 bits code
53
- * @param {string} className
54
- * @param {string} fileName
55
- * @returns {Number}
56
- */
57
- static generateCode(className, fileName = import.meta.filename) {
58
- const namespace = fileName.replace(appRoot.path, '').split(path.sep).join('/');
59
- const group = fileName === import.meta.filename ? '11111' : crc32(namespace).toString().substring(0, 5);
60
- return parseInt(`${group}000${StandardException.hash(className)}`, 10);
61
- }
62
-
63
- /**
64
- * Remove useless info from error stack
65
- * @param {string} stack
66
- * @returns {Array}
67
- */
68
- static stackBeautifier(stack) {
69
- const lines = stack.split('\n');
70
- const stackOut = [];
71
- for (const line of lines) {
72
- if (!line.match(/node_modules|\(node\.js|\(native/)) {
73
- stackOut.push(line);
74
- }
75
- }
76
- return stackOut;
77
- }
78
-
79
- static setI18nHandler(handler) {
80
- i18nHandler = handler;
81
- }
82
-
83
- /**
84
- * Deserialize an exception from a JSON object
85
- * @param {object} json
86
- */
87
- static factory(json = {}) {
88
- return factory(json);
89
- }
90
-
91
- /**
92
- * Http status code
93
- * @returns {number}
94
- */
95
- get statusCode() {
96
- return 500;
97
- }
98
-
99
- /**
100
- * If throw a StandardException, keep throw to above
101
- * If throw a Error, set Error message to exception message, set Error to details
102
- * If throw a String, set String to exception message
103
- * If throw a null or undefined, set exception name as exception message
104
- * Otherwise, throw a TypeError
105
- *
106
- * @param {StandardException|Error|String} exceptionOrMsg
107
- */
108
- constructor(exceptionOrMsg) {
109
- const throwingNothing = !exceptionOrMsg;
110
- const throwingString = typeof exceptionOrMsg === 'string';
111
- const throwingSelf = exceptionOrMsg instanceof StandardException;
112
- const throwingError = exceptionOrMsg instanceof Error;
113
- const throwingUnknown = throwingNothing + throwingString + throwingSelf + throwingError === 0;
114
-
115
- if (throwingUnknown === true) {
116
- throw new TypeError('Unexpected params for exception');
117
- }
118
-
119
- if (throwingSelf === true) {
120
- throw exceptionOrMsg;
121
- }
122
-
123
- let message = 'Something wrong';
124
- if (throwingString === true) {
125
- message = exceptionOrMsg;
126
- }
127
- if (throwingError === true) {
128
- ({ message } = exceptionOrMsg);
129
- }
130
- super(message);
131
-
132
- if (throwingNothing === true) {
133
- message = this.constructor.name;
134
- }
135
- this.message = message;
136
- this.humanMessage = message;
137
- this.throwingError = throwingError;
138
-
139
- Error.captureStackTrace(this, this.constructor);
140
- this.details = throwingError ? exceptionOrMsg : [];
141
- this.prevError = {};
142
- this.code = null;
143
- this.filename = import.meta.filename;
144
- this.translated = false;
145
- this.importance = 0;
146
- if (throwingError === true) {
147
- this.setPrevError(exceptionOrMsg);
148
- }
149
- }
150
-
151
- i18n(...args) {
152
- this.message = format(...args);
153
- this.humanMessage = i18nHandler(...args);
154
- if (this.message !== this.humanMessage) {
155
- this.translated = true;
156
- }
157
- return this;
158
- }
159
-
160
- getHumanMessage() {
161
- if (this.translated) {
162
- return this.humanMessage;
163
- }
164
- let message = i18nHandler(this.message);
165
- if (message !== this.message) {
166
- this.humanMessage = message;
167
- this.translated = true;
168
- return this.humanMessage;
169
- }
170
- message = i18nHandler(this.constructor.name);
171
- if (message !== this.constructor.name) {
172
- this.humanMessage = message;
173
- this.translated = true;
174
- return this.humanMessage;
175
- }
176
- return this.message;
177
- }
178
-
179
- setMessage(message) {
180
- this.message = message;
181
- return this;
182
- }
183
-
184
- setCode(code) {
185
- this.code = parseInt(code, 10);
186
- return this;
187
- }
188
-
189
- getCode() {
190
- if (this.code) {
191
- return this.code;
192
- }
193
- this.code = StandardException.generateCode(this.constructor.name, this.filename) || -1;
194
- return this.code;
195
- }
196
-
197
- getStatusCode() {
198
- return this.statusCode;
199
- }
200
-
201
- setImportance(importance) {
202
- this.importance = parseInt(importance, 10);
203
- return this;
204
- }
205
-
206
- getImportance() {
207
- return this.importance;
208
- }
209
-
210
- setFileName(filename) {
211
- this.filename = filename;
212
- return this;
213
- }
214
-
215
- getFileName() {
216
- return this.filename;
217
- }
218
-
219
- setDetails(details) {
220
- this.details = details;
221
- return this;
222
- }
223
-
224
- getDetails() {
225
- return Array.isArray(this.details) ? this.details : [this.details];
226
- }
227
-
228
- setPrevError(prevError) {
229
- this.prevError = prevError;
230
- return this;
231
- }
232
-
233
- getPrevError() {
234
- return this.prevError;
235
- }
236
-
237
- toJSON() {
238
- return {
239
- statusCode: this.statusCode,
240
- code: this.getCode(),
241
- name: this.constructor.name,
242
- message: this.message,
243
- humanMessage: this.getHumanMessage(),
244
- filename: this.getFileName(),
245
- prevError: this.getPrevError(),
246
- errors: Array.isArray(this.getDetails()) ?
247
- this.getDetails() : [this.getDetails()],
248
- stack: StandardException.stackBeautifier(this.stack),
249
- fullStack: this.stack.split('\n')
250
- };
251
- }
252
- }
253
-
254
- export class LogicException extends StandardException {
255
- get statusCode() {
256
- return 400;
257
- }
258
- }
259
-
260
- export class InvalidArgumentException extends LogicException {
261
- }
262
-
263
- export class FormInvalidateException extends InvalidArgumentException {
264
- constructor(exceptionOrMsg) {
265
- super(exceptionOrMsg);
266
- if (this.throwingError && exceptionOrMsg.isJoi === true) {
267
- this.message = exceptionOrMsg.details[0].message;
268
- this.details = exceptionOrMsg.details;
269
-
270
- this.i18n(this.message);
271
- this.translated = true;
272
- }
273
- }
274
- }
275
-
276
- export class ModelInvalidateException extends InvalidArgumentException {
277
- constructor(exceptionOrMsg) {
278
- super(exceptionOrMsg);
279
- if (this.throwingError) {
280
- this.details = exceptionOrMsg.errors;
281
- }
282
- }
283
- }
284
-
285
- export class HttpRequestLogicException extends InvalidArgumentException {
286
- /**
287
- * Support request promise error
288
- * https://github.com/request/promise-core/blob/master/lib/errors.js
289
- * @param exceptionOrMsg
290
- */
291
- constructor(exceptionOrMsg) {
292
- super(exceptionOrMsg);
293
- if (this.throwingError && ['StatusCodeError', 'RequestError', 'TransformError'].includes(exceptionOrMsg.name)) {
294
- this.details = exceptionOrMsg;
295
- const { response } = exceptionOrMsg;
296
- this.response = response || null;
297
- this.request = response ? response.request : null;
298
- }
299
- this.requestParams = null;
300
- this.responseParams = null;
301
- this.businessCode = null;
302
- }
303
-
304
- setResponse(response) {
305
- this.response = response || null;
306
- this.request = response ? response.request : null;
307
- return this;
308
- }
309
-
310
- getRequest() {
311
- if (!this.request) {
312
- throw new Error('No request set into exception');
313
- }
314
- return this.request;
315
- }
316
-
317
- getResponse() {
318
- if (!this.response) {
319
- throw new Error('No response set into exception');
320
- }
321
- return this.response;
322
- }
323
-
324
- setRequestParams(params) {
325
- this.requestParams = params;
326
- return this;
327
- }
328
-
329
- getRequestParams() {
330
- return this.requestParams;
331
- }
332
-
333
- setResponseParams(params) {
334
- this.responseParams = params;
335
- return this;
336
- }
337
-
338
- getResponseParams() {
339
- return this.responseParams;
340
- }
341
-
342
- setBusinessCode(code) {
343
- this.businessCode = code;
344
- return this;
345
- }
346
-
347
- getBusinessCode() {
348
- return this.businessCode;
349
- }
350
- }
351
-
352
- export class RestServiceLogicException extends HttpRequestLogicException {
353
- constructor(exceptionOrMsg) {
354
- super(exceptionOrMsg);
355
- if (this.throwingError && this.response && this.response.body) {
356
- this.prevError = factory(this.response.body);
357
- }
358
- }
359
- }
360
-
361
- export class UnauthorizedException extends LogicException {
362
- get statusCode() {
363
- return 401;
364
- }
365
- }
366
-
367
- export class OperationNotPermittedException extends LogicException {
368
- get statusCode() {
369
- return 403;
370
- }
371
- }
372
-
373
- export class ResourceNotFoundException extends LogicException {
374
- get statusCode() {
375
- return 404;
376
- }
377
- }
378
-
379
- export class OperationUnsupportedException extends LogicException {
380
- get statusCode() {
381
- return 405;
382
- }
383
- }
384
-
385
- export class ResourceConflictedException extends LogicException {
386
- get statusCode() {
387
- return 409;
388
- }
389
- }
390
-
391
- export class RuntimeException extends StandardException {
392
- }
393
-
394
- export class IOException extends RuntimeException {
395
- }
396
-
397
- export class HttpRequestIOException extends IOException {
398
- constructor(exceptionOrMsg) {
399
- super(exceptionOrMsg);
400
- if (this.throwingError && ['StatusCodeError', 'RequestError', 'TransformError'].includes(exceptionOrMsg.name)) {
401
- this.details = exceptionOrMsg;
402
- const { response } = exceptionOrMsg;
403
- this.response = response || null;
404
- this.request = response ? response.request : null;
405
- }
406
- this.requestParams = null;
407
- this.responseParams = null;
408
- this.businessCode = null;
409
- }
410
-
411
- getRequest() {
412
- return this.request;
413
- }
414
-
415
- getResponse() {
416
- return this.response;
417
- }
418
-
419
- setRequestParams(params) {
420
- this.requestParams = params;
421
- return this;
422
- }
423
-
424
- getRequestParams() {
425
- return this.requestParams;
426
- }
427
-
428
- setResponseParams(params) {
429
- this.responseParams = params;
430
- return this;
431
- }
432
-
433
- getResponseParams() {
434
- return this.responseParams;
435
- }
436
-
437
- setBusinessCode(code) {
438
- this.businessCode = code;
439
- return this;
440
- }
441
-
442
- getBusinessCode() {
443
- return this.businessCode;
444
- }
445
- }
446
-
447
- export class RestServiceIOException extends HttpRequestIOException {
448
- constructor(exceptionOrMsg) {
449
- super(exceptionOrMsg);
450
- if (this.throwingError && this.response && this.response.body) {
451
- this.prevError = factory(this.response.body);
452
- }
453
- }
454
- }
455
-
456
- export class DatabaseIOException extends IOException {
457
- }
458
-
459
- const exceptions = {
460
- StandardException,
461
- LogicException,
462
- InvalidArgumentException,
463
- FormInvalidateException,
464
- ModelInvalidateException,
465
- HttpRequestLogicException,
466
- RestServiceLogicException,
467
- UnauthorizedException,
468
- OperationUnsupportedException,
469
- OperationNotPermittedException,
470
- ResourceNotFoundException,
471
- ResourceConflictedException,
472
- RuntimeException,
473
- IOException,
474
- HttpRequestIOException,
475
- RestServiceIOException,
476
- DatabaseIOException
477
- };
478
-
479
- factory = (json) => {
480
- const {
481
- code, name, statusCode, message, prevError, errors, fullStack = []
482
- } = json;
483
- if (!code || !name || !statusCode || !message) {
484
- return {};
485
- }
486
-
487
- let e = null;
488
- if (Object.keys(exceptions).includes(name)) {
489
- e = new exceptions[name];
490
- } else if (statusCode < 500) {
491
- e = new LogicException(message);
492
- } else {
493
- e = new RuntimeException(message);
494
- }
495
-
496
- e.setCode(code)
497
- .setDetails(errors)
498
- .setPrevError(prevError);
499
- e.stack = fullStack.join('\n');
500
- return e;
501
- };
package/src/index.js DELETED
@@ -1,55 +0,0 @@
1
- import constitute from 'constitute';
2
- import sequelize from 'sequelize';
3
- import Joi from 'joi';
4
- import EvaEngine, * as engine from './engine.js';
5
- import * as swagger from './swagger/index.js';
6
- import * as exceptions from './exceptions/index.js';
7
- import * as services from './services/index.js';
8
- import * as middlewares from './middlewares/index.js';
9
- import * as ServiceProviders from './services/providers.js';
10
- import * as MiddlewareProviders from './middlewares/providers.js';
11
- import * as utils from './utils/index.js';
12
- import Command, * as commands from './commands/index.js';
13
- import Entities from './entities/index.js';
14
-
15
- const providers = {
16
- services: ServiceProviders,
17
- middlewares: MiddlewareProviders
18
- };
19
-
20
- const { wrapper } = utils;
21
-
22
- const {
23
- DI,
24
- express
25
- } = engine;
26
-
27
- /**
28
- * @typedef {Object} engineCore
29
- */
30
- const core = {
31
- EvaEngine,
32
- Command,
33
- DI,
34
- Entities,
35
- engine,
36
- express,
37
- commands,
38
- dependencies: {
39
- Joi,
40
- constitute,
41
- sequelize
42
- },
43
- exceptions,
44
- middlewares,
45
- sequelize,
46
- Joi,
47
- swagger,
48
- services,
49
- providers,
50
- wrapper,
51
- utils
52
- };
53
-
54
- export default core;
55
- export { core };
@@ -1,67 +0,0 @@
1
- import constitute from 'constitute';
2
- import wrapper from '../utils/wrapper.js';
3
- import { UnauthorizedException } from '../exceptions/index.js';
4
- import Config from '../services/config.js';
5
- import Now from '../services/now.js';
6
- import JsonWebToken from '../services/jwt_token.js';
7
-
8
- /**
9
- * @param _config {Config}
10
- * @param token {JsonWebToken}
11
- * @returns {function()}
12
- * @constructor
13
- */
14
- function AuthMiddleware(_config, token, now) {
15
- const config = _config.get();
16
- return () => wrapper(async (req, res, next) => {
17
- const jwToken = req.header('X-Token') || req.query.api_key;
18
- if (config.token.faker.enable === true && req.auth && req.auth.uid) {
19
- res.set('X-Uid', req.auth.uid);
20
- return next();
21
- }
22
- if (config.token.faker.enable === true && jwToken === config.token.faker.key) {
23
- req.auth = {
24
- type: 'fake',
25
- uid: config.token.faker.uid,
26
- token: config.token.faker.key
27
- };
28
- res.set('X-Uid', config.token.faker.uid);
29
- return next();
30
- }
31
- if (jwToken) {
32
- let parsedToken = {};
33
- try {
34
- parsedToken = await token.find(jwToken);
35
- } catch {
36
- throw new UnauthorizedException('Token not recognizable');
37
- }
38
- const { uid, expiredAt } = parsedToken;
39
- if (!uid) {
40
- throw new UnauthorizedException('User info not found in token');
41
- }
42
- if (typeof expiredAt !== 'number' || expiredAt <= now.getTimestamp()) {
43
- throw new UnauthorizedException('Token expired');
44
- }
45
- req.auth = {
46
- type: 'jwt',
47
- uid,
48
- token: jwToken
49
- };
50
- res.set('X-Uid', uid);
51
- return next();
52
- }
53
-
54
- if (req.session && req.session.uid) {
55
- req.auth = {
56
- type: 'session',
57
- uid: req.session.uid,
58
- token: ''
59
- };
60
- res.set('X-Uid', req.session.uid);
61
- return next();
62
- }
63
- throw new UnauthorizedException('No authority token found');
64
- });
65
- }
66
- constitute.Dependencies(Config, JsonWebToken, Now)(AuthMiddleware);
67
- export default AuthMiddleware;
@@ -1,28 +0,0 @@
1
- import { UnauthorizedException } from '../exceptions/index.js';
2
- import wrapper from '../utils/wrapper.js';
3
-
4
- /**
5
- * @returns {function()}
6
- * @constructor
7
- */
8
- function AuthKongMiddleware() {
9
- return () => wrapper(async (req, res, next) => {
10
- if (req.headers['x-anonymous-consumer'] === 'true') {
11
- throw new UnauthorizedException('Authentication failed');
12
- }
13
- const uid = Number.parseInt(req.headers['x-consumer-custom-id'], 10);
14
- const mobile = req.headers['x-consumer-custom-username'];
15
- if (uid > 0) {
16
- res.set('X-Uid', uid);
17
- res.set('X-Mobile', mobile);
18
- req.auth = {
19
- uid,
20
- mobile
21
- };
22
- return next();
23
- }
24
- throw new UnauthorizedException('Authentication failed, invalid UID');
25
- });
26
- }
27
-
28
- export default AuthKongMiddleware;