@jaypie/express 1.0.15 → 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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Finlayson Studio, LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -2,15 +2,7 @@
2
2
 
3
3
  Express Jaypie handler for AWS Lambda
4
4
 
5
- ## 📋 Usage
6
-
7
- ### Installation
8
-
9
- ```bash
10
- npm install @jaypie/express
11
- ```
12
-
13
- See [Jaypie documentation](https://github.com/finlaysonstudio/jaypie) for usage.
5
+ See [Jaypie](https://github.com/finlaysonstudio/jaypie) for usage.
14
6
 
15
7
  ## 📝 Changelog
16
8
 
@@ -22,4 +14,4 @@ See [Jaypie documentation](https://github.com/finlaysonstudio/jaypie) for usage.
22
14
 
23
15
  ## 📜 License
24
16
 
25
- Published by Finlayson Studio. All rights reserved
17
+ [MIT License](./LICENSE.txt). Published by Finlayson Studio
package/dist/module.cjs CHANGED
@@ -60,7 +60,7 @@ const decorateResponse = (
60
60
  //
61
61
  if (typeof res !== "object" || res === null) {
62
62
  log.warn("decorateResponse called but response is not an object");
63
- return; // eslint-disable-line no-useless-return
63
+ return;
64
64
  }
65
65
 
66
66
  try {
@@ -79,28 +79,28 @@ const decorateResponse = (
79
79
 
80
80
  // X-Project-Environment
81
81
  if (process.env.PROJECT_ENV) {
82
- res.setHeader(core.HTTP.HEADER.PROJECT.ENVIRONMENT, process.env.PROJECT_ENV);
82
+ res.set(core.HTTP.HEADER.PROJECT.ENVIRONMENT, process.env.PROJECT_ENV);
83
83
  }
84
84
 
85
85
  // X-Project-Handler
86
86
  if (handler) {
87
- res.setHeader(core.HTTP.HEADER.PROJECT.HANDLER, handler);
87
+ res.set(core.HTTP.HEADER.PROJECT.HANDLER, handler);
88
88
  }
89
89
 
90
90
  // X-Project-Invocation
91
91
  const currentInvoke = getCurrentInvokeUuid();
92
92
  if (currentInvoke) {
93
- res.setHeader(core.HTTP.HEADER.PROJECT.INVOCATION, currentInvoke);
93
+ res.set(core.HTTP.HEADER.PROJECT.INVOCATION, currentInvoke);
94
94
  }
95
95
 
96
96
  // X-Project-Key
97
97
  if (process.env.PROJECT_KEY) {
98
- res.setHeader(core.HTTP.HEADER.PROJECT.KEY, process.env.PROJECT_KEY);
98
+ res.set(core.HTTP.HEADER.PROJECT.KEY, process.env.PROJECT_KEY);
99
99
  }
100
100
 
101
101
  // X-Project-Version
102
102
  if (version) {
103
- res.setHeader(core.HTTP.HEADER.PROJECT.VERSION, version);
103
+ res.set(core.HTTP.HEADER.PROJECT.VERSION, version);
104
104
  }
105
105
 
106
106
  //
@@ -180,7 +180,6 @@ const expressHandler = (handler, options = {}) => {
180
180
  //
181
181
  // Validate
182
182
  //
183
- /* eslint-disable no-autofix/prefer-const */
184
183
  let {
185
184
  locals,
186
185
  name,
@@ -189,7 +188,6 @@ const expressHandler = (handler, options = {}) => {
189
188
  unavailable,
190
189
  validate,
191
190
  } = options;
192
- /* eslint-enable no-autofix/prefer-const */
193
191
  core.validate.function(handler);
194
192
  core.validate.optional.object(locals);
195
193
  setup = core.force.array(setup); // allows a single item
@@ -305,7 +303,6 @@ const expressHandler = (handler, options = {}) => {
305
303
  const key = keys[i];
306
304
  libLogger.trace(`[jaypie] Locals: ${key}`);
307
305
  if (typeof locals[key] === "function") {
308
- // eslint-disable-next-line no-await-in-loop
309
306
  localsReq.locals[key] = await locals[key](localsReq, localsRes);
310
307
  } else {
311
308
  localsReq.locals[key] = locals[key];
@@ -392,6 +389,7 @@ const expressHandler = (handler, options = {}) => {
392
389
  } else if (typeof response === "string") {
393
390
  try {
394
391
  res.status(status).json(JSON.parse(response));
392
+ // eslint-disable-next-line no-unused-vars
395
393
  } catch (error) {
396
394
  res.status(status).send(response);
397
395
  }
@@ -58,7 +58,7 @@ const decorateResponse = (
58
58
  //
59
59
  if (typeof res !== "object" || res === null) {
60
60
  log$1.warn("decorateResponse called but response is not an object");
61
- return; // eslint-disable-line no-useless-return
61
+ return;
62
62
  }
63
63
 
64
64
  try {
@@ -77,28 +77,28 @@ const decorateResponse = (
77
77
 
78
78
  // X-Project-Environment
79
79
  if (process.env.PROJECT_ENV) {
80
- res.setHeader(HTTP.HEADER.PROJECT.ENVIRONMENT, process.env.PROJECT_ENV);
80
+ res.set(HTTP.HEADER.PROJECT.ENVIRONMENT, process.env.PROJECT_ENV);
81
81
  }
82
82
 
83
83
  // X-Project-Handler
84
84
  if (handler) {
85
- res.setHeader(HTTP.HEADER.PROJECT.HANDLER, handler);
85
+ res.set(HTTP.HEADER.PROJECT.HANDLER, handler);
86
86
  }
87
87
 
88
88
  // X-Project-Invocation
89
89
  const currentInvoke = getCurrentInvokeUuid();
90
90
  if (currentInvoke) {
91
- res.setHeader(HTTP.HEADER.PROJECT.INVOCATION, currentInvoke);
91
+ res.set(HTTP.HEADER.PROJECT.INVOCATION, currentInvoke);
92
92
  }
93
93
 
94
94
  // X-Project-Key
95
95
  if (process.env.PROJECT_KEY) {
96
- res.setHeader(HTTP.HEADER.PROJECT.KEY, process.env.PROJECT_KEY);
96
+ res.set(HTTP.HEADER.PROJECT.KEY, process.env.PROJECT_KEY);
97
97
  }
98
98
 
99
99
  // X-Project-Version
100
100
  if (version) {
101
- res.setHeader(HTTP.HEADER.PROJECT.VERSION, version);
101
+ res.set(HTTP.HEADER.PROJECT.VERSION, version);
102
102
  }
103
103
 
104
104
  //
@@ -178,7 +178,6 @@ const expressHandler = (handler, options = {}) => {
178
178
  //
179
179
  // Validate
180
180
  //
181
- /* eslint-disable no-autofix/prefer-const */
182
181
  let {
183
182
  locals,
184
183
  name,
@@ -187,7 +186,6 @@ const expressHandler = (handler, options = {}) => {
187
186
  unavailable,
188
187
  validate: validate$1,
189
188
  } = options;
190
- /* eslint-enable no-autofix/prefer-const */
191
189
  validate.function(handler);
192
190
  validate.optional.object(locals);
193
191
  setup = force.array(setup); // allows a single item
@@ -303,7 +301,6 @@ const expressHandler = (handler, options = {}) => {
303
301
  const key = keys[i];
304
302
  libLogger.trace(`[jaypie] Locals: ${key}`);
305
303
  if (typeof locals[key] === "function") {
306
- // eslint-disable-next-line no-await-in-loop
307
304
  localsReq.locals[key] = await locals[key](localsReq, localsRes);
308
305
  } else {
309
306
  localsReq.locals[key] = locals[key];
@@ -390,6 +387,7 @@ const expressHandler = (handler, options = {}) => {
390
387
  } else if (typeof response === "string") {
391
388
  try {
392
389
  res.status(status).json(JSON.parse(response));
390
+ // eslint-disable-next-line no-unused-vars
393
391
  } catch (error) {
394
392
  res.status(status).send(response);
395
393
  }
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@jaypie/express",
3
- "version": "1.0.15",
3
+ "version": "1.1.1",
4
+ "license": "MIT",
4
5
  "author": "Finlayson Studio",
5
6
  "type": "module",
6
7
  "exports": {
@@ -17,18 +18,11 @@
17
18
  "format": "npm run format:package && npm run format:lint",
18
19
  "format:lint": "eslint --fix .",
19
20
  "format:package": "sort-package-json ./package.json",
20
- "init:deploy": "hygen jaypie workflow-npm",
21
21
  "lint": "eslint .",
22
- "new": "hygen jaypie vite",
23
- "new:command": "hygen jaypie command",
24
- "new:constants": "hygen jaypie constants",
25
- "new:hygen": "hygen jaypie hygen",
26
- "new:index": "hygen jaypie index",
27
- "new:model": "hygen jaypie model",
28
- "new:test": "hygen jaypie vitest",
29
22
  "prepublish": "npm run build",
30
- "test": "vitest",
23
+ "test": "vitest run .",
31
24
  "test:spec:constants": "vitest run ./src/__tests__/constants.spec.js",
25
+ "test:spec:cors.helper": "vitest run ./src/__tests__/cors.helper.spec.js",
32
26
  "test:spec:decorateResponse.helper": "vitest run ./src/__tests__/decorateResponse.helper.spec.js",
33
27
  "test:spec:echo.handler": "vitest run ./src/__tests__/echo.handler.spec.js",
34
28
  "test:spec:expressHandler": "vitest run ./src/__tests__/expressHandler.spec.js",
@@ -42,23 +36,12 @@
42
36
  },
43
37
  "dependencies": {
44
38
  "@codegenie/serverless-express": "^4.15.0",
45
- "@jaypie/core": "^1.0.44"
39
+ "@jaypie/core": "^1.1.0",
40
+ "@jaypie/errors": "^1.1.2",
41
+ "cors": "^2.8.5"
46
42
  },
47
- "devDependencies": {
48
- "@jaypie/testkit": "^1.0.29",
49
- "@rollup/plugin-commonjs": "^26.0.1",
50
- "@rollup/plugin-node-resolve": "^15.2.3",
51
- "eslint": "^8.57.0",
52
- "eslint-config-jaypie": "^1.0.7",
53
- "express": "^4.19.2",
54
- "hygen": "^6.2.11",
55
- "jest-extended": "^4.0.2",
56
- "mock-express-response": "^0.3.0",
57
- "prettier": "^3.2.5",
58
- "rollup": "^4.17.2",
59
- "rollup-plugin-auto-external": "^2.0.0",
60
- "sort-package-json": "^2.10.0",
61
- "supertest": "^7.0.0",
62
- "vitest": "^2.1.3"
63
- }
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "gitHead": "878517b1552208902adeef5f6658c0a5c580c3fc"
64
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;
@@ -20,7 +20,7 @@ const decorateResponse = (
20
20
  //
21
21
  if (typeof res !== "object" || res === null) {
22
22
  log.warn("decorateResponse called but response is not an object");
23
- return; // eslint-disable-line no-useless-return
23
+ return;
24
24
  }
25
25
 
26
26
  try {
@@ -39,28 +39,28 @@ const decorateResponse = (
39
39
 
40
40
  // X-Project-Environment
41
41
  if (process.env.PROJECT_ENV) {
42
- res.setHeader(HTTP.HEADER.PROJECT.ENVIRONMENT, process.env.PROJECT_ENV);
42
+ res.set(HTTP.HEADER.PROJECT.ENVIRONMENT, process.env.PROJECT_ENV);
43
43
  }
44
44
 
45
45
  // X-Project-Handler
46
46
  if (handler) {
47
- res.setHeader(HTTP.HEADER.PROJECT.HANDLER, handler);
47
+ res.set(HTTP.HEADER.PROJECT.HANDLER, handler);
48
48
  }
49
49
 
50
50
  // X-Project-Invocation
51
51
  const currentInvoke = getCurrentInvokeUuid();
52
52
  if (currentInvoke) {
53
- res.setHeader(HTTP.HEADER.PROJECT.INVOCATION, currentInvoke);
53
+ res.set(HTTP.HEADER.PROJECT.INVOCATION, currentInvoke);
54
54
  }
55
55
 
56
56
  // X-Project-Key
57
57
  if (process.env.PROJECT_KEY) {
58
- res.setHeader(HTTP.HEADER.PROJECT.KEY, process.env.PROJECT_KEY);
58
+ res.set(HTTP.HEADER.PROJECT.KEY, process.env.PROJECT_KEY);
59
59
  }
60
60
 
61
61
  // X-Project-Version
62
62
  if (version) {
63
- res.setHeader(HTTP.HEADER.PROJECT.VERSION, version);
63
+ res.set(HTTP.HEADER.PROJECT.VERSION, version);
64
64
  }
65
65
 
66
66
  //
@@ -30,7 +30,6 @@ const expressHandler = (handler, options = {}) => {
30
30
  //
31
31
  // Validate
32
32
  //
33
- /* eslint-disable no-autofix/prefer-const */
34
33
  let {
35
34
  locals,
36
35
  name,
@@ -39,7 +38,6 @@ const expressHandler = (handler, options = {}) => {
39
38
  unavailable,
40
39
  validate,
41
40
  } = options;
42
- /* eslint-enable no-autofix/prefer-const */
43
41
  validateIs.function(handler);
44
42
  validateIs.optional.object(locals);
45
43
  setup = force.array(setup); // allows a single item
@@ -155,7 +153,6 @@ const expressHandler = (handler, options = {}) => {
155
153
  const key = keys[i];
156
154
  libLogger.trace(`[jaypie] Locals: ${key}`);
157
155
  if (typeof locals[key] === "function") {
158
- // eslint-disable-next-line no-await-in-loop
159
156
  localsReq.locals[key] = await locals[key](localsReq, localsRes);
160
157
  } else {
161
158
  localsReq.locals[key] = locals[key];
@@ -242,6 +239,7 @@ const expressHandler = (handler, options = {}) => {
242
239
  } else if (typeof response === "string") {
243
240
  try {
244
241
  res.status(status).json(JSON.parse(response));
242
+ // eslint-disable-next-line no-unused-vars
245
243
  } catch (error) {
246
244
  res.status(status).send(response);
247
245
  }
package/.eslintignore DELETED
@@ -1 +0,0 @@
1
- dist/