@momsfriendlydevco/cowboy 1.0.13 → 1.0.15
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 +10 -10
- package/lib/cowboy.js +4 -4
- package/middleware/validate.js +8 -3
- package/middleware/validateBody.js +1 -3
- package/middleware/validateParams.js +1 -3
- package/middleware/validateQuery.js +1 -3
- package/package.json +1 -1
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().
|
|
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
|
@@ -130,7 +130,7 @@ export class Cowboy {
|
|
|
130
130
|
|
|
131
131
|
// Exec all earlyMiddleware - every time
|
|
132
132
|
await this.execMiddleware({
|
|
133
|
-
req, res,
|
|
133
|
+
req, res, env,
|
|
134
134
|
middleware: this.earlyMiddleware,
|
|
135
135
|
});
|
|
136
136
|
|
|
@@ -152,7 +152,7 @@ export class Cowboy {
|
|
|
152
152
|
|
|
153
153
|
// Exec route middleware
|
|
154
154
|
let response = await this.execMiddleware({
|
|
155
|
-
req, res,
|
|
155
|
+
req, res, env,
|
|
156
156
|
middleware: route.middleware,
|
|
157
157
|
});
|
|
158
158
|
|
|
@@ -162,7 +162,7 @@ export class Cowboy {
|
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
|
|
165
|
-
async execMiddleware({middleware, req, res}) {
|
|
165
|
+
async execMiddleware({middleware, req, res, env}) {
|
|
166
166
|
let middlewareStack = middleware
|
|
167
167
|
.map(m => {
|
|
168
168
|
let mFunc =
|
|
@@ -179,7 +179,7 @@ export class Cowboy {
|
|
|
179
179
|
while (middlewareStack.length > 0) {
|
|
180
180
|
let middleware = middlewareStack.shift();
|
|
181
181
|
try {
|
|
182
|
-
response = await middleware(req, res);
|
|
182
|
+
response = await middleware(req, res, env);
|
|
183
183
|
if (response?.hasSent) { // Stop middleware chain as some intermediate has signalled the chain should end
|
|
184
184
|
response = res;
|
|
185
185
|
break;
|
package/middleware/validate.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import joyful from '@momsfriendlydevco/joyful';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Run a Joi / Joyful validation function against a specific subkey within `req
|
|
5
|
+
*
|
|
6
|
+
* @param {String} subkey The subkey to run against
|
|
7
|
+
* @param {Function|Object} Callback to use with Joyful to validate with
|
|
8
|
+
* @returns {Void} Either a successful middleware cycle (if validatio succeeds) or a call to `res.status(400)` if failed
|
|
9
|
+
*/
|
|
3
10
|
export default function CowboyMiddlewareValidate(subkey, validator) {
|
|
4
11
|
return (req, res) => {
|
|
5
|
-
let joyfulResult = joyful(
|
|
6
|
-
[subkey]: validator,
|
|
7
|
-
});
|
|
12
|
+
let joyfulResult = joyful(req[subkey], validator);
|
|
8
13
|
|
|
9
14
|
if (joyfulResult !== true) { // Failed body validation?
|
|
10
15
|
return res
|