@jaypie/express 1.1.1 → 1.1.3
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/dist/module.cjs +95 -0
- package/dist/module.esm.js +95 -1
- package/package.json +2 -2
- package/src/cors.helper.js +9 -1
- package/src/index.js +1 -0
package/dist/module.cjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var errors = require('@jaypie/errors');
|
|
4
|
+
var expressCors = require('cors');
|
|
3
5
|
var core = require('@jaypie/core');
|
|
4
6
|
var serverlessExpress = require('@codegenie/serverless-express');
|
|
5
7
|
|
|
@@ -16,6 +18,98 @@ const EXPRESS = {
|
|
|
16
18
|
},
|
|
17
19
|
};
|
|
18
20
|
|
|
21
|
+
//
|
|
22
|
+
//
|
|
23
|
+
// Constants
|
|
24
|
+
//
|
|
25
|
+
|
|
26
|
+
const DEFAULT_HEADERS = ["Authorization", "X-Session-Id"];
|
|
27
|
+
const DEFAULT_METHODS = ["DELETE", "HEAD", "GET", "POST", "PUT"];
|
|
28
|
+
const HTTP_PROTOCOL = "http://";
|
|
29
|
+
const HTTPS_PROTOCOL = "https://";
|
|
30
|
+
const SANDBOX_ENV = "sandbox";
|
|
31
|
+
|
|
32
|
+
//
|
|
33
|
+
//
|
|
34
|
+
// Helper Functions
|
|
35
|
+
//
|
|
36
|
+
|
|
37
|
+
const ensureProtocol = (url) => {
|
|
38
|
+
if (!url) return url;
|
|
39
|
+
if (url.startsWith(HTTP_PROTOCOL) || url.startsWith(HTTPS_PROTOCOL))
|
|
40
|
+
return url;
|
|
41
|
+
return HTTPS_PROTOCOL + url;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
//
|
|
45
|
+
//
|
|
46
|
+
// Main
|
|
47
|
+
//
|
|
48
|
+
|
|
49
|
+
const corsHelper = (config = {}) => {
|
|
50
|
+
const { origins, methods, headers, overrides = {} } = config;
|
|
51
|
+
|
|
52
|
+
const options = {
|
|
53
|
+
origin(origin, callback) {
|
|
54
|
+
// Handle wildcard origin
|
|
55
|
+
if (origins === "*") {
|
|
56
|
+
callback(null, true);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Allow requests with no origin (like mobile apps, curl, etc)
|
|
61
|
+
if (!origin) {
|
|
62
|
+
callback(null, true);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const allowedOrigins = origins || [
|
|
67
|
+
ensureProtocol(process.env.BASE_URL),
|
|
68
|
+
ensureProtocol(process.env.PROJECT_BASE_URL),
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
// Add localhost origins in sandbox
|
|
72
|
+
if (!origins && process.env.PROJECT_ENV === SANDBOX_ENV) {
|
|
73
|
+
allowedOrigins.push("http://localhost");
|
|
74
|
+
allowedOrigins.push(/^http:\/\/localhost:\d+$/);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const isAllowed = allowedOrigins.some((allowed) => {
|
|
78
|
+
if (allowed instanceof RegExp) {
|
|
79
|
+
return allowed.test(origin);
|
|
80
|
+
}
|
|
81
|
+
return origin.includes(allowed);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
if (isAllowed) {
|
|
85
|
+
callback(null, true);
|
|
86
|
+
} else {
|
|
87
|
+
callback(new errors.CorsError());
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
methods: [...DEFAULT_METHODS, ...(methods || [])],
|
|
91
|
+
allowedHeaders: [...DEFAULT_HEADERS, ...(headers || [])],
|
|
92
|
+
...overrides,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
return expressCors(options);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
//
|
|
99
|
+
//
|
|
100
|
+
// Export
|
|
101
|
+
//
|
|
102
|
+
|
|
103
|
+
console.log("Export cors middleware");
|
|
104
|
+
var cors_helper = (config) => {
|
|
105
|
+
console.log("Create cors middleware");
|
|
106
|
+
const cors = corsHelper(config);
|
|
107
|
+
return (req, res, next) => {
|
|
108
|
+
console.log("Run cors middleware");
|
|
109
|
+
cors(req, res, next);
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
|
|
19
113
|
//
|
|
20
114
|
//
|
|
21
115
|
// Helper Functions
|
|
@@ -548,6 +642,7 @@ const {
|
|
|
548
642
|
|
|
549
643
|
exports.EXPRESS = EXPRESS;
|
|
550
644
|
exports.badRequestRoute = badRequestRoute;
|
|
645
|
+
exports.cors = cors_helper;
|
|
551
646
|
exports.echoRoute = echoRoute;
|
|
552
647
|
exports.expressHandler = expressHandler;
|
|
553
648
|
exports.expressHttpCodeHandler = httpHandler;
|
package/dist/module.esm.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { CorsError } from '@jaypie/errors';
|
|
2
|
+
import expressCors from 'cors';
|
|
1
3
|
import { log, JAYPIE, HTTP, validate, force, jaypieHandler, UnhandledError, BadRequestError, UnauthorizedError, ForbiddenError, NotFoundError, MethodNotAllowedError, GoneError, TeapotError, InternalError, BadGatewayError, UnavailableError, GatewayTimeoutError, NotImplementedError } from '@jaypie/core';
|
|
2
4
|
import { getCurrentInvoke } from '@codegenie/serverless-express';
|
|
3
5
|
|
|
@@ -14,6 +16,98 @@ const EXPRESS = {
|
|
|
14
16
|
},
|
|
15
17
|
};
|
|
16
18
|
|
|
19
|
+
//
|
|
20
|
+
//
|
|
21
|
+
// Constants
|
|
22
|
+
//
|
|
23
|
+
|
|
24
|
+
const DEFAULT_HEADERS = ["Authorization", "X-Session-Id"];
|
|
25
|
+
const DEFAULT_METHODS = ["DELETE", "HEAD", "GET", "POST", "PUT"];
|
|
26
|
+
const HTTP_PROTOCOL = "http://";
|
|
27
|
+
const HTTPS_PROTOCOL = "https://";
|
|
28
|
+
const SANDBOX_ENV = "sandbox";
|
|
29
|
+
|
|
30
|
+
//
|
|
31
|
+
//
|
|
32
|
+
// Helper Functions
|
|
33
|
+
//
|
|
34
|
+
|
|
35
|
+
const ensureProtocol = (url) => {
|
|
36
|
+
if (!url) return url;
|
|
37
|
+
if (url.startsWith(HTTP_PROTOCOL) || url.startsWith(HTTPS_PROTOCOL))
|
|
38
|
+
return url;
|
|
39
|
+
return HTTPS_PROTOCOL + url;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
//
|
|
43
|
+
//
|
|
44
|
+
// Main
|
|
45
|
+
//
|
|
46
|
+
|
|
47
|
+
const corsHelper = (config = {}) => {
|
|
48
|
+
const { origins, methods, headers, overrides = {} } = config;
|
|
49
|
+
|
|
50
|
+
const options = {
|
|
51
|
+
origin(origin, callback) {
|
|
52
|
+
// Handle wildcard origin
|
|
53
|
+
if (origins === "*") {
|
|
54
|
+
callback(null, true);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Allow requests with no origin (like mobile apps, curl, etc)
|
|
59
|
+
if (!origin) {
|
|
60
|
+
callback(null, true);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const allowedOrigins = origins || [
|
|
65
|
+
ensureProtocol(process.env.BASE_URL),
|
|
66
|
+
ensureProtocol(process.env.PROJECT_BASE_URL),
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
// Add localhost origins in sandbox
|
|
70
|
+
if (!origins && process.env.PROJECT_ENV === SANDBOX_ENV) {
|
|
71
|
+
allowedOrigins.push("http://localhost");
|
|
72
|
+
allowedOrigins.push(/^http:\/\/localhost:\d+$/);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const isAllowed = allowedOrigins.some((allowed) => {
|
|
76
|
+
if (allowed instanceof RegExp) {
|
|
77
|
+
return allowed.test(origin);
|
|
78
|
+
}
|
|
79
|
+
return origin.includes(allowed);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
if (isAllowed) {
|
|
83
|
+
callback(null, true);
|
|
84
|
+
} else {
|
|
85
|
+
callback(new CorsError());
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
methods: [...DEFAULT_METHODS, ...(methods || [])],
|
|
89
|
+
allowedHeaders: [...DEFAULT_HEADERS, ...(headers || [])],
|
|
90
|
+
...overrides,
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
return expressCors(options);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
//
|
|
97
|
+
//
|
|
98
|
+
// Export
|
|
99
|
+
//
|
|
100
|
+
|
|
101
|
+
console.log("Export cors middleware");
|
|
102
|
+
var cors_helper = (config) => {
|
|
103
|
+
console.log("Create cors middleware");
|
|
104
|
+
const cors = corsHelper(config);
|
|
105
|
+
return (req, res, next) => {
|
|
106
|
+
console.log("Run cors middleware");
|
|
107
|
+
cors(req, res, next);
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
|
|
17
111
|
//
|
|
18
112
|
//
|
|
19
113
|
// Helper Functions
|
|
@@ -544,4 +638,4 @@ const {
|
|
|
544
638
|
notImplementedRoute,
|
|
545
639
|
} = routes;
|
|
546
640
|
|
|
547
|
-
export { EXPRESS, badRequestRoute, echoRoute, expressHandler, httpHandler as expressHttpCodeHandler, forbiddenRoute, goneRoute, methodNotAllowedRoute, noContentRoute, notFoundRoute, notImplementedRoute };
|
|
641
|
+
export { EXPRESS, badRequestRoute, cors_helper as cors, echoRoute, expressHandler, httpHandler as expressHttpCodeHandler, forbiddenRoute, goneRoute, methodNotAllowedRoute, noContentRoute, notFoundRoute, notImplementedRoute };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jaypie/express",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Finlayson Studio",
|
|
6
6
|
"type": "module",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"publishConfig": {
|
|
44
44
|
"access": "public"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "cb9bf6543a9bd07129ea9354ceea73df0461de87"
|
|
47
47
|
}
|
package/src/cors.helper.js
CHANGED
|
@@ -83,4 +83,12 @@ const corsHelper = (config = {}) => {
|
|
|
83
83
|
// Export
|
|
84
84
|
//
|
|
85
85
|
|
|
86
|
-
|
|
86
|
+
console.log("Export cors middleware");
|
|
87
|
+
export default (config) => {
|
|
88
|
+
console.log("Create cors middleware");
|
|
89
|
+
const cors = corsHelper(config);
|
|
90
|
+
return (req, res, next) => {
|
|
91
|
+
console.log("Run cors middleware");
|
|
92
|
+
cors(req, res, next);
|
|
93
|
+
};
|
|
94
|
+
};
|
package/src/index.js
CHANGED