@azteam/express 1.2.124 → 1.2.127

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azteam/express",
3
- "version": "1.2.124",
3
+ "version": "1.2.127",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -1,8 +1,8 @@
1
- import { NOT_EXISTS } from '@azteam/error';
1
+ import {NOT_EXISTS} from '@azteam/error';
2
2
 
3
- import { REQUEST_TYPE, USER_LEVEL } from './constant';
4
- import { paginateMiddleware, roleMiddleware, validateMiddleware } from './middleware';
5
- import { rulesID } from './validate';
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
 
@@ -14,7 +14,8 @@ class AdminController extends Controller {
14
14
  CREATE: 2,
15
15
  UPDATE: 3,
16
16
  DELETE: 4,
17
- RESTORE: 5
17
+ RESTORE: 5,
18
+ DESTROY: 6
18
19
  }, // system roles
19
20
 
20
21
  paginateOptions: {},
@@ -28,9 +29,9 @@ class AdminController extends Controller {
28
29
  });
29
30
  }
30
31
 
31
- getPaginate() {
32
+ getPaginatePublic() {
32
33
  return {
33
- path: '/public',
34
+ path: '/',
34
35
  method: [
35
36
  roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
36
37
  paginateMiddleware(this.options.paginateOptions),
@@ -45,7 +46,7 @@ class AdminController extends Controller {
45
46
 
46
47
  getPaginateTrash() {
47
48
  return {
48
- path: '/trash',
49
+ path: '/',
49
50
  method: [
50
51
  roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
51
52
  paginateMiddleware(this.options.paginateOptions),
@@ -58,9 +59,9 @@ class AdminController extends Controller {
58
59
  };
59
60
  };
60
61
 
61
- getOne() {
62
+ getOnePubic() {
62
63
  return {
63
- path: '/public/:id',
64
+ path: '/:id',
64
65
  method: [
65
66
  roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
66
67
  validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
@@ -76,7 +77,7 @@ class AdminController extends Controller {
76
77
 
77
78
  getOneTrash() {
78
79
  return {
79
- path: '/trash/:id',
80
+ path: '/:id',
80
81
  method: [
81
82
  roleMiddleware([this.options.roles.READ], USER_LEVEL.ADMIN),
82
83
  validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
@@ -95,9 +96,9 @@ class AdminController extends Controller {
95
96
  return data;
96
97
  };
97
98
 
98
- postCreate() {
99
+ postCreatePublic() {
99
100
  return {
100
- path: '/public',
101
+ path: '/',
101
102
  method: [
102
103
  roleMiddleware([this.options.roles.CREATE], USER_LEVEL.ADMIN),
103
104
  validateMiddleware(REQUEST_TYPE.BODY, this.options.rulesCreate),
@@ -118,9 +119,9 @@ class AdminController extends Controller {
118
119
  return data;
119
120
  };
120
121
 
121
- putModify() {
122
+ putModifyPublic() {
122
123
  return {
123
- path: '/public/:id',
124
+ path: '/:id',
124
125
  method: [
125
126
  roleMiddleware([this.options.roles.UPDATE], USER_LEVEL.ADMIN),
126
127
  validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
@@ -140,9 +141,9 @@ class AdminController extends Controller {
140
141
  };
141
142
  };
142
143
 
143
- delete() {
144
+ deletePublic() {
144
145
  return {
145
- path: '/public/:id',
146
+ path: '/:id',
146
147
  method: [
147
148
  roleMiddleware([this.options.roles.DELETE], USER_LEVEL.ADMIN),
148
149
  validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
@@ -157,7 +158,7 @@ class AdminController extends Controller {
157
158
  };
158
159
  };
159
160
 
160
- postRestore() {
161
+ postRestoreTrash() {
161
162
  return {
162
163
  path: '/trash/:id',
163
164
  method: [
@@ -173,6 +174,23 @@ class AdminController extends Controller {
173
174
  ]
174
175
  };
175
176
  };
177
+
178
+ deleteDestroyTrash() {
179
+ return {
180
+ path: '/trash/:id',
181
+ method: [
182
+ roleMiddleware([this.options.roles.DESTROY], USER_LEVEL.ADMIN),
183
+ validateMiddleware(REQUEST_TYPE.PARAMS, rulesID),
184
+ async (req, res) => {
185
+ const item = await this.repository.findOneTrashById(req.params.id);
186
+ if (!item) return res.error(NOT_EXISTS);
187
+
188
+ await this.repository.destroy(item);
189
+ return res.success(true);
190
+ }
191
+ ]
192
+ };
193
+ };
176
194
  }
177
195
 
178
196
  export default AdminController;
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';
@@ -229,33 +230,31 @@ class ApiServer {
229
230
  });
230
231
 
231
232
  const msg = [];
232
- _.map(this.controllers, (obj) => {
233
+ _.map(this.controllers, (data) => {
234
+ const controller = data.controller;
235
+ const controllerName = data.name;
236
+ const controllerVersion = data.version;
233
237
 
234
- const {name, controller, version} = obj;
238
+ const listPublicRouter = controller.publicRouter();
235
239
 
236
- const listPublicMethod = obj.controller.publicMethod();
240
+ _.map(listPublicRouter, (method) => {
241
+ const {name, type} = method;
237
242
 
238
- _.map(listPublicMethod, (methodName) => {
243
+ const router = controller[name]();
239
244
 
240
- const matches = methodName.match(/get|post|put|patch|delete/);
241
- if (matches) {
242
- const type = matches[0];
243
- const item = controller[methodName]();
245
+ router.path = `/${method.path}/${router.path}`;
246
+ router.path = controller.pathName ? `/${controller.pathName}${router.path}` : router.path;
247
+ router.path = controllerVersion.startsWith('v') ? `/${controllerVersion}${router.path}` : router.path;
248
+ router.path = path.normalize(router.path);
244
249
 
245
- item.path = controller.pathName ? `/${controller.pathName}${item.path}` : item.path;
246
-
247
- item.path = version.startsWith('v') ? `/${version}${item.path}` : item.path;
248
-
249
- msg.push({
250
- controller: name,
251
- version,
252
- type,
253
- method: methodName,
254
- path: item.path
255
- });
256
-
257
- app[type](item.path, ...item.method);
258
- }
250
+ msg.push({
251
+ controller: controllerName,
252
+ version: controllerVersion,
253
+ type,
254
+ method: name,
255
+ path: router.path
256
+ });
257
+ app[type](router.path, ...router.method);
259
258
  });
260
259
  });
261
260
 
package/src/Controller.js CHANGED
@@ -8,7 +8,7 @@ class Controller {
8
8
  };
9
9
  }
10
10
 
11
- publicMethod() {
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
- result = [
20
- ...result,
21
- ...data.filter((methodName) => methodName.match(/^get|post|put|patch|delete/))
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;