@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 +2 -2
- package/lib/request.js +8 -0
- package/middleware/cors.js +32 -12
- package/package.json +1 -1
package/README.md
CHANGED
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
|
|
package/middleware/cors.js
CHANGED
|
@@ -1,20 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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(
|
|
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
|
}
|