@azteam/express 1.2.149 → 1.2.152

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.149",
3
+ "version": "1.2.152",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -27,7 +27,7 @@
27
27
  "@azteam/error": "latest",
28
28
  "@azteam/http-client": "latest",
29
29
  "@azteam/rabbitmq-async": "latest",
30
- "@azteam/redis-async": "latest",
30
+ "@azteam/redis-async": "1.0.48",
31
31
  "@grpc/grpc-js": "1.6.7",
32
32
  "@grpc/proto-loader": "0.6.12",
33
33
  "body-parser": "1.19.0",
@@ -41,6 +41,7 @@
41
41
  "fastest-validator": "1.8.0",
42
42
  "helmet": "3.21.2",
43
43
  "jsonwebtoken": "8.2.1",
44
+ "lodash": "4.17.21",
44
45
  "method-override": "3.0.0",
45
46
  "morgan": "1.10.0",
46
47
  "socket.io": "4.4.1",
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 { errorCatch, ErrorException, NOT_FOUND, UNKNOWN } from '@azteam/error';
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 = {
@@ -62,10 +65,12 @@ class Server {
62
65
 
63
66
  setMessageQueue(messageQueue) {
64
67
  this.messageQueue = messageQueue;
68
+ return this;
65
69
  }
66
70
 
67
71
  setRedis(redis) {
68
72
  this.redis = redis;
73
+ return this;
69
74
  }
70
75
 
71
76
  setCookieOptions(cookieOptions) {
@@ -125,6 +130,7 @@ class Server {
125
130
 
126
131
 
127
132
  startAPI(port) {
133
+
128
134
  if (!_.isEmpty(this.controllers)) {
129
135
 
130
136
  const WHITE_LIST = this.whiteList;
@@ -137,8 +143,8 @@ class Server {
137
143
  }));
138
144
 
139
145
  app.use(methodOverride());
140
- app.use(bodyParser.urlencoded({ limit: '5mb', extended: true }));
141
- app.use(bodyParser.json({ limit: '5mb' }));
146
+ app.use(bodyParser.urlencoded({limit: '5mb', extended: true}));
147
+ app.use(bodyParser.json({limit: '5mb'}));
142
148
 
143
149
  app.set('trust proxy', 1);
144
150
 
@@ -168,92 +174,108 @@ class Server {
168
174
  });
169
175
  app.get('/favicon.ico', (req, res) => res.status(204).json({}));
170
176
 
171
- app.use(async function(req, res, next) {
172
- req.trackDevice = {
173
- ip: req.ip,
174
- device: req.get('X-DEVICE') || req.get('User-Agent'),
175
- device_id: req.get('X-DEVICE-ID') || 'web',
176
- os: req.get('X-OS') || 'web'
177
- };
178
177
 
178
+ const {redis} = this;
179
+
180
+ if (redis) {
181
+ app.request.redis = redis;
182
+ app.response.redis = redis;
183
+ }
179
184
 
180
- res.error = function(code, errors = []) {
181
- throw new ErrorException(code, errors);
182
- };
183
185
 
184
- res.success = function(data = {}, guard = [], allows = []) {
185
- let guardData = data;
186
- if (data) {
187
- let resType = null;
188
- if (_.isArray(data)) {
189
- resType = RES_TYPE.ARRAY;
190
- } else if (_.isObject(data)) {
191
- resType = RES_TYPE.OBJECT;
192
- if (data.docs) {
193
- resType = RES_TYPE.DOCS;
194
- }
186
+ app.response.error = function(code, errors = []) {
187
+ throw new ErrorException(code, errors);
188
+ };
189
+
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;
195
201
  }
202
+ }
196
203
 
197
- if (_.isArray(guard)) {
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) {
198
215
  guard = [
199
216
  ...guard,
200
- '__v',
201
- '_id',
202
- 'deleted_at',
203
- 'updated_at',
204
- 'created_id',
205
- 'modified_id'
217
+ 'metadata_disable',
218
+ 'metadata_keywords',
219
+ 'metadata_description',
220
+ 'metadata_image_url'
206
221
  ];
207
- if (resType === RES_TYPE.ARRAY || resType === RES_TYPE.DOCS) {
208
- guard = [
209
- ...guard,
210
- 'metadata_disable',
211
- 'metadata_keywords',
212
- 'metadata_description',
213
- 'metadata_image_url'
214
- ];
215
- }
216
222
  }
217
- if (resType === RES_TYPE.DOCS) {
218
- guardData.docs = _.map(data.docs, item => {
219
- return omitItem(item, guard, allows);
220
- });
221
- } else if (resType === RES_TYPE.ARRAY) {
222
- guardData = _.map(data, item => {
223
- return omitItem(item, guard, allows);
224
- });
225
- } else if (resType === RES_TYPE.OBJECT) {
226
- guardData = omitItem(data, guard, allows);
227
- }
228
-
229
-
230
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
+ }
231
236
 
232
- return res.json({
233
- success: true,
234
- data: guardData,
235
- options: req.resOptions
236
- });
237
+ const resData = {
238
+ success: true,
239
+ data: guardData,
240
+ options: this.resOptions
237
241
  };
238
242
 
239
- res.cleanCookie = function(data) {
240
- _.map(data, (name) => {
241
- res.clearCookie(name, {
242
- domain: COOKIE_OPTIONS.domain
243
- });
244
- });
245
- };
243
+ if (this.redis && this.cache) {
244
+ this.redis.set(this.cache.key, resData, this.cache.ttl);
245
+ }
246
246
 
247
- res.addCookie = function(data) {
248
- _.map(data, (value, key) => {
249
- const maxAge = 86400000 * 365; // 1 year
250
- res.cookie(key, value, {
251
- ...COOKIE_OPTIONS,
252
- maxAge,
253
- expires: new Date(Date.now() + maxAge)
254
- });
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
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)
255
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'
256
277
  };
278
+
257
279
  next();
258
280
  });
259
281
 
@@ -270,7 +292,7 @@ class Server {
270
292
  const listPublicRouter = controller.publicRouter();
271
293
 
272
294
  _.map(listPublicRouter, (method) => {
273
- const { name, type } = method;
295
+ const {name, type} = method;
274
296
 
275
297
  const router = controller[name]();
276
298
 
@@ -311,7 +333,7 @@ class Server {
311
333
  this.callbackError(error);
312
334
  }
313
335
 
314
- return res.status(error.status).json({ success: false, errors: error.errors });
336
+ return res.status(error.status).json({success: false, errors: error.errors});
315
337
  });
316
338
 
317
339
  const server = http.Server(app);
@@ -376,4 +398,4 @@ class Server {
376
398
  }
377
399
  }
378
400
 
379
- export default Server;
401
+ export default Server;
@@ -0,0 +1,35 @@
1
+ import _ from 'lodash';
2
+
3
+ export default function(key, options = {}) {
4
+ options = {
5
+ ttl: 5,
6
+ limitPage: 5,
7
+ ...options
8
+ };
9
+ return async function(req, res, next) {
10
+ const {redis} = req;
11
+ if (redis) {
12
+ if (options.query) {
13
+ key += _.get(req, options.query);
14
+ }
15
+ if (req.paginate) ;
16
+ {
17
+ if (req.paginate.page <= options.limitPage) {
18
+ key += `page${req.paginate.page}`;
19
+ } else {
20
+ return next();
21
+ }
22
+ }
23
+ const cacheData = await redis.get(key);
24
+ if (cacheData) {
25
+ return res.json(cacheData);
26
+ } else {
27
+ res.cache = {
28
+ key,
29
+ ttl: options.ttl
30
+ };
31
+ }
32
+ }
33
+ return next();
34
+ };
35
+ }
@@ -1,7 +1,5 @@
1
1
  import etag from 'etag';
2
2
 
3
-
4
-
5
3
  function floorToMinute(time, minutes) {
6
4
  const roundSecond = minutes * 60;
7
5
  time = time - (time % (Math.floor(time / roundSecond) * roundSecond));
@@ -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
- req.resOptions = options;
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
+ }