@azteam/express 1.2.127 → 1.2.128

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 +54 -30
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azteam/express",
3
- "version": "1.2.127",
3
+ "version": "1.2.128",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "repository": {
package/src/ApiServer.js CHANGED
@@ -12,16 +12,32 @@ import _ from 'lodash';
12
12
  import 'express-async-errors';
13
13
  import {errorCatch, ErrorException, NOT_FOUND, UNKNOWN} from '@azteam/error';
14
14
 
15
- function omitItem(item, guard) {
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
+ }
27
+
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;
@@ -160,46 +176,54 @@ class ApiServer {
160
176
 
161
177
  res.success = function(data = {}, guard = [], allows = []) {
162
178
  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) {
179
+ let resType = RES_TYPE.ARRAY;
180
+ if (_.isObject(data)) {
181
+ resType = RES_TYPE.OBJECT;
182
+ if (data.docs) {
183
+ resType = RES_TYPE.DOCS;
184
+ }
185
+ }
186
+
187
+ if (_.isArray(guard)) {
174
188
  guard = [
175
189
  ...guard,
176
- 'metadata_disable',
177
- 'metadata_keywords',
178
- 'metadata_description',
179
- 'metadata_image_url'
190
+ '__v',
191
+ '_id',
192
+ 'deleted_at',
193
+ 'updated_at',
194
+ 'created_id',
195
+ 'modified_id'
180
196
  ];
197
+ if (resType === RES_TYPE.ARRAY || resType === RES_TYPE.DOCS) {
198
+ guard = [
199
+ ...guard,
200
+ 'metadata_disable',
201
+ 'metadata_keywords',
202
+ 'metadata_description',
203
+ 'metadata_image_url'
204
+ ];
205
+ }
181
206
  }
182
207
 
183
- guard = _.difference(guard, allows);
184
-
185
- if (_.isArray(data)) {
186
- data = _.map(data, item => {
187
- return omitItem(item, guard);
208
+ let guardData = data;
209
+ if (resType === RES_TYPE.DOCS) {
210
+ guardData.docs = _.map(data.docs, item => {
211
+ return omitItem(item, guard, allows);
188
212
  });
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
- }
213
+ } else if (resType === RES_TYPE.ARRAY) {
214
+ guardData = _.map(data, item => {
215
+ return omitItem(item, guard, allows);
216
+ });
217
+ } else if (resType === RES_TYPE.OBJECT) {
218
+ guardData = omitItem(data, guard, allows);
197
219
  }
220
+
221
+
198
222
  }
199
223
 
200
224
  return res.json({
201
225
  success: true,
202
- data,
226
+ data: guardData,
203
227
  options: req.resOptions
204
228
  });
205
229
  };