@azteam/express 1.2.114 → 1.2.117
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 +179 -0
- package/src/constant.js +7 -0
- package/src/index.js +6 -0
- package/src/validate.js +132 -0
package/package.json
CHANGED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { NOT_EXISTS, PERMISSION } from '@azteam/error';
|
|
2
|
+
|
|
3
|
+
import { HTTP_METHOD, paginateMiddleware, REQUEST_TYPE, roleMiddleware, validateMiddleware, rulesID } from './ApiServer';
|
|
4
|
+
import { USER_LEVEL } from './constant';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Controller {
|
|
8
|
+
constructor(pathName, repository, options = {}) {
|
|
9
|
+
this.pathName = pathName;
|
|
10
|
+
this.repository = repository;
|
|
11
|
+
|
|
12
|
+
this.options = {
|
|
13
|
+
roles: {
|
|
14
|
+
READ: 1,
|
|
15
|
+
CREATE: 2,
|
|
16
|
+
UPDATE: 3,
|
|
17
|
+
DELETE: 4,
|
|
18
|
+
RESTORE: 5,
|
|
19
|
+
}, // system roles
|
|
20
|
+
|
|
21
|
+
rulesCreate: {},
|
|
22
|
+
rulesModify: {},
|
|
23
|
+
|
|
24
|
+
guardResponse: {},
|
|
25
|
+
allowResponse: {},
|
|
26
|
+
|
|
27
|
+
...options,
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getPaginate() {
|
|
32
|
+
return {
|
|
33
|
+
path: `/${this.pathName}`,
|
|
34
|
+
method: [
|
|
35
|
+
roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
|
|
36
|
+
paginateMiddleware(paginateOptions),
|
|
37
|
+
async function(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
|
+
],
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
getPaginateTrash() {
|
|
47
|
+
return {
|
|
48
|
+
path: `/trash_${this.pathName}`,
|
|
49
|
+
method: [
|
|
50
|
+
roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
|
|
51
|
+
paginateMiddleware(paginateOptions),
|
|
52
|
+
async function(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
|
+
],
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
getOne() {
|
|
62
|
+
return {
|
|
63
|
+
path: `/${this.pathName}/:id`,
|
|
64
|
+
method: [
|
|
65
|
+
roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
|
|
66
|
+
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
|
67
|
+
async function(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
|
+
],
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
getOneTrash() {
|
|
78
|
+
return {
|
|
79
|
+
path: `/trash_${this.pathName}/:id`,
|
|
80
|
+
method: [
|
|
81
|
+
roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
|
|
82
|
+
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
|
83
|
+
async function(req, res) {
|
|
84
|
+
const item = await this.repository.findOneTrashById(req.params.id);
|
|
85
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
86
|
+
return res.success(item, this.options.guardResponse, this.options.allowResponse);
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
beforePostCreate(data) {
|
|
95
|
+
return data;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
postCreate() {
|
|
99
|
+
return {
|
|
100
|
+
path: `/${this.pathName}`,
|
|
101
|
+
method: [
|
|
102
|
+
roleMiddleware([this.options.roles.CREATE], USER_LEVEL.ADMIN),
|
|
103
|
+
validateMiddleware(REQUEST_TYPE.BODY, this.options.rulesCreate),
|
|
104
|
+
async function(req, res) {
|
|
105
|
+
|
|
106
|
+
const data = this.beforePostCreate(req.body);
|
|
107
|
+
|
|
108
|
+
const item = await this.repository.createByUser(req.user.id, data);
|
|
109
|
+
|
|
110
|
+
return res.success(item, this.options.guardResponse, this.options.allowResponse);
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
beforePutModify(item) {
|
|
118
|
+
return item;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
putModify() {
|
|
122
|
+
return {
|
|
123
|
+
path: `/${this.pathName}/:id`,
|
|
124
|
+
method: [
|
|
125
|
+
roleMiddleware([this.options.roles.UPDATE], USER_LEVEL.ADMIN),
|
|
126
|
+
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
|
127
|
+
validateMiddleware(REQUEST_TYPE.BODY, this.options.rulesModify),
|
|
128
|
+
async function(req, res) {
|
|
129
|
+
|
|
130
|
+
let item = await this.options.roles.findOneById(req.params.id);
|
|
131
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
132
|
+
|
|
133
|
+
item = await this.beforePutModify(item);
|
|
134
|
+
|
|
135
|
+
item = await this.options.roles.modifyByUser(req.user.id, item, req.body);
|
|
136
|
+
|
|
137
|
+
return res.success(item, this.options.guardResponse, this.options.allowResponse);
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
delete() {
|
|
144
|
+
return {
|
|
145
|
+
path: `/${this.pathName}/:id`,
|
|
146
|
+
method: [
|
|
147
|
+
roleMiddleware([this.options.roles.DELETE], USER_LEVEL.ADMIN),
|
|
148
|
+
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
|
149
|
+
async function(req, res) {
|
|
150
|
+
const item = await this.repository.findOneById(req.params.id);
|
|
151
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
152
|
+
|
|
153
|
+
await this.repository.deleteByUser(req.user.id, item);
|
|
154
|
+
return res.success(true);
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
postRestore() {
|
|
161
|
+
return {
|
|
162
|
+
path: `/trash_${this.pathName}/:id`,
|
|
163
|
+
method: [
|
|
164
|
+
roleMiddleware([this.options.roles.RESTORE], USER_LEVEL.ADMIN),
|
|
165
|
+
validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
|
|
166
|
+
async function(req, res) {
|
|
167
|
+
const item = await this.repository.findOneTrashById(req.params.id);
|
|
168
|
+
if (!item) return res.error(NOT_EXISTS);
|
|
169
|
+
|
|
170
|
+
await this.repository.restoreByUser(req.user.id, item);
|
|
171
|
+
return res.success(true);
|
|
172
|
+
},
|
|
173
|
+
],
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
},
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export default Controller;
|
package/src/constant.js
CHANGED
package/src/index.js
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import ApiServer from './ApiServer';
|
|
2
2
|
import SocketServer from './SocketServer';
|
|
3
3
|
|
|
4
|
+
import AdminController from './AdminController';
|
|
5
|
+
|
|
6
|
+
|
|
4
7
|
export * from './middleware';
|
|
5
8
|
export * from './constant';
|
|
9
|
+
export * from './validate';
|
|
6
10
|
|
|
7
11
|
export {
|
|
8
12
|
ApiServer,
|
|
9
13
|
SocketServer,
|
|
14
|
+
|
|
15
|
+
AdminController,
|
|
10
16
|
}
|
package/src/validate.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
export const schemaID = (optional = false) => ({
|
|
2
|
+
type: 'string',
|
|
3
|
+
length: 24,
|
|
4
|
+
pattern: /^[A-Fa-f0-9]{24}$/,
|
|
5
|
+
optional,
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
export const schemaEmail = (optional = false) => ({
|
|
9
|
+
type: 'email',
|
|
10
|
+
optional,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export const schemaUrl = (optional = false) => ({
|
|
14
|
+
type: 'url',
|
|
15
|
+
optional,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export const schemaBoolean = (optional = false) => ({
|
|
19
|
+
type: 'boolean',
|
|
20
|
+
convert: true,
|
|
21
|
+
optional,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export const schemaDate = (optional = false) => ({
|
|
25
|
+
type: 'number',
|
|
26
|
+
convert: true,
|
|
27
|
+
integer: true,
|
|
28
|
+
optional,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export const schemaNumber = (optional = false) => ({
|
|
32
|
+
type: 'number',
|
|
33
|
+
convert: true,
|
|
34
|
+
optional,
|
|
35
|
+
});
|
|
36
|
+
export const schemaInteger = (min = 0, max = 999999999, optional = false) => ({
|
|
37
|
+
type: 'number',
|
|
38
|
+
integer: true,
|
|
39
|
+
convert: true,
|
|
40
|
+
min,
|
|
41
|
+
max,
|
|
42
|
+
optional,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
export const schemaString = (min = 1, max = 255, optional = false) => ({
|
|
46
|
+
type: 'string',
|
|
47
|
+
min,
|
|
48
|
+
max,
|
|
49
|
+
optional,
|
|
50
|
+
});
|
|
51
|
+
export const schemaPassword = (optional = false) => ({
|
|
52
|
+
type: 'string',
|
|
53
|
+
min: 6,
|
|
54
|
+
max: 32,
|
|
55
|
+
optional,
|
|
56
|
+
});
|
|
57
|
+
export const schemaOTP = (optional = false) => ({
|
|
58
|
+
type: 'string',
|
|
59
|
+
length: 6,
|
|
60
|
+
pattern: /\d+/,
|
|
61
|
+
optional,
|
|
62
|
+
});
|
|
63
|
+
export const schemaImage = (optional = false) => ({
|
|
64
|
+
type: 'string',
|
|
65
|
+
max: 255,
|
|
66
|
+
optional,
|
|
67
|
+
});
|
|
68
|
+
export const schemaVideo = (optional = false) => ({
|
|
69
|
+
type: 'string',
|
|
70
|
+
max: 255,
|
|
71
|
+
optional,
|
|
72
|
+
});
|
|
73
|
+
export const schemaPhoneNumber = (optional = false) => ({
|
|
74
|
+
type: 'string',
|
|
75
|
+
length: 10,
|
|
76
|
+
pattern: /^((09|03|07|08|05)+([0-9]{8})\b)$/,
|
|
77
|
+
optional,
|
|
78
|
+
});
|
|
79
|
+
export const schemaSlug = (optional = false) => ({
|
|
80
|
+
type: 'string',
|
|
81
|
+
pattern: /[A-Za-z0-9_-]+/,
|
|
82
|
+
max: 300,
|
|
83
|
+
optional,
|
|
84
|
+
});
|
|
85
|
+
export const schemaVersion = (optional = false) => ({
|
|
86
|
+
type: 'string',
|
|
87
|
+
pattern: /^(\d+)\.(\d+)\.(\d+)$/,
|
|
88
|
+
max: 300,
|
|
89
|
+
optional,
|
|
90
|
+
});
|
|
91
|
+
export const schemaJSON = (optional = false) => ({
|
|
92
|
+
type: 'json',
|
|
93
|
+
optional,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
export const schemaGallery = (optional = false) => ({
|
|
97
|
+
type: 'array',
|
|
98
|
+
items: schemaImage(),
|
|
99
|
+
optional,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
export const schemaObject = (props = {}, optional = false) => ({
|
|
103
|
+
type: 'object',
|
|
104
|
+
strict: 'remove',
|
|
105
|
+
props,
|
|
106
|
+
optional,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
/* rules */
|
|
111
|
+
|
|
112
|
+
export const rulesID = {
|
|
113
|
+
id: schemaID(),
|
|
114
|
+
};
|
|
115
|
+
export const rulesSlug = {
|
|
116
|
+
slug: schemaSlug(),
|
|
117
|
+
};
|
|
118
|
+
export const rulesPlatformType = {
|
|
119
|
+
type: {
|
|
120
|
+
type: 'enum',
|
|
121
|
+
values: ['web', 'app'],
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
export const rulesMetadata = {
|
|
125
|
+
metadata_disable: schemaBoolean(true),
|
|
126
|
+
metadata_title: schemaString(0, 255, true),
|
|
127
|
+
metadata_title_og: schemaString(0, 255, true),
|
|
128
|
+
metadata_description: schemaString(0, 255, true),
|
|
129
|
+
metadata_description_og: schemaString(0, 255, true),
|
|
130
|
+
metadata_keywords: schemaString(0, 255, true),
|
|
131
|
+
metadata_image_url: schemaImage(true),
|
|
132
|
+
};
|