@momsfriendlydevco/cowboy 1.0.14 → 1.0.16

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
@@ -45,7 +45,7 @@ export default cowboy()
45
45
  .get('/widgets', ()=> // Fetch a list of widgets
46
46
  widgetStore.fetchAll()
47
47
  )
48
- .post('/widgets', async (req, res) => { // Create a new widget
48
+ .post('/widgets', async (req, res, env) => { // Create a new widget
49
49
  let newWidget = await widgetStore.create(req.body);
50
50
  res.send({id: newWidget.id}); // Explicitly send response
51
51
  })
@@ -56,7 +56,7 @@ export default cowboy()
56
56
  req => widgetStore.fetch(req.params.id),
57
57
  )
58
58
  .delete('/widgets/:id', // Try to delete a widget
59
- (req, res) => { // Apply custom middleware
59
+ (req, res, env) => { // Apply custom middleware
60
60
  let isAllowed = await widgetStore.userIsValid(req.headers.auth);
61
61
  if (!isAllowed) return res.sendStatus(403); // Stop bad actors
62
62
  },
@@ -265,7 +265,7 @@ import cowboy from '@momsfriendlydevco/cowboy';
265
265
  cowboy()
266
266
  .get('/path',
267
267
  'cors',
268
- (req, res) => /* ... */
268
+ (req, res, env) => /* ... */
269
269
  )
270
270
 
271
271
  // Name + options - specify an array with an optional options object
@@ -275,7 +275,7 @@ cowboy()
275
275
  option1: value1,
276
276
  /* ... */
277
277
  }],
278
- (req, res) => /* ... */
278
+ (req, res, env) => /* ... */
279
279
  )
280
280
 
281
281
 
@@ -287,7 +287,7 @@ cowboy()
287
287
  option1: value1,
288
288
  /* ... */
289
289
  }),
290
- (req, res) => /* ... */
290
+ (req, res, env) => /* ... */
291
291
  )
292
292
  ```
293
293
 
@@ -312,7 +312,7 @@ cowboy()
312
312
  widget: joi.string().required().valid('froody', 'doodad'),
313
313
  size: joi.number().optional(),
314
314
  })],
315
- (req, res) => /* ... */
315
+ (req, res, env) => /* ... */
316
316
  )
317
317
  ```
318
318
 
@@ -331,7 +331,7 @@ cowboy()
331
331
  widget: joi.string().required().valid('froody', 'doodad'),
332
332
  size: joi.number().optional(),
333
333
  })],
334
- (req, res) => /* ... */
334
+ (req, res, env) => /* ... */
335
335
  )
336
336
  ```
337
337
 
@@ -348,9 +348,9 @@ import cowboy from '@momsfriendlydevco/cowboy';
348
348
  cowboy()
349
349
  .get('/widgets/:id',
350
350
  ['validateParams', joi => {
351
- id: joi.string().requried(),
351
+ id: joi.string().required(),
352
352
  })],
353
- (req, res) => /* ... */
353
+ (req, res, env) => /* ... */
354
354
  )
355
355
  ```
356
356
 
@@ -369,6 +369,6 @@ cowboy()
369
369
  ['validateQuery', joi => {
370
370
  q: joi.string().requried(),
371
371
  })],
372
- (req, res) => /* ... */
372
+ (req, res, env) => /* ... */
373
373
  )
374
374
  ```
package/lib/cowboy.js CHANGED
@@ -2,6 +2,7 @@ import debug from '#lib/debug';
2
2
  import CowboyMiddleware from '#middleware';
3
3
  import CowboyRequest from '#lib/request';
4
4
  import CowboyResponse from '#lib/response';
5
+ import {compile as compileRoutePaths} from '@momsfriendlydevco/path-match';
5
6
 
6
7
  export class Cowboy {
7
8
  /**
@@ -70,9 +71,10 @@ export class Cowboy {
70
71
  route(methods, paths, ...middleware) {
71
72
  this.routes.push({
72
73
  methods: Array.isArray(methods) ? methods : [methods],
73
- paths: Array.isArray(paths) ? paths : [paths],
74
+ matcher: compileRoutePaths(paths),
74
75
  middleware,
75
76
  })
77
+
76
78
  return this;
77
79
  }
78
80
 
@@ -96,11 +98,7 @@ export class Cowboy {
96
98
  resolve(req) {
97
99
  return this.routes.find(route =>
98
100
  route.methods.includes(req.method) // Method matches
99
- && route.paths.some(path => // Path matches
100
- typeof path == 'string' ? req.path == path
101
- : path instanceof RegExp ? path.test(req.path)
102
- : (()=> { throw new Error('Path is not a String or RegExp') })()
103
- )
101
+ && route.matcher.isMatch(req.path)
104
102
  );
105
103
  }
106
104
 
@@ -130,7 +128,7 @@ export class Cowboy {
130
128
 
131
129
  // Exec all earlyMiddleware - every time
132
130
  await this.execMiddleware({
133
- req, res,
131
+ req, res, env,
134
132
  middleware: this.earlyMiddleware,
135
133
  });
136
134
 
@@ -152,7 +150,7 @@ export class Cowboy {
152
150
 
153
151
  // Exec route middleware
154
152
  let response = await this.execMiddleware({
155
- req, res,
153
+ req, res, env,
156
154
  middleware: route.middleware,
157
155
  });
158
156
 
@@ -162,7 +160,7 @@ export class Cowboy {
162
160
  }
163
161
 
164
162
 
165
- async execMiddleware({middleware, req, res}) {
163
+ async execMiddleware({middleware, req, res, env}) {
166
164
  let middlewareStack = middleware
167
165
  .map(m => {
168
166
  let mFunc =
@@ -179,7 +177,7 @@ export class Cowboy {
179
177
  while (middlewareStack.length > 0) {
180
178
  let middleware = middlewareStack.shift();
181
179
  try {
182
- response = await middleware(req, res);
180
+ response = await middleware(req, res, env);
183
181
  if (response?.hasSent) { // Stop middleware chain as some intermediate has signalled the chain should end
184
182
  response = res;
185
183
  break;
package/lib/request.js CHANGED
@@ -54,6 +54,9 @@ export default class CowboyRequest {
54
54
  this.hostname = url.hostname;
55
55
  this.query = Object.fromEntries(url.searchParams);
56
56
 
57
+ this.routePath = ''; // Eventually matching routePath segment
58
+ this.params = {}; // Set empty object for path extraction
59
+
57
60
  // Slurp the headers
58
61
  this.headers = Object.fromEntries(cfReq.headers.entries());
59
62
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momsfriendlydevco/cowboy",
3
- "version": "1.0.14",
3
+ "version": "1.0.16",
4
4
  "description": "Wrapper around Cloudflare Wrangler to provide a more Express-like experience",
5
5
  "scripts": {
6
6
  "lint": "eslint ."
@@ -36,6 +36,7 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "@momsfriendlydevco/joyful": "^1.0.1",
39
+ "@momsfriendlydevco/path-match": "^1.0.0",
39
40
  "toml": "^3.0.0"
40
41
  },
41
42
  "devDependencies": {