@stonyx/rest-server 0.2.1-alpha.2 → 0.2.1-alpha.4

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
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.2.1-alpha.2",
7
+ "version": "0.2.1-alpha.4",
8
8
  "description": "Rest Server Module for Stonyx Framework",
9
9
  "repository": {
10
10
  "type": "git",
@@ -27,10 +27,10 @@
27
27
  "dependencies": {
28
28
  "cors": "^2.8.5",
29
29
  "express": "^5.1.0",
30
- "stonyx": "0.2.3-beta.2"
30
+ "stonyx": "0.2.3-beta.4"
31
31
  },
32
32
  "devDependencies": {
33
- "@stonyx/utils": "0.2.3-beta.3",
33
+ "@stonyx/utils": "0.2.3-beta.4",
34
34
  "qunit": "^2.24.1",
35
35
  "sinon": "^21.0.0"
36
36
  },
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, authorization } = classInstance;
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, ...routeCalls);
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
@@ -64,7 +59,11 @@ export default class Request {
64
59
 
65
60
  if (response === undefined) response = await mainCall(req, getState(req));
66
61
  if (Number.isInteger(response)) return sendStatusResponse(res, response);
67
-
62
+
63
+ // Handle redirect if set via call state object
64
+ const { redirect } = getState(req);
65
+ if (redirect) return res.redirect(redirect);
66
+
68
67
  // Handle pipe if set via call state object
69
68
  const { pipe } = getState(req);
70
69
  if (pipe) {