@momsfriendlydevco/cowboy 1.0.1 → 1.0.3

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/lib/cowboy.js CHANGED
@@ -19,6 +19,20 @@ export class Cowboy {
19
19
  * @property {Array<CowboyMiddleware>} [middleware] Middleware / resolvers to use for the route, should it match
20
20
  */
21
21
 
22
+
23
+ /**
24
+ * General settings for this Cowboy instance
25
+ *
26
+ * @type {Object}
27
+ * @property {Function} pathTidy Additional tidyup for server request paths, useful if the API does not live at the server root. Defaults to removing a "/api/:worker/" prefix
28
+ */
29
+ settings = {
30
+ pathTidy(path) {
31
+ return path.replace(/^\/api\/\w+/, '/');
32
+ },
33
+ };
34
+
35
+
22
36
  /**
23
37
  * List of middleware which will be called on all matching routes
24
38
  * @type Array<CowboyMiddleware>
@@ -93,7 +107,10 @@ export class Cowboy {
93
107
  }
94
108
 
95
109
  // Create basic [req]uest / [res]ponse objects
96
- let req = new CowboyRequest(cfReq, {router: this});
110
+ let req = new CowboyRequest(cfReq, {
111
+ router: this,
112
+ pathTidy: this.settings.pathTidy,
113
+ });
97
114
  let res = new CowboyResponse();
98
115
 
99
116
  // Exec all earlyMiddleware - every time
package/lib/request.js CHANGED
@@ -33,7 +33,7 @@ export default class CowboyRequest {
33
33
 
34
34
  // Break appart the incoming URL
35
35
  let url = new URL(cfReq.url);
36
- this.path = url.pathname;
36
+ this.path = this.pathTidy(url.pathname);
37
37
  this.hostname = url.hostname;
38
38
  }
39
39
  }
package/lib/response.js CHANGED
@@ -66,7 +66,11 @@ export default class CowboyResponse {
66
66
  */
67
67
  status(code) {
68
68
  this.code = code;
69
- if (!this.body) this.body = 'ok'; // Set body payload if we don't already have one
69
+ if (!this.body)
70
+ this.body = this.code >= 200 && this.code <= 299
71
+ ? 'ok' // Set body payload if we don't already have one
72
+ : `${this.code}: Fail`
73
+
70
74
  return this;
71
75
  }
72
76
 
@@ -11,8 +11,11 @@ export default function CowboyMiddlewareCORS(headers) {
11
11
  res.set(injectHeaders);
12
12
 
13
13
  // Handle hits to OPTIONS '/' endpoint
14
- req.router.options('/', (req, res) => {
15
- return res.sendStatus(200);
16
- });
14
+ if (!req.router.loadedCors) {
15
+ req.router.options('/', (req, res) => {
16
+ return res.sendStatus(200);
17
+ });
18
+ req.router.loadedCors = true; // Mark we've already done this so we don't keep tweaking the router
19
+ }
17
20
  }
18
21
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momsfriendlydevco/cowboy",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Wrapper around Cloudflare Wrangler to provide a more Express-like experience",
5
5
  "scripts": {
6
6
  "lint": "eslint ."