@azteam/express 1.2.127 → 1.2.130
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 +2 -2
- package/src/ApiServer.js +60 -37
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azteam/express",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.130",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@azteam/crypto": "1.0.17",
|
|
27
27
|
"@azteam/error": "1.0.15",
|
|
28
|
-
"@azteam/http-client": "1.0.
|
|
28
|
+
"@azteam/http-client": "1.0.81",
|
|
29
29
|
"body-parser": "1.19.0",
|
|
30
30
|
"cookie-parser": "1.4.4",
|
|
31
31
|
"cors": "2.8.4",
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
'
|
|
177
|
-
'
|
|
178
|
-
'
|
|
179
|
-
'
|
|
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
|
-
|
|
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 (
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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,7 +261,7 @@ 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
|
|
|
@@ -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;
|