@azteam/express 1.2.281 → 1.2.283
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 +4 -4
- package/src/AdminController.js +311 -0
- package/src/Controller.js +38 -0
- package/src/Server.js +409 -0
- package/src/SocketServer.js +186 -0
- package/src/constant.js +12 -0
- package/src/index.js +12 -0
- package/src/middleware/adminRoleMiddleware.js +7 -0
- package/src/middleware/authMiddleware.js +53 -0
- package/src/middleware/cacheMiddleware.js +37 -0
- package/src/middleware/etagMiddleware.js +20 -0
- package/src/middleware/index.js +11 -0
- package/src/middleware/limitRequestMiddleware.js +18 -0
- package/src/middleware/paginateMiddleware.js +128 -0
- package/src/middleware/roleMiddleware.js +16 -0
- package/src/middleware/signMiddleware.js +14 -0
- package/src/middleware/systemRoleMiddleware.js +7 -0
- package/src/middleware/validateMiddleware.js +58 -0
- package/src/middleware/verifyGoogleAppMiddleware.js +12 -0
- package/src/validate.js +161 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azteam/express",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.283",
|
|
4
4
|
"main": "./lib/index.js",
|
|
5
5
|
"module": "./src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
"author": "toda <sp.azsolution.net@gmail.com>",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@azteam/crypto": "1.0.
|
|
13
|
-
"@azteam/error": "1.0.
|
|
14
|
-
"@azteam/http-client": "1.0.
|
|
12
|
+
"@azteam/crypto": "1.0.27",
|
|
13
|
+
"@azteam/error": "1.0.29",
|
|
14
|
+
"@azteam/http-client": "1.0.103",
|
|
15
15
|
"@grpc/grpc-js": "1.6.7",
|
|
16
16
|
"@grpc/proto-loader": "0.6.12",
|
|
17
17
|
"body-parser": "1.19.0",
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import {NOT_EXISTS} from '@azteam/error';
|
|
3
|
+
|
|
4
|
+
import {REQUEST_TYPE} from './constant';
|
|
5
|
+
import {rulesId, schemaBoolean, schemaEnum, schemaNumber} from './validate';
|
|
6
|
+
import {adminRoleMiddleware, paginateMiddleware, validateMiddleware} from './middleware';
|
|
7
|
+
import Controller from './Controller';
|
|
8
|
+
|
|
9
|
+
const ALLOW_FIELDS = ['created_id', 'modified_id', 'restored_id', 'deleted_id', 'resource'];
|
|
10
|
+
|
|
11
|
+
class AdminController extends Controller {
|
|
12
|
+
constructor(pathName, repository, options = {}) {
|
|
13
|
+
super(pathName, repository);
|
|
14
|
+
|
|
15
|
+
this.roles = {
|
|
16
|
+
EXEC: null,
|
|
17
|
+
READ: null,
|
|
18
|
+
CREATE: null,
|
|
19
|
+
UPDATE: null,
|
|
20
|
+
DELETE: null,
|
|
21
|
+
RESTORE: null,
|
|
22
|
+
DESTROY: null,
|
|
23
|
+
IMPORT: null,
|
|
24
|
+
EXPORT: null,
|
|
25
|
+
|
|
26
|
+
...options.roles,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
this.paginateOptions = options.paginateOptions || {};
|
|
30
|
+
this.guardResponse = options.guardResponse || [];
|
|
31
|
+
this.allowResponse = options.allowResponse ? [...options.allowResponse, ...ALLOW_FIELDS] : ALLOW_FIELDS;
|
|
32
|
+
|
|
33
|
+
this.rulesCreate = {
|
|
34
|
+
...options.rulesCreate,
|
|
35
|
+
priority: schemaNumber(true),
|
|
36
|
+
status: schemaEnum([0, 1, 2], true),
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
this.rulesModify = {
|
|
40
|
+
isForceModify: schemaBoolean(true),
|
|
41
|
+
..._.mapValues(this.rulesCreate, function (obj) {
|
|
42
|
+
return {...obj, optional: true};
|
|
43
|
+
}),
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
if (this.paginateOptions) {
|
|
47
|
+
this.paginateOptions.allowSearchFields = [
|
|
48
|
+
...this.paginateOptions.allowSearchFields,
|
|
49
|
+
'status',
|
|
50
|
+
'created_at_start',
|
|
51
|
+
'created_at_end',
|
|
52
|
+
'modified_at_start',
|
|
53
|
+
'modified_at_end',
|
|
54
|
+
];
|
|
55
|
+
this.paginateOptions.allowSortFields = [...this.paginateOptions.allowSortFields, 'created_at', 'modified_at', 'status'];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
methodGetPaginatePublic = async (req, res) => {
|
|
60
|
+
const paginateData = await this.repository.find(req.query, req.paginate);
|
|
61
|
+
return res.success(paginateData, this.guardResponse, this.allowResponse);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
getPaginatePublic() {
|
|
65
|
+
return {
|
|
66
|
+
disabled: !this.roles.READ,
|
|
67
|
+
path: '/',
|
|
68
|
+
method: [adminRoleMiddleware([this.roles.READ]), paginateMiddleware(this.paginateOptions), this.methodGetPaginatePublic],
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
methodGetPaginateTrash = async (req, res) => {
|
|
73
|
+
const paginateData = await this.repository.findTrash(req.query, req.paginate);
|
|
74
|
+
return res.success(paginateData, this.guardResponse, this.allowResponse);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
getPaginateTrash() {
|
|
78
|
+
return {
|
|
79
|
+
disabled: !this.roles.READ,
|
|
80
|
+
path: '/',
|
|
81
|
+
method: [adminRoleMiddleware([this.roles.READ]), paginateMiddleware(this.paginateOptions), this.methodGetPaginateTrash],
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
methodGetOnePublic = async (req, res) => {
|
|
86
|
+
const item = await this.repository.findOneById(req.params.id);
|
|
87
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
88
|
+
return res.success(item, this.guardResponse, this.allowResponse);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
getOnePublic() {
|
|
92
|
+
return {
|
|
93
|
+
disabled: !this.roles.READ,
|
|
94
|
+
path: '/:id',
|
|
95
|
+
method: [adminRoleMiddleware([this.roles.READ]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesId), this.methodGetOnePublic],
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
methodGetOneTrash = async (req, res) => {
|
|
100
|
+
const item = await this.repository.findOneTrashById(req.params.id);
|
|
101
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
102
|
+
return res.success(item, this.guardResponse, this.allowResponse);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
getOneTrash() {
|
|
106
|
+
return {
|
|
107
|
+
disabled: !this.roles.READ,
|
|
108
|
+
path: '/:id',
|
|
109
|
+
method: [adminRoleMiddleware([this.roles.READ]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesId), this.methodGetOneTrash],
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async beforeCreate(data) {
|
|
114
|
+
return data;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async afterCreate(item) {
|
|
118
|
+
return item;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
methodPostCreatePublic = async (req, res) => {
|
|
122
|
+
const data = await this.beforeCreate(req.body);
|
|
123
|
+
|
|
124
|
+
let item = await this.repository.createByUser(req.user.id, data);
|
|
125
|
+
|
|
126
|
+
item = await this.afterCreate(item);
|
|
127
|
+
|
|
128
|
+
return res.success(item, this.guardResponse, this.allowResponse);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
postCreatePublic() {
|
|
132
|
+
return {
|
|
133
|
+
disabled: !this.roles.CREATE,
|
|
134
|
+
path: '/',
|
|
135
|
+
method: [adminRoleMiddleware([this.roles.CREATE]), validateMiddleware(REQUEST_TYPE.BODY, this.rulesCreate), this.methodPostCreatePublic],
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async beforeModify(data) {
|
|
140
|
+
return data;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async afterModify(item) {
|
|
144
|
+
return item;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
methodPutModifyPublic = async (req, res) => {
|
|
148
|
+
let item = await this.repository.findOneById(req.params.id);
|
|
149
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
150
|
+
|
|
151
|
+
const data = await this.beforeModify(req.body);
|
|
152
|
+
|
|
153
|
+
item.loadData(data);
|
|
154
|
+
item.modified_id = req.user.id;
|
|
155
|
+
await item.save();
|
|
156
|
+
|
|
157
|
+
item = await this.afterModify(item);
|
|
158
|
+
|
|
159
|
+
return res.success(item, this.guardResponse, this.allowResponse);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
putModifyPublic() {
|
|
163
|
+
return {
|
|
164
|
+
disabled: !this.roles.UPDATE,
|
|
165
|
+
path: '/:id',
|
|
166
|
+
method: [
|
|
167
|
+
adminRoleMiddleware([this.roles.UPDATE]),
|
|
168
|
+
validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
|
|
169
|
+
validateMiddleware(REQUEST_TYPE.BODY, this.rulesModify),
|
|
170
|
+
this.methodPutModifyPublic,
|
|
171
|
+
],
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
putModifyStatusAvailablePublic() {
|
|
176
|
+
return {
|
|
177
|
+
disabled: !this.roles.UPDATE,
|
|
178
|
+
path: '/available/:id',
|
|
179
|
+
method: [
|
|
180
|
+
adminRoleMiddleware([this.roles.UPDATE]),
|
|
181
|
+
validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
|
|
182
|
+
async (req, res) => {
|
|
183
|
+
let item = await this.repository.findOneById(req.params.id);
|
|
184
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
185
|
+
|
|
186
|
+
await item.modifyStatusAvailable(req.user.id);
|
|
187
|
+
|
|
188
|
+
item = await this.afterModify(item);
|
|
189
|
+
|
|
190
|
+
return res.success(item, this.guardResponse, this.allowResponse);
|
|
191
|
+
},
|
|
192
|
+
],
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
putModifyStatusUnavailablePublic() {
|
|
197
|
+
return {
|
|
198
|
+
disabled: !this.roles.UPDATE,
|
|
199
|
+
path: '/unavailable/:id',
|
|
200
|
+
method: [
|
|
201
|
+
adminRoleMiddleware([this.roles.UPDATE]),
|
|
202
|
+
validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
|
|
203
|
+
async (req, res) => {
|
|
204
|
+
let item = await this.repository.findOneById(req.params.id);
|
|
205
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
206
|
+
|
|
207
|
+
await item.modifyStatusUnavailable(req.body.message, req.user.id);
|
|
208
|
+
|
|
209
|
+
item = await this.afterModify(item);
|
|
210
|
+
|
|
211
|
+
return res.success(item, this.guardResponse, this.allowResponse);
|
|
212
|
+
},
|
|
213
|
+
],
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
putModifyStatusWaitingPublic() {
|
|
218
|
+
return {
|
|
219
|
+
disabled: !this.roles.UPDATE,
|
|
220
|
+
path: '/waiting/:id',
|
|
221
|
+
method: [
|
|
222
|
+
adminRoleMiddleware([this.roles.UPDATE]),
|
|
223
|
+
validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
|
|
224
|
+
async (req, res) => {
|
|
225
|
+
let item = await this.repository.findOneById(req.params.id);
|
|
226
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
227
|
+
|
|
228
|
+
await item.modifyStatusWaiting(req.user.id);
|
|
229
|
+
|
|
230
|
+
item = await this.afterModify(item);
|
|
231
|
+
|
|
232
|
+
return res.success(item, this.guardResponse, this.allowResponse);
|
|
233
|
+
},
|
|
234
|
+
],
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async afterDelete(item) {
|
|
239
|
+
return item;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
methodDeletePublic = async (req, res) => {
|
|
243
|
+
const item = await this.repository.findOneById(req.params.id);
|
|
244
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
245
|
+
|
|
246
|
+
await item.delete(req.user.id);
|
|
247
|
+
|
|
248
|
+
await this.afterDelete(item);
|
|
249
|
+
|
|
250
|
+
return res.success(true);
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
deletePublic() {
|
|
254
|
+
return {
|
|
255
|
+
disabled: !this.roles.DELETE,
|
|
256
|
+
path: '/:id',
|
|
257
|
+
method: [adminRoleMiddleware([this.roles.DELETE]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesId), this.methodDeletePublic],
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
methodPostRestoreTrash = async (req, res) => {
|
|
262
|
+
const item = await this.repository.findOneTrashById(req.params.id);
|
|
263
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
264
|
+
|
|
265
|
+
await item.restore(req.user.id);
|
|
266
|
+
|
|
267
|
+
return res.success(true);
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
postRestoreTrash() {
|
|
271
|
+
return {
|
|
272
|
+
disabled: !this.roles.RESTORE,
|
|
273
|
+
path: '/:id',
|
|
274
|
+
method: [adminRoleMiddleware([this.roles.RESTORE]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesId), this.methodPostRestoreTrash],
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async afterDestroy(item) {
|
|
279
|
+
return item;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
methodDeleteDestroyTrash = async (req, res) => {
|
|
283
|
+
const item = await this.repository.findOne(
|
|
284
|
+
{
|
|
285
|
+
_id: req.params.id,
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
force: true,
|
|
289
|
+
}
|
|
290
|
+
);
|
|
291
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
292
|
+
|
|
293
|
+
const oldItem = _.cloneDeep(item);
|
|
294
|
+
|
|
295
|
+
await this.repository.destroy(item.id);
|
|
296
|
+
|
|
297
|
+
await this.afterDestroy(oldItem);
|
|
298
|
+
|
|
299
|
+
return res.success(true);
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
deleteDestroyTrash() {
|
|
303
|
+
return {
|
|
304
|
+
disabled: !this.roles.DESTROY,
|
|
305
|
+
path: '/:id',
|
|
306
|
+
method: [adminRoleMiddleware([this.roles.DESTROY]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesId), this.methodDeleteDestroyTrash],
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export default AdminController;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
class Controller {
|
|
2
|
+
constructor(pathName = '', repository = null) {
|
|
3
|
+
this.pathName = pathName;
|
|
4
|
+
this.repository = repository;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
publicRouter() {
|
|
8
|
+
let child = this;
|
|
9
|
+
const result = [];
|
|
10
|
+
|
|
11
|
+
while (Object.getPrototypeOf(child.__proto__)) {
|
|
12
|
+
const data = Object.getOwnPropertyNames(Object.getPrototypeOf(child));
|
|
13
|
+
|
|
14
|
+
data.map(function (methodName) {
|
|
15
|
+
const matches = methodName.match(/^(get|post|put|patch|delete)/);
|
|
16
|
+
if (matches) {
|
|
17
|
+
let path = '/';
|
|
18
|
+
if (methodName.endsWith('Public')) {
|
|
19
|
+
path += 'public';
|
|
20
|
+
} else if (methodName.endsWith('Trash')) {
|
|
21
|
+
path += 'trash';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
result.push({
|
|
25
|
+
type: matches[0],
|
|
26
|
+
name: methodName,
|
|
27
|
+
path,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
});
|
|
32
|
+
child = child.__proto__;
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default Controller;
|