@jaypie/express 1.1.0 → 1.1.2

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 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,83 @@ 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
+
19
98
  //
20
99
  //
21
100
  // Helper Functions
@@ -548,6 +627,7 @@ const {
548
627
 
549
628
  exports.EXPRESS = EXPRESS;
550
629
  exports.badRequestRoute = badRequestRoute;
630
+ exports.cors = corsHelper;
551
631
  exports.echoRoute = echoRoute;
552
632
  exports.expressHandler = expressHandler;
553
633
  exports.expressHttpCodeHandler = httpHandler;
@@ -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,83 @@ 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
+
17
96
  //
18
97
  //
19
98
  // Helper Functions
@@ -544,4 +623,4 @@ const {
544
623
  notImplementedRoute,
545
624
  } = routes;
546
625
 
547
- export { EXPRESS, badRequestRoute, echoRoute, expressHandler, httpHandler as expressHttpCodeHandler, forbiddenRoute, goneRoute, methodNotAllowedRoute, noContentRoute, notFoundRoute, notImplementedRoute };
626
+ export { EXPRESS, badRequestRoute, corsHelper 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.0",
3
+ "version": "1.1.2",
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": "26b6847fa8b061594bd899c2e098822dad198e51"
46
+ "gitHead": "ec672a9713d9e81e70e93988933e97b853cc1432"
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;
package/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { EXPRESS } from "./constants.js";
2
+ export { default as cors } from "./cors.helper.js";
2
3
  export { default as expressHandler } from "./expressHandler.js";
3
4
  export { default as expressHttpCodeHandler } from "./http.handler.js";
4
5
  export * from "./routes.js";