@momsfriendlydevco/cowboy 1.0.16 → 1.0.18

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/README.md CHANGED
@@ -106,7 +106,7 @@ let router = new Cowboy()
106
106
  Notes:
107
107
  * All middleware items are called in sequence - and are async waited-on)
108
108
  * If any middleware functions fail the entire chain aborts with an error
109
- * All middleware functions are called as `(CowboyRequest, CowboyResponse)`
109
+ * All middleware functions are called as `(CowboyRequest, CowboyResponse, Env)`
110
110
  * If any middleware functions call `res.end()` (or any of its automatic methods like `res.send()` / `res.sendStatus()`) the chain also aborts successfully
111
111
  * If the last middleware function returns a non response object - i.e. the function didn't call `res.send()` its assumed to be a valid output and is automatically wrapped
112
112
 
@@ -335,6 +335,10 @@ cowboy()
335
335
  )
336
336
  ```
337
337
 
338
+ validateHeaders(validator)
339
+ -----------------------
340
+ Shorthand validator which runs validation on the `req.headers` parameter only.
341
+
338
342
 
339
343
  validateParams(validator)
340
344
  -------------------------
package/lib/cowboy.js CHANGED
@@ -69,9 +69,11 @@ export class Cowboy {
69
69
  * @returns {Cowboy} This chainable Cowboy router instance
70
70
  */
71
71
  route(methods, paths, ...middleware) {
72
+ let matcher = compileRoutePaths(paths);
72
73
  this.routes.push({
73
74
  methods: Array.isArray(methods) ? methods : [methods],
74
- matcher: compileRoutePaths(paths),
75
+ paths: matcher.paths,
76
+ matcher,
75
77
  middleware,
76
78
  })
77
79
 
@@ -148,6 +150,10 @@ export class Cowboy {
148
150
  return res.sendStatus(404).toCloudflareResponse(); // No matching route
149
151
  }
150
152
 
153
+ // Populate params
154
+ let firstPathIndex = route.paths.findIndex(re => re.test(req.path));
155
+ req.params = route.paths[firstPathIndex].exec(req.path)?.groups;
156
+
151
157
  // Exec route middleware
152
158
  let response = await this.execMiddleware({
153
159
  req, res, env,
package/lib/response.js CHANGED
@@ -99,7 +99,12 @@ export default class CowboyResponse {
99
99
  status: this.code,
100
100
  headers: this.headers,
101
101
  };
102
- console.log('Build response', JSON.stringify({...cfOptions, body: this.body}, null, '\t'));
102
+ console.log('Response', JSON.stringify({
103
+ ...cfOptions,
104
+ body:
105
+ typeof this.body == 'string' && this.body.length > 30 ? this.body.substr(0, 50) + '…'
106
+ : this.body,
107
+ }, null, '\t'));
103
108
  return new this.CloudflareResponse(this.body, cfOptions);
104
109
  }
105
110
  }
@@ -1,6 +1,7 @@
1
1
  import cors from '#middleware/cors';
2
2
  import validate from '#middleware/validate';
3
3
  import validateBody from '#middleware/validateBody';
4
+ import validateHeaders from '#middleware/validateHeaders';
4
5
  import validateParams from '#middleware/validateParams';
5
6
  import validateQuery from '#middleware/validateQuery';
6
7
 
@@ -8,6 +9,7 @@ export default {
8
9
  cors,
9
10
  validate,
10
11
  validateBody,
12
+ validateHeaders,
11
13
  validateParams,
12
14
  validateQuery,
13
15
  }
@@ -0,0 +1,5 @@
1
+ import CowboyMiddlewareValidate from '#middleware/validate';
2
+
3
+ export default function CowboyMiddlewareValidateHeaders(validator) {
4
+ return CowboyMiddlewareValidate('headers', validator);
5
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momsfriendlydevco/cowboy",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "Wrapper around Cloudflare Wrangler to provide a more Express-like experience",
5
5
  "scripts": {
6
6
  "lint": "eslint ."