@azteam/express 1.2.126 → 1.2.129

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/ApiServer.js +62 -39
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azteam/express",
3
- "version": "1.2.126",
3
+ "version": "1.2.129",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "repository": {
package/src/ApiServer.js CHANGED
@@ -10,18 +10,34 @@ 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
+
15
+
16
+ const RES_TYPE = {
17
+ ARRAY: 'ARRAY',
18
+ OBJECT: 'OBJECT',
19
+ DOCS: 'DOCS'
20
+ };
21
+
22
+ function omitItem(item, guard, allows) {
23
+
24
+ if (_.isArray(guard)) {
25
+ guard = _.difference(guard, allows);
26
+ }
14
27
 
15
- function omitItem(item, guard) {
16
28
  if (item.toJSON) {
17
29
  item = item.toJSON();
18
30
  }
19
31
  if (_.isObject(item)) {
32
+ if (guard === '*') {
33
+ return _.pick(item, allows);
34
+ }
20
35
  return _.omit(item, guard);
21
36
  }
22
37
  return item;
23
38
  }
24
39
 
40
+
25
41
  class ApiServer {
26
42
  constructor(currentDir = '', options = {}) {
27
43
  this.options = options;
@@ -114,8 +130,8 @@ class ApiServer {
114
130
  }));
115
131
 
116
132
  app.use(methodOverride());
117
- app.use(bodyParser.urlencoded({limit: '5mb', extended: true}));
118
- app.use(bodyParser.json({limit: '5mb'}));
133
+ app.use(bodyParser.urlencoded({ limit: '5mb', extended: true }));
134
+ app.use(bodyParser.json({ limit: '5mb' }));
119
135
 
120
136
  app.set('trust proxy', 1);
121
137
 
@@ -159,47 +175,54 @@ class ApiServer {
159
175
  };
160
176
 
161
177
  res.success = function(data = {}, guard = [], allows = []) {
178
+ let guardData = data;
162
179
  if (data) {
163
- guard = [
164
- ...guard,
165
- '__v',
166
- '_id',
167
- 'deleted_at',
168
- 'updated_at',
169
- 'created_id',
170
- 'modified_id'
171
- ];
172
-
173
- if (_.isArray(data) || data.docs) {
180
+ let resType = RES_TYPE.ARRAY;
181
+ if (_.isObject(data)) {
182
+ resType = RES_TYPE.OBJECT;
183
+ if (data.docs) {
184
+ resType = RES_TYPE.DOCS;
185
+ }
186
+ }
187
+
188
+ if (_.isArray(guard)) {
174
189
  guard = [
175
190
  ...guard,
176
- 'metadata_disable',
177
- 'metadata_keywords',
178
- 'metadata_description',
179
- 'metadata_image_url'
191
+ '__v',
192
+ '_id',
193
+ 'deleted_at',
194
+ 'updated_at',
195
+ 'created_id',
196
+ 'modified_id'
180
197
  ];
198
+ if (resType === RES_TYPE.ARRAY || resType === RES_TYPE.DOCS) {
199
+ guard = [
200
+ ...guard,
201
+ 'metadata_disable',
202
+ 'metadata_keywords',
203
+ 'metadata_description',
204
+ 'metadata_image_url'
205
+ ];
206
+ }
181
207
  }
182
-
183
- guard = _.difference(guard, allows);
184
-
185
- if (_.isArray(data)) {
186
- data = _.map(data, item => {
187
- return omitItem(item, guard);
208
+ if (resType === RES_TYPE.DOCS) {
209
+ guardData.docs = _.map(data.docs, item => {
210
+ return omitItem(item, guard, allows);
188
211
  });
189
- } else if (_.isObject(data)) {
190
- if (data.docs) {
191
- data.docs = _.map(data.docs, item => {
192
- return omitItem(item, guard);
193
- });
194
- } else {
195
- data = omitItem(data, guard);
196
- }
212
+ } else if (resType === RES_TYPE.ARRAY) {
213
+ guardData = _.map(data, item => {
214
+ return omitItem(item, guard, allows);
215
+ });
216
+ } else if (resType === RES_TYPE.OBJECT) {
217
+ guardData = omitItem(data, guard, allows);
197
218
  }
219
+
220
+
198
221
  }
199
222
 
200
223
  return res.json({
201
224
  success: true,
202
- data,
225
+ data: guardData,
203
226
  options: req.resOptions
204
227
  });
205
228
  };
@@ -238,22 +261,22 @@ class ApiServer {
238
261
  const listPublicRouter = controller.publicRouter();
239
262
 
240
263
  _.map(listPublicRouter, (method) => {
241
- const {name, type} = method;
264
+ const { name, type } = method;
242
265
 
243
266
  const router = controller[name]();
244
267
 
245
268
  router.path = `/${method.path}/${router.path}`;
246
269
  router.path = controller.pathName ? `/${controller.pathName}${router.path}` : router.path;
247
270
  router.path = controllerVersion.startsWith('v') ? `/${controllerVersion}${router.path}` : router.path;
271
+ router.path = path.normalize(router.path);
248
272
 
249
273
  msg.push({
250
274
  controller: controllerName,
251
275
  version: controllerVersion,
252
276
  type,
253
277
  method: name,
254
- path: path.resolve(router.path)
278
+ path: router.path
255
279
  });
256
-
257
280
  app[type](router.path, ...router.method);
258
281
  });
259
282
  });
@@ -279,7 +302,7 @@ class ApiServer {
279
302
  this.callbackError(error);
280
303
  }
281
304
 
282
- return res.status(error.status).json({success: false, errors: error.errors});
305
+ return res.status(error.status).json({ success: false, errors: error.errors });
283
306
  });
284
307
 
285
308
  const server = http.Server(app);
@@ -335,4 +358,4 @@ class ApiServer {
335
358
  }
336
359
  }
337
360
 
338
- export default ApiServer;
361
+ export default ApiServer;