@jaypie/express 1.1.7 → 1.1.9

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
@@ -23,8 +23,6 @@ const EXPRESS = {
23
23
  // Constants
24
24
  //
25
25
 
26
- const DEFAULT_HEADERS = ["Authorization", "X-Session-Id"];
27
- const DEFAULT_METHODS = ["DELETE", "HEAD", "GET", "POST", "PUT"];
28
26
  const HTTP_PROTOCOL = "http://";
29
27
  const HTTPS_PROTOCOL = "https://";
30
28
  const SANDBOX_ENV = "sandbox";
@@ -41,57 +39,67 @@ const ensureProtocol = (url) => {
41
39
  return HTTPS_PROTOCOL + url;
42
40
  };
43
41
 
42
+ const dynamicOriginCallbackHandler = (origin) => {
43
+ return (requestOrigin, callback) => {
44
+ // Handle wildcard origin
45
+ if (origin === "*") {
46
+ callback(null, true);
47
+ return;
48
+ }
49
+
50
+ // Allow requests with no origin (like mobile apps, curl, etc)
51
+ if (!requestOrigin) {
52
+ callback(null, true);
53
+ return;
54
+ }
55
+
56
+ const allowedOrigins = [];
57
+ if (process.env.BASE_URL) {
58
+ allowedOrigins.push(ensureProtocol(process.env.BASE_URL));
59
+ }
60
+ if (process.env.PROJECT_BASE_URL) {
61
+ allowedOrigins.push(ensureProtocol(process.env.PROJECT_BASE_URL));
62
+ }
63
+ if (origin) {
64
+ const additionalOrigins = core.force.array(origin);
65
+ allowedOrigins.push(...additionalOrigins);
66
+ }
67
+
68
+ // Add localhost origins in sandbox
69
+ if (
70
+ process.env.PROJECT_ENV === SANDBOX_ENV ||
71
+ core.envBoolean("PROJECT_SANDBOX_MODE")
72
+ ) {
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(requestOrigin);
80
+ }
81
+ return requestOrigin.includes(allowed);
82
+ });
83
+
84
+ if (isAllowed) {
85
+ callback(null, true);
86
+ } else {
87
+ callback(new errors.CorsError());
88
+ }
89
+ };
90
+ };
91
+
44
92
  //
45
93
  //
46
94
  // Main
47
95
  //
48
96
 
49
97
  const corsHelper = (config = {}) => {
50
- const { origins, methods, headers, overrides = {} } = config;
98
+ const { origin, overrides = {} } = config;
51
99
 
52
100
  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 (
73
- process.env.PROJECT_ENV === SANDBOX_ENV ||
74
- core.envBoolean("PROJECT_DEV")
75
- ) {
76
- allowedOrigins.push("http://localhost");
77
- allowedOrigins.push(/^http:\/\/localhost:\d+$/);
78
- }
79
-
80
- const isAllowed = allowedOrigins.some((allowed) => {
81
- if (allowed instanceof RegExp) {
82
- return allowed.test(origin);
83
- }
84
- return origin.includes(allowed);
85
- });
86
-
87
- if (isAllowed) {
88
- callback(null, true);
89
- } else {
90
- callback(new errors.CorsError());
91
- }
92
- },
93
- methods: [...DEFAULT_METHODS, ...(methods || [])],
94
- allowedHeaders: [...DEFAULT_HEADERS, ...(headers || [])],
101
+ origin: dynamicOriginCallbackHandler(origin),
102
+ // * The default behavior is to allow any headers and methods so they are not included here
95
103
  ...overrides,
96
104
  };
97
105
 
@@ -1,5 +1,5 @@
1
1
  import { CorsError } from '@jaypie/errors';
2
- import { envBoolean, log, JAYPIE, HTTP, validate, force, jaypieHandler, UnhandledError, BadRequestError, UnauthorizedError, ForbiddenError, NotFoundError, MethodNotAllowedError, GoneError, TeapotError, InternalError, BadGatewayError, UnavailableError, GatewayTimeoutError, NotImplementedError } from '@jaypie/core';
2
+ import { force, envBoolean, log, JAYPIE, HTTP, validate, jaypieHandler, UnhandledError, BadRequestError, UnauthorizedError, ForbiddenError, NotFoundError, MethodNotAllowedError, GoneError, TeapotError, InternalError, BadGatewayError, UnavailableError, GatewayTimeoutError, NotImplementedError } from '@jaypie/core';
3
3
  import expressCors from 'cors';
4
4
  import { getCurrentInvoke } from '@codegenie/serverless-express';
5
5
 
@@ -21,8 +21,6 @@ const EXPRESS = {
21
21
  // Constants
22
22
  //
23
23
 
24
- const DEFAULT_HEADERS = ["Authorization", "X-Session-Id"];
25
- const DEFAULT_METHODS = ["DELETE", "HEAD", "GET", "POST", "PUT"];
26
24
  const HTTP_PROTOCOL = "http://";
27
25
  const HTTPS_PROTOCOL = "https://";
28
26
  const SANDBOX_ENV = "sandbox";
@@ -39,57 +37,67 @@ const ensureProtocol = (url) => {
39
37
  return HTTPS_PROTOCOL + url;
40
38
  };
41
39
 
40
+ const dynamicOriginCallbackHandler = (origin) => {
41
+ return (requestOrigin, callback) => {
42
+ // Handle wildcard origin
43
+ if (origin === "*") {
44
+ callback(null, true);
45
+ return;
46
+ }
47
+
48
+ // Allow requests with no origin (like mobile apps, curl, etc)
49
+ if (!requestOrigin) {
50
+ callback(null, true);
51
+ return;
52
+ }
53
+
54
+ const allowedOrigins = [];
55
+ if (process.env.BASE_URL) {
56
+ allowedOrigins.push(ensureProtocol(process.env.BASE_URL));
57
+ }
58
+ if (process.env.PROJECT_BASE_URL) {
59
+ allowedOrigins.push(ensureProtocol(process.env.PROJECT_BASE_URL));
60
+ }
61
+ if (origin) {
62
+ const additionalOrigins = force.array(origin);
63
+ allowedOrigins.push(...additionalOrigins);
64
+ }
65
+
66
+ // Add localhost origins in sandbox
67
+ if (
68
+ process.env.PROJECT_ENV === SANDBOX_ENV ||
69
+ envBoolean("PROJECT_SANDBOX_MODE")
70
+ ) {
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(requestOrigin);
78
+ }
79
+ return requestOrigin.includes(allowed);
80
+ });
81
+
82
+ if (isAllowed) {
83
+ callback(null, true);
84
+ } else {
85
+ callback(new CorsError());
86
+ }
87
+ };
88
+ };
89
+
42
90
  //
43
91
  //
44
92
  // Main
45
93
  //
46
94
 
47
95
  const corsHelper = (config = {}) => {
48
- const { origins, methods, headers, overrides = {} } = config;
96
+ const { origin, overrides = {} } = config;
49
97
 
50
98
  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 (
71
- process.env.PROJECT_ENV === SANDBOX_ENV ||
72
- envBoolean("PROJECT_DEV")
73
- ) {
74
- allowedOrigins.push("http://localhost");
75
- allowedOrigins.push(/^http:\/\/localhost:\d+$/);
76
- }
77
-
78
- const isAllowed = allowedOrigins.some((allowed) => {
79
- if (allowed instanceof RegExp) {
80
- return allowed.test(origin);
81
- }
82
- return origin.includes(allowed);
83
- });
84
-
85
- if (isAllowed) {
86
- callback(null, true);
87
- } else {
88
- callback(new CorsError());
89
- }
90
- },
91
- methods: [...DEFAULT_METHODS, ...(methods || [])],
92
- allowedHeaders: [...DEFAULT_HEADERS, ...(headers || [])],
99
+ origin: dynamicOriginCallbackHandler(origin),
100
+ // * The default behavior is to allow any headers and methods so they are not included here
93
101
  ...overrides,
94
102
  };
95
103
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaypie/express",
3
- "version": "1.1.7",
3
+ "version": "1.1.9",
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": "b6172271a5bfdc96126fe3eded3e17e382c4409b"
46
+ "gitHead": "4838a796c61186888f143d6a86adf6b89a02dd5c"
47
47
  }
@@ -1,5 +1,5 @@
1
1
  import { CorsError } from "@jaypie/errors";
2
- import { envBoolean } from "@jaypie/core";
2
+ import { envBoolean, force } from "@jaypie/core";
3
3
  import expressCors from "cors";
4
4
 
5
5
  //
@@ -7,8 +7,6 @@ import expressCors from "cors";
7
7
  // Constants
8
8
  //
9
9
 
10
- const DEFAULT_HEADERS = ["Authorization", "X-Session-Id"];
11
- const DEFAULT_METHODS = ["DELETE", "HEAD", "GET", "POST", "PUT"];
12
10
  const HTTP_PROTOCOL = "http://";
13
11
  const HTTPS_PROTOCOL = "https://";
14
12
  const SANDBOX_ENV = "sandbox";
@@ -25,57 +23,67 @@ const ensureProtocol = (url) => {
25
23
  return HTTPS_PROTOCOL + url;
26
24
  };
27
25
 
26
+ export const dynamicOriginCallbackHandler = (origin) => {
27
+ return (requestOrigin, callback) => {
28
+ // Handle wildcard origin
29
+ if (origin === "*") {
30
+ callback(null, true);
31
+ return;
32
+ }
33
+
34
+ // Allow requests with no origin (like mobile apps, curl, etc)
35
+ if (!requestOrigin) {
36
+ callback(null, true);
37
+ return;
38
+ }
39
+
40
+ const allowedOrigins = [];
41
+ if (process.env.BASE_URL) {
42
+ allowedOrigins.push(ensureProtocol(process.env.BASE_URL));
43
+ }
44
+ if (process.env.PROJECT_BASE_URL) {
45
+ allowedOrigins.push(ensureProtocol(process.env.PROJECT_BASE_URL));
46
+ }
47
+ if (origin) {
48
+ const additionalOrigins = force.array(origin);
49
+ allowedOrigins.push(...additionalOrigins);
50
+ }
51
+
52
+ // Add localhost origins in sandbox
53
+ if (
54
+ process.env.PROJECT_ENV === SANDBOX_ENV ||
55
+ envBoolean("PROJECT_SANDBOX_MODE")
56
+ ) {
57
+ allowedOrigins.push("http://localhost");
58
+ allowedOrigins.push(/^http:\/\/localhost:\d+$/);
59
+ }
60
+
61
+ const isAllowed = allowedOrigins.some((allowed) => {
62
+ if (allowed instanceof RegExp) {
63
+ return allowed.test(requestOrigin);
64
+ }
65
+ return requestOrigin.includes(allowed);
66
+ });
67
+
68
+ if (isAllowed) {
69
+ callback(null, true);
70
+ } else {
71
+ callback(new CorsError());
72
+ }
73
+ };
74
+ };
75
+
28
76
  //
29
77
  //
30
78
  // Main
31
79
  //
32
80
 
33
81
  const corsHelper = (config = {}) => {
34
- const { origins, methods, headers, overrides = {} } = config;
82
+ const { origin, overrides = {} } = config;
35
83
 
36
84
  const options = {
37
- origin(origin, callback) {
38
- // Handle wildcard origin
39
- if (origins === "*") {
40
- callback(null, true);
41
- return;
42
- }
43
-
44
- // Allow requests with no origin (like mobile apps, curl, etc)
45
- if (!origin) {
46
- callback(null, true);
47
- return;
48
- }
49
-
50
- const allowedOrigins = origins || [
51
- ensureProtocol(process.env.BASE_URL),
52
- ensureProtocol(process.env.PROJECT_BASE_URL),
53
- ];
54
-
55
- // Add localhost origins in sandbox
56
- if (
57
- process.env.PROJECT_ENV === SANDBOX_ENV ||
58
- envBoolean("PROJECT_DEV")
59
- ) {
60
- allowedOrigins.push("http://localhost");
61
- allowedOrigins.push(/^http:\/\/localhost:\d+$/);
62
- }
63
-
64
- const isAllowed = allowedOrigins.some((allowed) => {
65
- if (allowed instanceof RegExp) {
66
- return allowed.test(origin);
67
- }
68
- return origin.includes(allowed);
69
- });
70
-
71
- if (isAllowed) {
72
- callback(null, true);
73
- } else {
74
- callback(new CorsError());
75
- }
76
- },
77
- methods: [...DEFAULT_METHODS, ...(methods || [])],
78
- allowedHeaders: [...DEFAULT_HEADERS, ...(headers || [])],
85
+ origin: dynamicOriginCallbackHandler(origin),
86
+ // * The default behavior is to allow any headers and methods so they are not included here
79
87
  ...overrides,
80
88
  };
81
89