@azteam/express 1.2.148 → 1.2.151
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azteam/express",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.151",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -23,9 +23,13 @@
|
|
|
23
23
|
},
|
|
24
24
|
"homepage": "https://github.com/toda93/express#readme",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@azteam/crypto": "
|
|
27
|
-
"@azteam/error": "
|
|
28
|
-
"@azteam/http-client": "
|
|
26
|
+
"@azteam/crypto": "latest",
|
|
27
|
+
"@azteam/error": "latest",
|
|
28
|
+
"@azteam/http-client": "latest",
|
|
29
|
+
"@azteam/rabbitmq-async": "latest",
|
|
30
|
+
"@azteam/redis-async": "latest",
|
|
31
|
+
"@grpc/grpc-js": "1.6.7",
|
|
32
|
+
"@grpc/proto-loader": "0.6.12",
|
|
29
33
|
"body-parser": "1.19.0",
|
|
30
34
|
"cookie-parser": "1.4.4",
|
|
31
35
|
"cors": "2.8.4",
|
package/src/Server.js
CHANGED
|
@@ -10,7 +10,7 @@ import morgan from 'morgan';
|
|
|
10
10
|
import cors from 'cors';
|
|
11
11
|
import _ from 'lodash';
|
|
12
12
|
import 'express-async-errors';
|
|
13
|
-
import {
|
|
13
|
+
import {errorCatch, ErrorException, NOT_FOUND, UNKNOWN} from '@azteam/error';
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
const RES_TYPE = {
|
|
@@ -40,6 +40,9 @@ function omitItem(item, guard, allows) {
|
|
|
40
40
|
|
|
41
41
|
class Server {
|
|
42
42
|
constructor(currentDir = '', options = {}) {
|
|
43
|
+
this.redis = null;
|
|
44
|
+
this.messageQueue = null;
|
|
45
|
+
|
|
43
46
|
this.options = options;
|
|
44
47
|
|
|
45
48
|
this.cookieOptions = {
|
|
@@ -57,8 +60,17 @@ class Server {
|
|
|
57
60
|
this.debug = process.env.NODE_ENV === 'development';
|
|
58
61
|
|
|
59
62
|
this.initController(currentDir);
|
|
63
|
+
}
|
|
64
|
+
|
|
60
65
|
|
|
66
|
+
setMessageQueue(messageQueue) {
|
|
67
|
+
this.messageQueue = messageQueue;
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
61
70
|
|
|
71
|
+
setRedis(redis) {
|
|
72
|
+
this.redis = redis;
|
|
73
|
+
return this;
|
|
62
74
|
}
|
|
63
75
|
|
|
64
76
|
setCookieOptions(cookieOptions) {
|
|
@@ -118,6 +130,7 @@ class Server {
|
|
|
118
130
|
|
|
119
131
|
|
|
120
132
|
startAPI(port) {
|
|
133
|
+
|
|
121
134
|
if (!_.isEmpty(this.controllers)) {
|
|
122
135
|
|
|
123
136
|
const WHITE_LIST = this.whiteList;
|
|
@@ -130,8 +143,8 @@ class Server {
|
|
|
130
143
|
}));
|
|
131
144
|
|
|
132
145
|
app.use(methodOverride());
|
|
133
|
-
app.use(bodyParser.urlencoded({
|
|
134
|
-
app.use(bodyParser.json({
|
|
146
|
+
app.use(bodyParser.urlencoded({limit: '5mb', extended: true}));
|
|
147
|
+
app.use(bodyParser.json({limit: '5mb'}));
|
|
135
148
|
|
|
136
149
|
app.set('trust proxy', 1);
|
|
137
150
|
|
|
@@ -161,92 +174,108 @@ class Server {
|
|
|
161
174
|
});
|
|
162
175
|
app.get('/favicon.ico', (req, res) => res.status(204).json({}));
|
|
163
176
|
|
|
164
|
-
app.use(async function(req, res, next) {
|
|
165
|
-
req.trackDevice = {
|
|
166
|
-
ip: req.ip,
|
|
167
|
-
device: req.get('X-DEVICE') || req.get('User-Agent'),
|
|
168
|
-
device_id: req.get('X-DEVICE-ID') || 'web',
|
|
169
|
-
os: req.get('X-OS') || 'web'
|
|
170
|
-
};
|
|
171
177
|
|
|
178
|
+
const {redis} = this;
|
|
172
179
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
180
|
+
if (redis) {
|
|
181
|
+
app.request.redis = redis;
|
|
182
|
+
app.response.redis = redis;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
app.response.error = function(code, errors = []) {
|
|
187
|
+
throw new ErrorException(code, errors);
|
|
188
|
+
};
|
|
176
189
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
190
|
+
app.response.success = function(data = {}, guard = [], allows = []) {
|
|
191
|
+
|
|
192
|
+
let guardData = data;
|
|
193
|
+
if (data) {
|
|
194
|
+
let resType = null;
|
|
195
|
+
if (_.isArray(data)) {
|
|
196
|
+
resType = RES_TYPE.ARRAY;
|
|
197
|
+
} else if (_.isObject(data)) {
|
|
198
|
+
resType = RES_TYPE.OBJECT;
|
|
199
|
+
if (data.docs) {
|
|
200
|
+
resType = RES_TYPE.DOCS;
|
|
188
201
|
}
|
|
202
|
+
}
|
|
189
203
|
|
|
190
|
-
|
|
204
|
+
if (_.isArray(guard)) {
|
|
205
|
+
guard = [
|
|
206
|
+
...guard,
|
|
207
|
+
'__v',
|
|
208
|
+
'_id',
|
|
209
|
+
'deleted_at',
|
|
210
|
+
'updated_at',
|
|
211
|
+
'created_id',
|
|
212
|
+
'modified_id'
|
|
213
|
+
];
|
|
214
|
+
if (resType === RES_TYPE.ARRAY || resType === RES_TYPE.DOCS) {
|
|
191
215
|
guard = [
|
|
192
216
|
...guard,
|
|
193
|
-
'
|
|
194
|
-
'
|
|
195
|
-
'
|
|
196
|
-
'
|
|
197
|
-
'created_id',
|
|
198
|
-
'modified_id'
|
|
217
|
+
'metadata_disable',
|
|
218
|
+
'metadata_keywords',
|
|
219
|
+
'metadata_description',
|
|
220
|
+
'metadata_image_url'
|
|
199
221
|
];
|
|
200
|
-
if (resType === RES_TYPE.ARRAY || resType === RES_TYPE.DOCS) {
|
|
201
|
-
guard = [
|
|
202
|
-
...guard,
|
|
203
|
-
'metadata_disable',
|
|
204
|
-
'metadata_keywords',
|
|
205
|
-
'metadata_description',
|
|
206
|
-
'metadata_image_url'
|
|
207
|
-
];
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
if (resType === RES_TYPE.DOCS) {
|
|
211
|
-
guardData.docs = _.map(data.docs, item => {
|
|
212
|
-
return omitItem(item, guard, allows);
|
|
213
|
-
});
|
|
214
|
-
} else if (resType === RES_TYPE.ARRAY) {
|
|
215
|
-
guardData = _.map(data, item => {
|
|
216
|
-
return omitItem(item, guard, allows);
|
|
217
|
-
});
|
|
218
|
-
} else if (resType === RES_TYPE.OBJECT) {
|
|
219
|
-
guardData = omitItem(data, guard, allows);
|
|
220
222
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
223
|
}
|
|
224
|
+
if (resType === RES_TYPE.DOCS) {
|
|
225
|
+
guardData.docs = _.map(data.docs, item => {
|
|
226
|
+
return omitItem(item, guard, allows);
|
|
227
|
+
});
|
|
228
|
+
} else if (resType === RES_TYPE.ARRAY) {
|
|
229
|
+
guardData = _.map(data, item => {
|
|
230
|
+
return omitItem(item, guard, allows);
|
|
231
|
+
});
|
|
232
|
+
} else if (resType === RES_TYPE.OBJECT) {
|
|
233
|
+
guardData = omitItem(data, guard, allows);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
224
236
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
});
|
|
237
|
+
const resData = {
|
|
238
|
+
success: true,
|
|
239
|
+
data: guardData,
|
|
240
|
+
options: this.resOptions
|
|
230
241
|
};
|
|
231
242
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
domain: COOKIE_OPTIONS.domain
|
|
236
|
-
});
|
|
237
|
-
});
|
|
238
|
-
};
|
|
243
|
+
if (this.redis && this.cache) {
|
|
244
|
+
this.redis.set(this.cache.key, resData, this.ttl);
|
|
245
|
+
}
|
|
239
246
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
});
|
|
247
|
+
return this.json(resData);
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
app.response.cleanCookie = function(data) {
|
|
251
|
+
_.map(data, (name) => {
|
|
252
|
+
this.clearCookie(name, {
|
|
253
|
+
domain: COOKIE_OPTIONS.domain
|
|
248
254
|
});
|
|
255
|
+
});
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
app.response.addCookie = function(data) {
|
|
259
|
+
_.map(data, (value, key) => {
|
|
260
|
+
const maxAge = 86400000 * 365; // 1 year
|
|
261
|
+
this.cookie(key, value, {
|
|
262
|
+
...COOKIE_OPTIONS,
|
|
263
|
+
maxAge,
|
|
264
|
+
expires: new Date(Date.now() + maxAge)
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
app.use(async function(req, res, next) {
|
|
270
|
+
delete res.cache;
|
|
271
|
+
|
|
272
|
+
req.trackDevice = {
|
|
273
|
+
ip: req.ip,
|
|
274
|
+
device: req.get('X-DEVICE') || req.get('User-Agent'),
|
|
275
|
+
device_id: req.get('X-DEVICE-ID') || 'web',
|
|
276
|
+
os: req.get('X-OS') || 'web'
|
|
249
277
|
};
|
|
278
|
+
|
|
250
279
|
next();
|
|
251
280
|
});
|
|
252
281
|
|
|
@@ -263,7 +292,7 @@ class Server {
|
|
|
263
292
|
const listPublicRouter = controller.publicRouter();
|
|
264
293
|
|
|
265
294
|
_.map(listPublicRouter, (method) => {
|
|
266
|
-
const {
|
|
295
|
+
const {name, type} = method;
|
|
267
296
|
|
|
268
297
|
const router = controller[name]();
|
|
269
298
|
|
|
@@ -304,7 +333,7 @@ class Server {
|
|
|
304
333
|
this.callbackError(error);
|
|
305
334
|
}
|
|
306
335
|
|
|
307
|
-
return res.status(error.status).json({
|
|
336
|
+
return res.status(error.status).json({success: false, errors: error.errors});
|
|
308
337
|
});
|
|
309
338
|
|
|
310
339
|
const server = http.Server(app);
|
|
@@ -369,4 +398,4 @@ class Server {
|
|
|
369
398
|
}
|
|
370
399
|
}
|
|
371
400
|
|
|
372
|
-
export default Server;
|
|
401
|
+
export default Server;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export default function(key, ttl = 5) {
|
|
2
|
+
return async function(req, res, next) {
|
|
3
|
+
const {redis} = req;
|
|
4
|
+
if (redis) {
|
|
5
|
+
const cacheData = await redis.get(key);
|
|
6
|
+
if (cacheData) {
|
|
7
|
+
return res.json(cacheData);
|
|
8
|
+
} else {
|
|
9
|
+
res.cache = {
|
|
10
|
+
key,
|
|
11
|
+
ttl
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return next();
|
|
16
|
+
};
|
|
17
|
+
}
|
package/src/middleware/index.js
CHANGED
|
@@ -5,4 +5,5 @@ export {default as roleMiddleware} from './roleMiddleware';
|
|
|
5
5
|
export {default as paginateMiddleware} from './paginateMiddleware';
|
|
6
6
|
export {default as validateMiddleware} from './validateMiddleware';
|
|
7
7
|
export {default as limitRequestMiddleware} from './limitRequestMiddleware';
|
|
8
|
+
export {default as cacheMiddleware} from './cacheMiddleware';
|
|
8
9
|
|
|
@@ -36,7 +36,7 @@ export default function(options = {}) {
|
|
|
36
36
|
return async function(req, res, next) {
|
|
37
37
|
req.query = omitData(req.query);
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
res.resOptions = options;
|
|
40
40
|
req.paginate = {
|
|
41
41
|
limit: options.limit
|
|
42
42
|
};
|
|
@@ -71,14 +71,14 @@ export default function(options = {}) {
|
|
|
71
71
|
req.query[newKey] = {
|
|
72
72
|
...req.query[newKey],
|
|
73
73
|
$gte: value
|
|
74
|
-
}
|
|
74
|
+
};
|
|
75
75
|
} else if (key.endsWith('_end')) {
|
|
76
76
|
const newKey = key.replace('_end', '');
|
|
77
77
|
|
|
78
78
|
req.query[newKey] = {
|
|
79
79
|
...req.query[newKey],
|
|
80
80
|
$lt: value
|
|
81
|
-
}
|
|
81
|
+
};
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
|
|
@@ -102,5 +102,5 @@ export default function(options = {}) {
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
return next();
|
|
105
|
-
}
|
|
106
|
-
}
|
|
105
|
+
};
|
|
106
|
+
}
|