@momsfriendlydevco/cowboy 1.0.5 → 1.0.7

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
@@ -77,11 +77,11 @@ export class Cowboy {
77
77
 
78
78
  /**
79
79
  * Prepend middleware which will be used for all routes
80
- * @param {CowboyMiddleware} middleware Middleware to use
80
+ * @param {CowboyMiddleware...} middleware Middleware(s) to use
81
81
  * @returns {Cowboy} This chainable Cowboy router instance
82
82
  */
83
83
  use(...middleware) {
84
- this.earlyMiddleware.push(middleware);
84
+ this.earlyMiddleware.push(...middleware);
85
85
  return this;
86
86
  }
87
87
 
@@ -120,7 +120,18 @@ export class Cowboy {
120
120
  router: this,
121
121
  pathTidy: this.settings.pathTidy,
122
122
  });
123
- if (cfReq.body) req.body = await cfReq.json();
123
+
124
+ // Decode input if we're expecting JSON
125
+ if (cfReq.body) {
126
+ try {
127
+ req.body = await cfReq.json();
128
+ } catch (e) {
129
+ if (debug.enabled) {
130
+ debug('Failed to decode request body as JSON: [[[' + cfReq.body.toString() + ']]]');
131
+ }
132
+ throw new Error('Invalid JSON body');
133
+ }
134
+ }
124
135
 
125
136
  let res = new CowboyResponse();
126
137
  debug('Incoming request:', req.toString());
package/lib/testkit.js CHANGED
@@ -18,6 +18,7 @@ export let worker;
18
18
  * @param {Object} [options] Additional options to mutate behaviour
19
19
  * @param {Axios} [options.axios] Axios instance to mutate with the base URL, if specified
20
20
  * @param {Boolean} [options.server=true] Initialize a local server - disable this if you're running your own
21
+ * @param {Boolean} [options.debug=false] Force debug as if `DEBUG=cowboy` was set
21
22
  * @param {Function} [options.logOutput] Function to wrap STDOUT output. Called as `(line:String)`
22
23
  * @param {Function} [options.logOutputErr] Function to wrap STDERR output. Called as `(line:String)`
23
24
  * @param {String} [options.host='127.0.0.1'] Host to run Wrangler on
@@ -30,6 +31,7 @@ export function start(options) {
30
31
  let settings = {
31
32
  axios: null,
32
33
  server: true,
34
+ debug: false,
33
35
  logOutput: output => console.log('WRANGLER>', output),
34
36
  logOutputErr: output => console.log('WRANGLER!', output),
35
37
  host: '127.0.0.1',
@@ -38,6 +40,7 @@ export function start(options) {
38
40
  ...options,
39
41
  };
40
42
 
43
+ if (settings.debug) Debug.enable('cowboy');
41
44
  debug('Start cowboy testkit');
42
45
  let wranglerConfig; // Eventual wrangler config
43
46
 
@@ -93,7 +96,6 @@ export function start(options) {
93
96
  });
94
97
  })
95
98
  // }}}
96
- // .then(()=> new Promise(resolve => setTimeout(resolve, 10 * 1000)))
97
99
  // Mutate axios if provided {{{
98
100
  .then(()=> {
99
101
  if (settings.axios) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momsfriendlydevco/cowboy",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Wrapper around Cloudflare Wrangler to provide a more Express-like experience",
5
5
  "scripts": {
6
6
  "lint": "eslint ."