@azteam/express 1.2.175 → 1.2.178
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 +127 -112
- package/src/Controller.js +1 -5
- package/src/Server.js +23 -25
package/package.json
CHANGED
package/src/AdminController.js
CHANGED
|
@@ -3,88 +3,82 @@ import {NOT_EXISTS} from '@azteam/error';
|
|
|
3
3
|
import {REQUEST_TYPE} from './constant';
|
|
4
4
|
import {rulesID} from './validate';
|
|
5
5
|
import Controller from './Controller';
|
|
6
|
-
import {adminRoleMiddleware, paginateMiddleware, validateMiddleware} from './middleware';
|
|
6
|
+
import {adminRoleMiddleware, paginateMiddleware, validateMiddleware, verifyGoogleAppMiddleware} from './middleware';
|
|
7
7
|
|
|
8
8
|
class AdminController extends Controller {
|
|
9
9
|
constructor(pathName, repository, options = {}) {
|
|
10
|
-
super(pathName, repository
|
|
11
|
-
roles: {
|
|
12
|
-
READ: 1,
|
|
13
|
-
CREATE: 2,
|
|
14
|
-
UPDATE: 3,
|
|
15
|
-
DELETE: 4,
|
|
16
|
-
RESTORE: 5,
|
|
17
|
-
DESTROY: 6,
|
|
18
|
-
}, // system roles
|
|
10
|
+
super(pathName, repository);
|
|
19
11
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
this.roles = {
|
|
13
|
+
READ: null,
|
|
14
|
+
CREATE: null,
|
|
15
|
+
UPDATE: null,
|
|
16
|
+
DELETE: null,
|
|
17
|
+
RESTORE: null,
|
|
18
|
+
DESTROY: null,
|
|
23
19
|
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
...options.roles,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
this.paginateOptions = options.paginateOptions || {};
|
|
24
|
+
this.guardResponse = options.guardResponse || {};
|
|
25
|
+
this.allowResponse = options.allowResponse || {};
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
}
|
|
27
|
+
this.rulesCreate = options.rulesCreate || {};
|
|
28
|
+
this.rulesModify = options.rulesModify || {};
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
methodGetPaginatePublic = async (req, res) => {
|
|
32
|
+
const paginateData = await this.repository.find(req.query, req.paginate);
|
|
33
|
+
return res.success(paginateData, this.guardResponse, this.allowResponse);
|
|
34
|
+
};
|
|
35
|
+
|
|
31
36
|
getPaginatePublic() {
|
|
32
37
|
return {
|
|
38
|
+
disabled: !this.roles.READ,
|
|
33
39
|
path: '/',
|
|
34
|
-
method: [
|
|
35
|
-
adminRoleMiddleware([this.options.roles.READ]),
|
|
36
|
-
paginateMiddleware(this.options.paginateOptions),
|
|
37
|
-
async (req, res) => {
|
|
38
|
-
const paginateData = await this.repository.find(req.query, req.paginate);
|
|
39
|
-
|
|
40
|
-
return res.success(paginateData, this.options.guardResponse, this.options.allowResponse);
|
|
41
|
-
},
|
|
42
|
-
],
|
|
40
|
+
method: [adminRoleMiddleware([this.roles.READ]), paginateMiddleware(this.paginateOptions), this.methodGetPaginatePublic],
|
|
43
41
|
};
|
|
44
42
|
}
|
|
45
43
|
|
|
44
|
+
methodGetPaginateTrash = async (req, res) => {
|
|
45
|
+
const paginateData = await this.repository.findTrash(req.query, req.paginate);
|
|
46
|
+
return res.success(paginateData, this.guardResponse, this.allowResponse);
|
|
47
|
+
};
|
|
48
|
+
|
|
46
49
|
getPaginateTrash() {
|
|
47
50
|
return {
|
|
51
|
+
disabled: !this.roles.READ,
|
|
48
52
|
path: '/',
|
|
49
|
-
method: [
|
|
50
|
-
adminRoleMiddleware([this.options.roles.READ]),
|
|
51
|
-
paginateMiddleware(this.options.paginateOptions),
|
|
52
|
-
async (req, res) => {
|
|
53
|
-
const paginateData = await this.repository.findTrash(req.query, req.paginate);
|
|
54
|
-
|
|
55
|
-
return res.success(paginateData, this.options.guardResponse, this.options.allowResponse);
|
|
56
|
-
},
|
|
57
|
-
],
|
|
53
|
+
method: [adminRoleMiddleware([this.roles.READ]), paginateMiddleware(this.paginateOptions), this.methodGetPaginateTrash],
|
|
58
54
|
};
|
|
59
55
|
}
|
|
60
56
|
|
|
57
|
+
methodGetOnePublic = async (req, res) => {
|
|
58
|
+
const item = await this.repository.findOneById(req.params.id);
|
|
59
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
60
|
+
return res.success(item, this.guardResponse, this.allowResponse);
|
|
61
|
+
};
|
|
62
|
+
|
|
61
63
|
getOnePublic() {
|
|
62
64
|
return {
|
|
65
|
+
disabled: !this.roles.READ,
|
|
63
66
|
path: '/:id',
|
|
64
|
-
method: [
|
|
65
|
-
adminRoleMiddleware([this.options.roles.READ]),
|
|
66
|
-
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
|
67
|
-
async (req, res) => {
|
|
68
|
-
const item = await this.repository.findOneById(req.params.id);
|
|
69
|
-
if (!item) return res.error(NOT_EXISTS);
|
|
70
|
-
return res.success(item, this.options.guardResponse, this.options.allowResponse);
|
|
71
|
-
},
|
|
72
|
-
],
|
|
67
|
+
method: [adminRoleMiddleware([this.roles.READ]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesID), this.methodGetOnePublic],
|
|
73
68
|
};
|
|
74
69
|
}
|
|
75
70
|
|
|
71
|
+
methodGetOneTrash = async (req, res) => {
|
|
72
|
+
const item = await this.repository.findOneTrashById(req.params.id);
|
|
73
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
74
|
+
return res.success(item, this.guardResponse, this.allowResponse);
|
|
75
|
+
};
|
|
76
|
+
|
|
76
77
|
getOneTrash() {
|
|
77
78
|
return {
|
|
79
|
+
disabled: !this.roles.READ,
|
|
78
80
|
path: '/:id',
|
|
79
|
-
method: [
|
|
80
|
-
adminRoleMiddleware([this.options.roles.READ]),
|
|
81
|
-
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
|
82
|
-
async (req, res) => {
|
|
83
|
-
const item = await this.repository.findOneTrashById(req.params.id);
|
|
84
|
-
if (!item) return res.error(NOT_EXISTS);
|
|
85
|
-
return res.success(item, this.options.guardResponse, this.options.allowResponse);
|
|
86
|
-
},
|
|
87
|
-
],
|
|
81
|
+
method: [adminRoleMiddleware([this.roles.READ]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesID), this.methodGetOneTrash],
|
|
88
82
|
};
|
|
89
83
|
}
|
|
90
84
|
|
|
@@ -96,22 +90,21 @@ class AdminController extends Controller {
|
|
|
96
90
|
return item;
|
|
97
91
|
}
|
|
98
92
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
path: '/',
|
|
102
|
-
method: [
|
|
103
|
-
adminRoleMiddleware([this.options.roles.CREATE]),
|
|
104
|
-
validateMiddleware(REQUEST_TYPE.BODY, this.options.rulesCreate),
|
|
105
|
-
async (req, res) => {
|
|
106
|
-
const data = await this.beforeCreate(req.body, req.user);
|
|
93
|
+
methodPostCreatePublic = async (req, res) => {
|
|
94
|
+
const data = await this.beforeCreate(req.body, req.user);
|
|
107
95
|
|
|
108
|
-
|
|
96
|
+
let item = await this.repository.createByUser(req.user.id, data);
|
|
109
97
|
|
|
110
|
-
|
|
98
|
+
item = await this.afterCreate(item, req.user);
|
|
111
99
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
100
|
+
return res.success(item, this.guardResponse, this.allowResponse);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
postCreatePublic() {
|
|
104
|
+
return {
|
|
105
|
+
disabled: !this.roles.CREATE,
|
|
106
|
+
path: '/',
|
|
107
|
+
method: [adminRoleMiddleware([this.roles.CREATE]), validateMiddleware(REQUEST_TYPE.BODY, this.rulesCreate), this.methodPostCreatePublic],
|
|
115
108
|
};
|
|
116
109
|
}
|
|
117
110
|
|
|
@@ -123,77 +116,99 @@ class AdminController extends Controller {
|
|
|
123
116
|
return item;
|
|
124
117
|
}
|
|
125
118
|
|
|
119
|
+
methodPutModifyPublic = async (req, res) => {
|
|
120
|
+
let item = await this.repository.findOneById(req.params.id);
|
|
121
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
122
|
+
|
|
123
|
+
const data = await this.beforeModify(req.body, req.user);
|
|
124
|
+
|
|
125
|
+
item = await this.repository.modifyByUser(req.user.id, item, data);
|
|
126
|
+
|
|
127
|
+
item = await this.afterModify(item, req.user);
|
|
128
|
+
|
|
129
|
+
return res.success(item, this.guardResponse, this.allowResponse);
|
|
130
|
+
};
|
|
131
|
+
|
|
126
132
|
putModifyPublic() {
|
|
127
133
|
return {
|
|
134
|
+
disabled: !this.roles.UPDATE,
|
|
128
135
|
path: '/:id',
|
|
129
136
|
method: [
|
|
130
|
-
adminRoleMiddleware([this.
|
|
137
|
+
adminRoleMiddleware([this.roles.UPDATE]),
|
|
131
138
|
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
|
132
|
-
validateMiddleware(REQUEST_TYPE.BODY, this.
|
|
133
|
-
|
|
134
|
-
let item = await this.repository.findOneById(req.params.id);
|
|
135
|
-
if (!item) return res.error(NOT_EXISTS);
|
|
136
|
-
|
|
137
|
-
const data = await this.beforeModify(req.body, req.user);
|
|
138
|
-
|
|
139
|
-
item = await this.repository.modifyByUser(req.user.id, item, data);
|
|
140
|
-
|
|
141
|
-
item = await this.afterModify(item, req.user);
|
|
142
|
-
|
|
143
|
-
return res.success(item, this.options.guardResponse, this.options.allowResponse);
|
|
144
|
-
},
|
|
139
|
+
validateMiddleware(REQUEST_TYPE.BODY, this.rulesModify),
|
|
140
|
+
this.methodPutModifyPublic,
|
|
145
141
|
],
|
|
146
142
|
};
|
|
147
143
|
}
|
|
148
144
|
|
|
145
|
+
methodDeletePublic = async (req, res) => {
|
|
146
|
+
const item = await this.repository.findOneById(req.params.id);
|
|
147
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
148
|
+
|
|
149
|
+
await this.repository.deleteByUser(req.user.id, item);
|
|
150
|
+
return res.success(true);
|
|
151
|
+
};
|
|
152
|
+
|
|
149
153
|
deletePublic() {
|
|
150
154
|
return {
|
|
155
|
+
disabled: !this.roles.DELETE,
|
|
151
156
|
path: '/:id',
|
|
152
|
-
method: [
|
|
153
|
-
adminRoleMiddleware([this.options.roles.DELETE]),
|
|
154
|
-
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
|
155
|
-
async (req, res) => {
|
|
156
|
-
const item = await this.repository.findOneById(req.params.id);
|
|
157
|
-
if (!item) return res.error(NOT_EXISTS);
|
|
158
|
-
|
|
159
|
-
await this.repository.deleteByUser(req.user.id, item);
|
|
160
|
-
return res.success(true);
|
|
161
|
-
},
|
|
162
|
-
],
|
|
157
|
+
method: [adminRoleMiddleware([this.roles.DELETE]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesID), this.methodDeletePublic],
|
|
163
158
|
};
|
|
164
159
|
}
|
|
165
160
|
|
|
161
|
+
methodPostRestoreTrash = async (req, res) => {
|
|
162
|
+
const item = await this.repository.findOneTrashById(req.params.id);
|
|
163
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
164
|
+
|
|
165
|
+
await this.repository.restoreByUser(req.user.id, item);
|
|
166
|
+
return res.success(true);
|
|
167
|
+
};
|
|
168
|
+
|
|
166
169
|
postRestoreTrash() {
|
|
167
170
|
return {
|
|
171
|
+
disabled: !this.roles.RESTORE,
|
|
168
172
|
path: '/:id',
|
|
169
|
-
method: [
|
|
170
|
-
adminRoleMiddleware([this.options.roles.RESTORE]),
|
|
171
|
-
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
|
172
|
-
async (req, res) => {
|
|
173
|
-
const item = await this.repository.findOneTrashById(req.params.id);
|
|
174
|
-
if (!item) return res.error(NOT_EXISTS);
|
|
175
|
-
|
|
176
|
-
await this.repository.restoreByUser(req.user.id, item);
|
|
177
|
-
return res.success(true);
|
|
178
|
-
},
|
|
179
|
-
],
|
|
173
|
+
method: [adminRoleMiddleware([this.roles.RESTORE]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesID), this.methodPostRestoreTrash],
|
|
180
174
|
};
|
|
181
175
|
}
|
|
182
176
|
|
|
177
|
+
methodDeleteDestroyTrash = async (req, res) => {
|
|
178
|
+
const item = await this.repository.findOneTrashById(req.params.id);
|
|
179
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
180
|
+
|
|
181
|
+
await this.repository.destroy(item);
|
|
182
|
+
return res.success(true);
|
|
183
|
+
};
|
|
184
|
+
|
|
183
185
|
deleteDestroyTrash() {
|
|
184
186
|
return {
|
|
187
|
+
disabled: !this.roles.DESTROY,
|
|
185
188
|
path: '/:id',
|
|
186
|
-
method: [
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
async (req, res) => {
|
|
190
|
-
const item = await this.repository.findOneTrashById(req.params.id);
|
|
191
|
-
if (!item) return res.error(NOT_EXISTS);
|
|
189
|
+
method: [adminRoleMiddleware([this.roles.DESTROY]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesID), this.methodDeleteDestroyTrash],
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
192
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
193
|
+
methodDPostImportSheet = async (req, res) => {
|
|
194
|
+
return res.success(true);
|
|
195
|
+
};
|
|
196
|
+
postImportSheet() {
|
|
197
|
+
return {
|
|
198
|
+
disabled: !this.roles.CREATE,
|
|
199
|
+
path: '/',
|
|
200
|
+
method: [adminRoleMiddleware([this.roles.CREATE]), verifyGoogleAppMiddleware(), this.methodDPostImportSheet],
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
methodPostExportSheet = async (req, res) => {
|
|
205
|
+
return res.success(true);
|
|
206
|
+
};
|
|
207
|
+
postExportSheet() {
|
|
208
|
+
return {
|
|
209
|
+
disabled: !this.roles.READ,
|
|
210
|
+
path: '/',
|
|
211
|
+
method: [adminRoleMiddleware([this.roles.READ]), verifyGoogleAppMiddleware(), this.methodPostExportSheet],
|
|
197
212
|
};
|
|
198
213
|
}
|
|
199
214
|
}
|
package/src/Controller.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
class Controller {
|
|
2
|
-
constructor(pathName = '', repository = null
|
|
2
|
+
constructor(pathName = '', repository = null) {
|
|
3
3
|
this.pathName = pathName;
|
|
4
4
|
this.repository = repository;
|
|
5
|
-
|
|
6
|
-
this.options = {
|
|
7
|
-
...options,
|
|
8
|
-
};
|
|
9
5
|
}
|
|
10
6
|
|
|
11
7
|
publicRouter() {
|
package/src/Server.js
CHANGED
|
@@ -254,7 +254,7 @@ class Server {
|
|
|
254
254
|
|
|
255
255
|
const msg = [];
|
|
256
256
|
_.map(this.controllers, (data) => {
|
|
257
|
-
const controller = data
|
|
257
|
+
const {controller} = data;
|
|
258
258
|
const controllerName = data.name;
|
|
259
259
|
const controllerVersion = data.version;
|
|
260
260
|
|
|
@@ -264,20 +264,21 @@ class Server {
|
|
|
264
264
|
const {name, type} = method;
|
|
265
265
|
|
|
266
266
|
const router = controller[name]();
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
267
|
+
if (!router.disabled) {
|
|
268
|
+
router.path = `/${method.path}/${router.path}`;
|
|
269
|
+
router.path = controller.pathName ? `/${controller.pathName}${router.path}` : router.path;
|
|
270
|
+
router.path = controllerVersion.startsWith('v') ? `/${controllerVersion}${router.path}` : router.path;
|
|
271
|
+
router.path = path.normalize(router.path);
|
|
272
|
+
|
|
273
|
+
msg.push({
|
|
274
|
+
controller: controllerName,
|
|
275
|
+
version: controllerVersion,
|
|
276
|
+
type,
|
|
277
|
+
method: name,
|
|
278
|
+
path: router.path,
|
|
279
|
+
});
|
|
280
|
+
app[type](router.path, ...router.method);
|
|
281
|
+
}
|
|
281
282
|
});
|
|
282
283
|
});
|
|
283
284
|
|
|
@@ -291,11 +292,11 @@ class Server {
|
|
|
291
292
|
throw new ErrorException(NOT_FOUND);
|
|
292
293
|
});
|
|
293
294
|
|
|
294
|
-
app.use((
|
|
295
|
-
const error = errorCatch(
|
|
295
|
+
app.use((err, req, res, next) => {
|
|
296
|
+
const error = errorCatch(err);
|
|
296
297
|
|
|
297
298
|
if (error.errors[0].code === UNKNOWN) {
|
|
298
|
-
console.error(req.originalUrl,
|
|
299
|
+
console.error(req.originalUrl, err);
|
|
299
300
|
}
|
|
300
301
|
|
|
301
302
|
if (this.callbackError) {
|
|
@@ -316,7 +317,7 @@ class Server {
|
|
|
316
317
|
throw error;
|
|
317
318
|
}
|
|
318
319
|
|
|
319
|
-
|
|
320
|
+
const bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port}`;
|
|
320
321
|
|
|
321
322
|
switch (error.code) {
|
|
322
323
|
case 'EACCES':
|
|
@@ -335,18 +336,15 @@ class Server {
|
|
|
335
336
|
server.listen(port);
|
|
336
337
|
|
|
337
338
|
return server;
|
|
338
|
-
} else {
|
|
339
|
-
throw Error('No controllers in use API');
|
|
340
339
|
}
|
|
341
|
-
|
|
340
|
+
throw Error('No controllers in use API');
|
|
342
341
|
}
|
|
343
342
|
|
|
344
343
|
startSocket(port) {
|
|
345
344
|
if (!_.isEmpty(this.controllers)) {
|
|
346
|
-
|
|
347
|
-
throw Error('No controllers in use SOCKET');
|
|
345
|
+
return this;
|
|
348
346
|
}
|
|
349
|
-
|
|
347
|
+
throw Error('No controllers in use SOCKET');
|
|
350
348
|
}
|
|
351
349
|
|
|
352
350
|
setAlertCallback(callback) {
|