evaengine 1.0.14 → 3.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.
- package/README.md +15 -7
- package/bin/engine +2 -31
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +32 -0
- package/dist/bin.js.map +1 -0
- package/{src/commands/index.js → dist/commands/index.d.ts} +1 -8
- package/dist/commands/index.js +6 -0
- package/dist/commands/index.js.map +1 -0
- package/dist/commands/interface.d.ts +18 -0
- package/dist/commands/interface.js +40 -0
- package/dist/commands/interface.js.map +1 -0
- package/dist/commands/make_entity.d.ts +47 -0
- package/dist/commands/make_entity.js +410 -0
- package/dist/commands/make_entity.js.map +1 -0
- package/dist/commands/tramp.d.ts +7 -0
- package/dist/commands/tramp.js +29 -0
- package/dist/commands/tramp.js.map +1 -0
- package/dist/config/index.d.ts +92 -0
- package/dist/config/index.js +96 -0
- package/dist/config/index.js.map +1 -0
- package/dist/di.d.ts +131 -0
- package/dist/di.js +197 -0
- package/dist/di.js.map +1 -0
- package/dist/engine.d.ts +85 -0
- package/dist/engine.js +404 -0
- package/dist/engine.js.map +1 -0
- package/dist/entities/index.d.ts +54 -0
- package/dist/entities/index.js +228 -0
- package/dist/entities/index.js.map +1 -0
- package/dist/exceptions/index.d.ts +153 -0
- package/dist/exceptions/index.js +466 -0
- package/dist/exceptions/index.js.map +1 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/dist/middlewares/auth.d.ts +5 -0
- package/dist/middlewares/auth.js +63 -0
- package/dist/middlewares/auth.js.map +1 -0
- package/dist/middlewares/auth_kong.d.ts +2 -0
- package/dist/middlewares/auth_kong.js +24 -0
- package/dist/middlewares/auth_kong.js.map +1 -0
- package/dist/middlewares/debug.d.ts +3 -0
- package/dist/middlewares/debug.js +50 -0
- package/dist/middlewares/debug.js.map +1 -0
- package/{src/middlewares/index.js → dist/middlewares/index.d.ts} +1 -11
- package/dist/middlewares/index.js +9 -0
- package/dist/middlewares/index.js.map +1 -0
- package/dist/middlewares/providers.d.ts +25 -0
- package/dist/middlewares/providers.js +63 -0
- package/dist/middlewares/providers.js.map +1 -0
- package/dist/middlewares/session.d.ts +5 -0
- package/dist/middlewares/session.js +46 -0
- package/dist/middlewares/session.js.map +1 -0
- package/dist/middlewares/trace.d.ts +52 -0
- package/dist/middlewares/trace.js +225 -0
- package/dist/middlewares/trace.js.map +1 -0
- package/dist/middlewares/validator.d.ts +10 -0
- package/dist/middlewares/validator.js +43 -0
- package/dist/middlewares/validator.js.map +1 -0
- package/dist/middlewares/view_cache.d.ts +14 -0
- package/dist/middlewares/view_cache.js +102 -0
- package/dist/middlewares/view_cache.js.map +1 -0
- package/dist/services/cache.d.ts +68 -0
- package/dist/services/cache.js +206 -0
- package/dist/services/cache.js.map +1 -0
- package/dist/services/config.d.ts +17 -0
- package/dist/services/config.js +67 -0
- package/dist/services/config.js.map +1 -0
- package/dist/services/env.d.ts +12 -0
- package/dist/services/env.js +24 -0
- package/dist/services/env.js.map +1 -0
- package/dist/services/event_manager.d.ts +30 -0
- package/dist/services/event_manager.js +74 -0
- package/dist/services/event_manager.js.map +1 -0
- package/dist/services/http_client.d.ts +67 -0
- package/dist/services/http_client.js +61 -0
- package/dist/services/http_client.js.map +1 -0
- package/{src/services/index.js → dist/services/index.d.ts} +1 -15
- package/dist/services/index.js +14 -0
- package/dist/services/index.js.map +1 -0
- package/{src/services/interface.js → dist/services/interface.d.ts} +1 -3
- package/dist/services/interface.js +6 -0
- package/dist/services/interface.js.map +1 -0
- package/{src/services/joi.js → dist/services/joi.d.ts} +2 -9
- package/dist/services/joi.js +11 -0
- package/dist/services/joi.js.map +1 -0
- package/dist/services/jwt_token.d.ts +25 -0
- package/dist/services/jwt_token.js +76 -0
- package/dist/services/jwt_token.js.map +1 -0
- package/dist/services/jwt_token_kong.d.ts +26 -0
- package/dist/services/jwt_token_kong.js +64 -0
- package/dist/services/jwt_token_kong.js.map +1 -0
- package/dist/services/logger.d.ts +31 -0
- package/dist/services/logger.js +125 -0
- package/dist/services/logger.js.map +1 -0
- package/dist/services/namespace.d.ts +62 -0
- package/dist/services/namespace.js +171 -0
- package/dist/services/namespace.js.map +1 -0
- package/dist/services/now.d.ts +15 -0
- package/dist/services/now.js +42 -0
- package/dist/services/now.js.map +1 -0
- package/dist/services/providers.d.ts +55 -0
- package/dist/services/providers.js +134 -0
- package/dist/services/providers.js.map +1 -0
- package/dist/services/redis.d.ts +19 -0
- package/dist/services/redis.js +52 -0
- package/dist/services/redis.js.map +1 -0
- package/dist/services/rest_client.d.ts +49 -0
- package/dist/services/rest_client.js +95 -0
- package/dist/services/rest_client.js.map +1 -0
- package/dist/swagger/index.d.ts +223 -0
- package/dist/swagger/index.js +564 -0
- package/dist/swagger/index.js.map +1 -0
- package/dist/types/express.d.ts +14 -0
- package/dist/types/vendor.d.ts +30 -0
- package/dist/utils/api_scaffold.d.ts +74 -0
- package/dist/utils/api_scaffold.js +220 -0
- package/dist/utils/api_scaffold.js.map +1 -0
- package/dist/utils/case_converter.d.ts +7 -0
- package/dist/utils/case_converter.js +29 -0
- package/dist/utils/case_converter.js.map +1 -0
- package/dist/utils/crc32.d.ts +2 -0
- package/dist/utils/crc32.js +43 -0
- package/dist/utils/crc32.js.map +1 -0
- package/dist/utils/cron.d.ts +27 -0
- package/dist/utils/cron.js +186 -0
- package/dist/utils/cron.js.map +1 -0
- package/dist/utils/datetime.d.ts +4 -0
- package/dist/utils/datetime.js +6 -0
- package/dist/utils/datetime.js.map +1 -0
- package/dist/utils/host.d.ts +18 -0
- package/dist/utils/host.js +33 -0
- package/dist/utils/host.js.map +1 -0
- package/{src/utils/index.js → dist/utils/index.d.ts} +2 -26
- package/dist/utils/index.js +12 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/jwt.d.ts +16 -0
- package/dist/utils/jwt.js +93 -0
- package/dist/utils/jwt.js.map +1 -0
- package/dist/utils/pagination.d.ts +150 -0
- package/dist/utils/pagination.js +242 -0
- package/dist/utils/pagination.js.map +1 -0
- package/dist/utils/random.d.ts +3 -0
- package/dist/utils/random.js +18 -0
- package/dist/utils/random.js.map +1 -0
- package/dist/utils/request_client.d.ts +33 -0
- package/dist/utils/request_client.js +153 -0
- package/dist/utils/request_client.js.map +1 -0
- package/dist/utils/smart_query.d.ts +144 -0
- package/dist/utils/smart_query.js +310 -0
- package/dist/utils/smart_query.js.map +1 -0
- package/dist/utils/test.d.ts +14 -0
- package/dist/utils/test.js +37 -0
- package/dist/utils/test.js.map +1 -0
- package/dist/utils/wrapper.d.ts +3 -0
- package/dist/utils/wrapper.js +5 -0
- package/dist/utils/wrapper.js.map +1 -0
- package/package.json +22 -15
- package/index.js +0 -2
- package/src/commands/interface.js +0 -45
- package/src/commands/make_entity.js +0 -490
- package/src/commands/tramp.js +0 -34
- package/src/config/index.js +0 -95
- package/src/di.js +0 -96
- package/src/engine.js +0 -556
- package/src/entities/index.js +0 -272
- package/src/exceptions/index.js +0 -501
- package/src/index.js +0 -55
- package/src/middlewares/auth.js +0 -67
- package/src/middlewares/auth_kong.js +0 -28
- package/src/middlewares/debug.js +0 -88
- package/src/middlewares/providers.js +0 -73
- package/src/middlewares/session.js +0 -63
- package/src/middlewares/trace.js +0 -254
- package/src/middlewares/validator.js +0 -50
- package/src/middlewares/view_cache.js +0 -127
- package/src/services/cache.js +0 -223
- package/src/services/config.js +0 -115
- package/src/services/env.js +0 -28
- package/src/services/event_manager.js +0 -84
- package/src/services/http_client.js +0 -69
- package/src/services/jwt_token.js +0 -89
- package/src/services/jwt_token_kong.js +0 -75
- package/src/services/logger.js +0 -150
- package/src/services/namespace.js +0 -176
- package/src/services/now.js +0 -49
- package/src/services/providers.js +0 -158
- package/src/services/redis.js +0 -63
- package/src/services/rest_client.js +0 -112
- package/src/swagger/index.js +0 -665
- package/src/utils/api_scaffold.js +0 -240
- package/src/utils/case_converter.js +0 -33
- package/src/utils/crc32.js +0 -44
- package/src/utils/cron.js +0 -208
- package/src/utils/datetime.js +0 -13
- package/src/utils/host.js +0 -39
- package/src/utils/pagination.js +0 -260
- package/src/utils/random.js +0 -23
- package/src/utils/request_client.js +0 -179
- package/src/utils/smart_query.js +0 -352
- package/src/utils/test.js +0 -48
- package/src/utils/wrapper.js +0 -8
package/src/exceptions/index.js
DELETED
|
@@ -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 };
|
package/src/middlewares/auth.js
DELETED
|
@@ -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;
|