@jaypie/express 1.1.0 → 1.1.1
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/package.json +6 -3
- package/src/cors.helper.js +86 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jaypie/express",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Finlayson Studio",
|
|
6
6
|
"type": "module",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"prepublish": "npm run build",
|
|
23
23
|
"test": "vitest run .",
|
|
24
24
|
"test:spec:constants": "vitest run ./src/__tests__/constants.spec.js",
|
|
25
|
+
"test:spec:cors.helper": "vitest run ./src/__tests__/cors.helper.spec.js",
|
|
25
26
|
"test:spec:decorateResponse.helper": "vitest run ./src/__tests__/decorateResponse.helper.spec.js",
|
|
26
27
|
"test:spec:echo.handler": "vitest run ./src/__tests__/echo.handler.spec.js",
|
|
27
28
|
"test:spec:expressHandler": "vitest run ./src/__tests__/expressHandler.spec.js",
|
|
@@ -35,10 +36,12 @@
|
|
|
35
36
|
},
|
|
36
37
|
"dependencies": {
|
|
37
38
|
"@codegenie/serverless-express": "^4.15.0",
|
|
38
|
-
"@jaypie/core": "^1.1.0"
|
|
39
|
+
"@jaypie/core": "^1.1.0",
|
|
40
|
+
"@jaypie/errors": "^1.1.2",
|
|
41
|
+
"cors": "^2.8.5"
|
|
39
42
|
},
|
|
40
43
|
"publishConfig": {
|
|
41
44
|
"access": "public"
|
|
42
45
|
},
|
|
43
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "878517b1552208902adeef5f6658c0a5c580c3fc"
|
|
44
47
|
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { CorsError } from "@jaypie/errors";
|
|
2
|
+
import expressCors from "cors";
|
|
3
|
+
|
|
4
|
+
//
|
|
5
|
+
//
|
|
6
|
+
// Constants
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
const DEFAULT_HEADERS = ["Authorization", "X-Session-Id"];
|
|
10
|
+
const DEFAULT_METHODS = ["DELETE", "HEAD", "GET", "POST", "PUT"];
|
|
11
|
+
const HTTP_PROTOCOL = "http://";
|
|
12
|
+
const HTTPS_PROTOCOL = "https://";
|
|
13
|
+
const SANDBOX_ENV = "sandbox";
|
|
14
|
+
|
|
15
|
+
//
|
|
16
|
+
//
|
|
17
|
+
// Helper Functions
|
|
18
|
+
//
|
|
19
|
+
|
|
20
|
+
const ensureProtocol = (url) => {
|
|
21
|
+
if (!url) return url;
|
|
22
|
+
if (url.startsWith(HTTP_PROTOCOL) || url.startsWith(HTTPS_PROTOCOL))
|
|
23
|
+
return url;
|
|
24
|
+
return HTTPS_PROTOCOL + url;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
//
|
|
28
|
+
//
|
|
29
|
+
// Main
|
|
30
|
+
//
|
|
31
|
+
|
|
32
|
+
const corsHelper = (config = {}) => {
|
|
33
|
+
const { origins, methods, headers, overrides = {} } = config;
|
|
34
|
+
|
|
35
|
+
const options = {
|
|
36
|
+
origin(origin, callback) {
|
|
37
|
+
// Handle wildcard origin
|
|
38
|
+
if (origins === "*") {
|
|
39
|
+
callback(null, true);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Allow requests with no origin (like mobile apps, curl, etc)
|
|
44
|
+
if (!origin) {
|
|
45
|
+
callback(null, true);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const allowedOrigins = origins || [
|
|
50
|
+
ensureProtocol(process.env.BASE_URL),
|
|
51
|
+
ensureProtocol(process.env.PROJECT_BASE_URL),
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
// Add localhost origins in sandbox
|
|
55
|
+
if (!origins && process.env.PROJECT_ENV === SANDBOX_ENV) {
|
|
56
|
+
allowedOrigins.push("http://localhost");
|
|
57
|
+
allowedOrigins.push(/^http:\/\/localhost:\d+$/);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const isAllowed = allowedOrigins.some((allowed) => {
|
|
61
|
+
if (allowed instanceof RegExp) {
|
|
62
|
+
return allowed.test(origin);
|
|
63
|
+
}
|
|
64
|
+
return origin.includes(allowed);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
if (isAllowed) {
|
|
68
|
+
callback(null, true);
|
|
69
|
+
} else {
|
|
70
|
+
callback(new CorsError());
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
methods: [...DEFAULT_METHODS, ...(methods || [])],
|
|
74
|
+
allowedHeaders: [...DEFAULT_HEADERS, ...(headers || [])],
|
|
75
|
+
...overrides,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return expressCors(options);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
//
|
|
82
|
+
//
|
|
83
|
+
// Export
|
|
84
|
+
//
|
|
85
|
+
|
|
86
|
+
export default corsHelper;
|