@azteam/express 1.2.125 → 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.
- package/package.json +1 -1
- package/src/AdminController.js +19 -19
- package/src/ApiServer.js +75 -52
- package/src/Controller.js +17 -5
package/package.json
CHANGED
package/src/AdminController.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {NOT_EXISTS} from '@azteam/error';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import {REQUEST_TYPE, USER_LEVEL} from './constant';
|
|
4
|
+
import {paginateMiddleware, roleMiddleware, validateMiddleware} from './middleware';
|
|
5
|
+
import {rulesID} from './validate';
|
|
6
6
|
import Controller from './Controller';
|
|
7
7
|
|
|
8
8
|
|
|
@@ -29,9 +29,9 @@ class AdminController extends Controller {
|
|
|
29
29
|
});
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
getPaginatePublic() {
|
|
33
33
|
return {
|
|
34
|
-
path: '/
|
|
34
|
+
path: '/',
|
|
35
35
|
method: [
|
|
36
36
|
roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
|
|
37
37
|
paginateMiddleware(this.options.paginateOptions),
|
|
@@ -46,7 +46,7 @@ class AdminController extends Controller {
|
|
|
46
46
|
|
|
47
47
|
getPaginateTrash() {
|
|
48
48
|
return {
|
|
49
|
-
path: '/
|
|
49
|
+
path: '/',
|
|
50
50
|
method: [
|
|
51
51
|
roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
|
|
52
52
|
paginateMiddleware(this.options.paginateOptions),
|
|
@@ -59,9 +59,9 @@ class AdminController extends Controller {
|
|
|
59
59
|
};
|
|
60
60
|
};
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
getOnePubic() {
|
|
63
63
|
return {
|
|
64
|
-
path: '
|
|
64
|
+
path: '/:id',
|
|
65
65
|
method: [
|
|
66
66
|
roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
|
|
67
67
|
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
|
@@ -77,7 +77,7 @@ class AdminController extends Controller {
|
|
|
77
77
|
|
|
78
78
|
getOneTrash() {
|
|
79
79
|
return {
|
|
80
|
-
path: '
|
|
80
|
+
path: '/:id',
|
|
81
81
|
method: [
|
|
82
82
|
roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
|
|
83
83
|
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
|
@@ -96,9 +96,9 @@ class AdminController extends Controller {
|
|
|
96
96
|
return data;
|
|
97
97
|
};
|
|
98
98
|
|
|
99
|
-
|
|
99
|
+
postCreatePublic() {
|
|
100
100
|
return {
|
|
101
|
-
path: '/
|
|
101
|
+
path: '/',
|
|
102
102
|
method: [
|
|
103
103
|
roleMiddleware([this.options.roles.CREATE], USER_LEVEL.ADMIN),
|
|
104
104
|
validateMiddleware(REQUEST_TYPE.BODY, this.options.rulesCreate),
|
|
@@ -119,9 +119,9 @@ class AdminController extends Controller {
|
|
|
119
119
|
return data;
|
|
120
120
|
};
|
|
121
121
|
|
|
122
|
-
|
|
122
|
+
putModifyPublic() {
|
|
123
123
|
return {
|
|
124
|
-
path: '
|
|
124
|
+
path: '/:id',
|
|
125
125
|
method: [
|
|
126
126
|
roleMiddleware([this.options.roles.UPDATE], USER_LEVEL.ADMIN),
|
|
127
127
|
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
|
@@ -141,9 +141,9 @@ class AdminController extends Controller {
|
|
|
141
141
|
};
|
|
142
142
|
};
|
|
143
143
|
|
|
144
|
-
|
|
144
|
+
deletePublic() {
|
|
145
145
|
return {
|
|
146
|
-
path: '
|
|
146
|
+
path: '/:id',
|
|
147
147
|
method: [
|
|
148
148
|
roleMiddleware([this.options.roles.DELETE], USER_LEVEL.ADMIN),
|
|
149
149
|
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
|
@@ -158,7 +158,7 @@ class AdminController extends Controller {
|
|
|
158
158
|
};
|
|
159
159
|
};
|
|
160
160
|
|
|
161
|
-
|
|
161
|
+
postRestoreTrash() {
|
|
162
162
|
return {
|
|
163
163
|
path: '/trash/:id',
|
|
164
164
|
method: [
|
|
@@ -175,9 +175,9 @@ class AdminController extends Controller {
|
|
|
175
175
|
};
|
|
176
176
|
};
|
|
177
177
|
|
|
178
|
-
|
|
178
|
+
deleteDestroyTrash() {
|
|
179
179
|
return {
|
|
180
|
-
path: '/
|
|
180
|
+
path: '/trash/:id',
|
|
181
181
|
method: [
|
|
182
182
|
roleMiddleware([this.options.roles.DESTROY], USER_LEVEL.ADMIN),
|
|
183
183
|
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
package/src/ApiServer.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
2
3
|
import http from 'http';
|
|
3
4
|
import express from 'express';
|
|
4
5
|
import helmet from 'helmet';
|
|
@@ -11,16 +12,32 @@ import _ from 'lodash';
|
|
|
11
12
|
import 'express-async-errors';
|
|
12
13
|
import {errorCatch, ErrorException, NOT_FOUND, UNKNOWN} from '@azteam/error';
|
|
13
14
|
|
|
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
|
+
}
|
|
27
|
+
|
|
15
28
|
if (item.toJSON) {
|
|
16
29
|
item = item.toJSON();
|
|
17
30
|
}
|
|
18
31
|
if (_.isObject(item)) {
|
|
32
|
+
if (guard === '*') {
|
|
33
|
+
return _.pick(item, allows);
|
|
34
|
+
}
|
|
19
35
|
return _.omit(item, guard);
|
|
20
36
|
}
|
|
21
37
|
return item;
|
|
22
38
|
}
|
|
23
39
|
|
|
40
|
+
|
|
24
41
|
class ApiServer {
|
|
25
42
|
constructor(currentDir = '', options = {}) {
|
|
26
43
|
this.options = options;
|
|
@@ -159,46 +176,54 @@ class ApiServer {
|
|
|
159
176
|
|
|
160
177
|
res.success = function(data = {}, guard = [], allows = []) {
|
|
161
178
|
if (data) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
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)) {
|
|
173
188
|
guard = [
|
|
174
189
|
...guard,
|
|
175
|
-
'
|
|
176
|
-
'
|
|
177
|
-
'
|
|
178
|
-
'
|
|
190
|
+
'__v',
|
|
191
|
+
'_id',
|
|
192
|
+
'deleted_at',
|
|
193
|
+
'updated_at',
|
|
194
|
+
'created_id',
|
|
195
|
+
'modified_id'
|
|
179
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
|
+
}
|
|
180
206
|
}
|
|
181
207
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
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);
|
|
187
212
|
});
|
|
188
|
-
} else if (
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
data = omitItem(data, guard);
|
|
195
|
-
}
|
|
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);
|
|
196
219
|
}
|
|
220
|
+
|
|
221
|
+
|
|
197
222
|
}
|
|
198
223
|
|
|
199
224
|
return res.json({
|
|
200
225
|
success: true,
|
|
201
|
-
data,
|
|
226
|
+
data: guardData,
|
|
202
227
|
options: req.resOptions
|
|
203
228
|
});
|
|
204
229
|
};
|
|
@@ -229,33 +254,31 @@ class ApiServer {
|
|
|
229
254
|
});
|
|
230
255
|
|
|
231
256
|
const msg = [];
|
|
232
|
-
_.map(this.controllers, (
|
|
257
|
+
_.map(this.controllers, (data) => {
|
|
258
|
+
const controller = data.controller;
|
|
259
|
+
const controllerName = data.name;
|
|
260
|
+
const controllerVersion = data.version;
|
|
233
261
|
|
|
234
|
-
const
|
|
262
|
+
const listPublicRouter = controller.publicRouter();
|
|
235
263
|
|
|
236
|
-
|
|
264
|
+
_.map(listPublicRouter, (method) => {
|
|
265
|
+
const {name, type} = method;
|
|
237
266
|
|
|
238
|
-
|
|
267
|
+
const router = controller[name]();
|
|
239
268
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
269
|
+
router.path = `/${method.path}/${router.path}`;
|
|
270
|
+
router.path = controller.pathName ? `/${controller.pathName}${router.path}` : router.path;
|
|
271
|
+
router.path = controllerVersion.startsWith('v') ? `/${controllerVersion}${router.path}` : router.path;
|
|
272
|
+
router.path = path.normalize(router.path);
|
|
244
273
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
method: methodName,
|
|
254
|
-
path: item.path
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
app[type](item.path, ...item.method);
|
|
258
|
-
}
|
|
274
|
+
msg.push({
|
|
275
|
+
controller: controllerName,
|
|
276
|
+
version: controllerVersion,
|
|
277
|
+
type,
|
|
278
|
+
method: name,
|
|
279
|
+
path: router.path
|
|
280
|
+
});
|
|
281
|
+
app[type](router.path, ...router.method);
|
|
259
282
|
});
|
|
260
283
|
});
|
|
261
284
|
|
package/src/Controller.js
CHANGED
|
@@ -8,7 +8,7 @@ class Controller {
|
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
publicRouter() {
|
|
12
12
|
let child = this;
|
|
13
13
|
let result = [];
|
|
14
14
|
|
|
@@ -16,11 +16,23 @@ class Controller {
|
|
|
16
16
|
|
|
17
17
|
const data = Object.getOwnPropertyNames(Object.getPrototypeOf(child));
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
data.map((methodName) => {
|
|
20
|
+
const matches = methodName.match(/get|post|put|patch|delete/);
|
|
21
|
+
if (matches) {
|
|
22
|
+
let path = '/';
|
|
23
|
+
if (methodName.match(/Public/)) {
|
|
24
|
+
path += 'public';
|
|
25
|
+
} else if (methodName.match(/Trash/)) {
|
|
26
|
+
path += 'trash';
|
|
27
|
+
}
|
|
23
28
|
|
|
29
|
+
result.push({
|
|
30
|
+
type: matches[0],
|
|
31
|
+
name: methodName,
|
|
32
|
+
path
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
});
|
|
24
36
|
child = child.__proto__;
|
|
25
37
|
}
|
|
26
38
|
return result;
|