@momsfriendlydevco/cowboy 1.0.8 → 1.0.10

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
@@ -291,8 +291,8 @@ cowboy()
291
291
  ```
292
292
 
293
293
 
294
- cors
295
- ----
294
+ cors(options)
295
+ -------------
296
296
  Inject simple CORS headers to allow websites to use the endpoint from the browser frontend.
297
297
 
298
298
 
package/lib/request.js CHANGED
@@ -16,6 +16,13 @@ export default class CowboyRequest {
16
16
  hostname;
17
17
 
18
18
 
19
+ /**
20
+ * Extracted URL query parameters
21
+ * @type {Object}
22
+ */
23
+ query = {};
24
+
25
+
19
26
  constructor(cfReq, props) {
20
27
  // Copy all cfReq keys locally as a shallow copy
21
28
  Object.assign(
@@ -35,6 +42,7 @@ export default class CowboyRequest {
35
42
  let url = new URL(cfReq.url);
36
43
  this.path = this.pathTidy(url.pathname);
37
44
  this.hostname = url.hostname;
45
+ this.query = Object.fromEntries(url.searchParams);
38
46
  }
39
47
 
40
48
 
@@ -1,20 +1,40 @@
1
- export default function CowboyMiddlewareCORS(headers) {
2
- let injectHeaders = headers || {
3
- 'Access-Control-Allow-Origin': '*',
4
- 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
5
- 'Access-Control-Allow-Headers': '*',
6
- 'Content-Type': 'application/json;charset=UTF-8',
1
+ /**
2
+ * Register a generic middleware to handle CORS requests
3
+ *
4
+ * @param {Object} [options] Additional options to mutate behaviour
5
+ * @param {Boolean} [options.attachOptions=true] Attach an `options` method against all routes that don't already have one to pass the CORS pre-flight check
6
+ * @param {Object} [options.headers] Generic CORS headers to inject
7
+ *
8
+ * @returns {CowboyMiddleware}
9
+ */
10
+ export default function CowboyMiddlewareCORS(options) {
11
+ let settings = {
12
+ attachOptions: true,
13
+ headers: {
14
+ 'Access-Control-Allow-Origin': '*',
15
+ 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
16
+ 'Access-Control-Allow-Headers': '*',
17
+ 'Content-Type': 'application/json;charset=UTF-8',
18
+ },
19
+ ...options,
7
20
  };
8
21
 
9
22
  return (req, res) => {
10
23
  // Always inject CORS headers
11
- res.set(injectHeaders);
24
+ res.set(settings.headers);
25
+
26
+ // Inject various OPTIONS endpoints for CORS pre-flight
27
+ if (settings.attachOptions && !req.router.loadedCors) {
28
+ req.router.routes
29
+ .filter(route => !route.methods.includes('OPTIONS'))
30
+ .forEach(route =>
31
+ route.paths.forEach(path =>
32
+ req.router.options(path, (req, res) =>
33
+ res.sendStatus(200)
34
+ )
35
+ )
36
+ );
12
37
 
13
- // Handle hits to OPTIONS '/' endpoint
14
- if (!req.router.loadedCors) {
15
- req.router.options('/', (req, res) => {
16
- return res.sendStatus(200);
17
- });
18
38
  req.router.loadedCors = true; // Mark we've already done this so we don't keep tweaking the router
19
39
  }
20
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momsfriendlydevco/cowboy",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "Wrapper around Cloudflare Wrangler to provide a more Express-like experience",
5
5
  "scripts": {
6
6
  "lint": "eslint ."