@stonyx/rest-server 0.2.1-beta.11 → 0.2.1-beta.12
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/main.js +3 -8
- package/src/request.js +8 -13
package/package.json
CHANGED
package/src/main.js
CHANGED
|
@@ -75,17 +75,12 @@ export default class RestServer {
|
|
|
75
75
|
const { api } = this;
|
|
76
76
|
const classInstance = new routeClass(options);
|
|
77
77
|
const route = name === 'index' ? '/' : `/${name}`;
|
|
78
|
-
const { expressInstance
|
|
79
|
-
|
|
80
|
-
const routeCalls = [ expressInstance ];
|
|
81
|
-
|
|
82
|
-
// Assign auth callback if it exists
|
|
83
|
-
if (authorization) routeCalls.unshift(authorization.bind(classInstance));
|
|
78
|
+
const { expressInstance } = classInstance;
|
|
84
79
|
|
|
85
80
|
classInstance.registerCalls();
|
|
86
81
|
expressInstance.mountpath = route;
|
|
87
|
-
|
|
82
|
+
|
|
88
83
|
// Mount handler to main api instance
|
|
89
|
-
api.use(route,
|
|
84
|
+
api.use(route, expressInstance);
|
|
90
85
|
}
|
|
91
86
|
}
|
package/src/request.js
CHANGED
|
@@ -29,20 +29,10 @@ export default class Request {
|
|
|
29
29
|
this.expressInstance = api;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
// auth hook wrapper
|
|
33
|
-
authorization(req, res, next) {
|
|
34
|
-
if (!this.auth) return next();
|
|
35
|
-
|
|
36
|
-
const status = this.auth(req, Request.getState(req));
|
|
37
|
-
if (status) return Request.sendStatusResponse(res, status);
|
|
38
|
-
|
|
39
|
-
next();
|
|
40
|
-
}
|
|
41
|
-
|
|
42
32
|
registerCalls() {
|
|
43
33
|
const { expressInstance } = this;
|
|
44
|
-
const { getState } = Request;
|
|
45
|
-
|
|
34
|
+
const { getState, sendStatusResponse } = Request;
|
|
35
|
+
|
|
46
36
|
for (const [method, handlers] of Object.entries(this.handlers)) {
|
|
47
37
|
if (!METHODS.has(method)) {
|
|
48
38
|
console.warn(`Method "${method}" is not a valid HTTP method. Skipping...`);
|
|
@@ -51,9 +41,14 @@ export default class Request {
|
|
|
51
41
|
|
|
52
42
|
for (const [route, handler] of Object.entries(handlers)) {
|
|
53
43
|
expressInstance[method](route, async (req, res) => {
|
|
44
|
+
// Run auth after route matching so request.params is populated
|
|
45
|
+
if (this.auth) {
|
|
46
|
+
const status = this.auth(req, getState(req));
|
|
47
|
+
if (status) return sendStatusResponse(res, status);
|
|
48
|
+
}
|
|
49
|
+
|
|
54
50
|
const callStack = [...makeArray(handler)];
|
|
55
51
|
const mainCall = callStack.pop();
|
|
56
|
-
const { sendStatusResponse } = Request;
|
|
57
52
|
let response;
|
|
58
53
|
|
|
59
54
|
// Run middleware
|