@jaypie/express 1.1.8 → 1.1.10

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
@@ -39,16 +39,16 @@ const ensureProtocol = (url) => {
39
39
  return HTTPS_PROTOCOL + url;
40
40
  };
41
41
 
42
- const dynamicOriginCallbackHandler = (origins) => {
43
- return (origin, callback) => {
42
+ const dynamicOriginCallbackHandler = (origin) => {
43
+ return (requestOrigin, callback) => {
44
44
  // Handle wildcard origin
45
- if (origins === "*") {
45
+ if (origin === "*") {
46
46
  callback(null, true);
47
47
  return;
48
48
  }
49
49
 
50
50
  // Allow requests with no origin (like mobile apps, curl, etc)
51
- if (!origin) {
51
+ if (!requestOrigin) {
52
52
  callback(null, true);
53
53
  return;
54
54
  }
@@ -60,8 +60,8 @@ const dynamicOriginCallbackHandler = (origins) => {
60
60
  if (process.env.PROJECT_BASE_URL) {
61
61
  allowedOrigins.push(ensureProtocol(process.env.PROJECT_BASE_URL));
62
62
  }
63
- if (origins) {
64
- const additionalOrigins = core.force.array(origins);
63
+ if (origin) {
64
+ const additionalOrigins = core.force.array(origin);
65
65
  allowedOrigins.push(...additionalOrigins);
66
66
  }
67
67
 
@@ -76,9 +76,9 @@ const dynamicOriginCallbackHandler = (origins) => {
76
76
 
77
77
  const isAllowed = allowedOrigins.some((allowed) => {
78
78
  if (allowed instanceof RegExp) {
79
- return allowed.test(origin);
79
+ return allowed.test(requestOrigin);
80
80
  }
81
- return origin.includes(allowed);
81
+ return requestOrigin.includes(allowed);
82
82
  });
83
83
 
84
84
  if (isAllowed) {
@@ -95,10 +95,10 @@ const dynamicOriginCallbackHandler = (origins) => {
95
95
  //
96
96
 
97
97
  const corsHelper = (config = {}) => {
98
- const { origins, overrides = {} } = config;
98
+ const { origin, overrides = {} } = config;
99
99
 
100
100
  const options = {
101
- origin: dynamicOriginCallbackHandler(origins),
101
+ origin: dynamicOriginCallbackHandler(origin),
102
102
  // * The default behavior is to allow any headers and methods so they are not included here
103
103
  ...overrides,
104
104
  };
@@ -37,16 +37,16 @@ const ensureProtocol = (url) => {
37
37
  return HTTPS_PROTOCOL + url;
38
38
  };
39
39
 
40
- const dynamicOriginCallbackHandler = (origins) => {
41
- return (origin, callback) => {
40
+ const dynamicOriginCallbackHandler = (origin) => {
41
+ return (requestOrigin, callback) => {
42
42
  // Handle wildcard origin
43
- if (origins === "*") {
43
+ if (origin === "*") {
44
44
  callback(null, true);
45
45
  return;
46
46
  }
47
47
 
48
48
  // Allow requests with no origin (like mobile apps, curl, etc)
49
- if (!origin) {
49
+ if (!requestOrigin) {
50
50
  callback(null, true);
51
51
  return;
52
52
  }
@@ -58,8 +58,8 @@ const dynamicOriginCallbackHandler = (origins) => {
58
58
  if (process.env.PROJECT_BASE_URL) {
59
59
  allowedOrigins.push(ensureProtocol(process.env.PROJECT_BASE_URL));
60
60
  }
61
- if (origins) {
62
- const additionalOrigins = force.array(origins);
61
+ if (origin) {
62
+ const additionalOrigins = force.array(origin);
63
63
  allowedOrigins.push(...additionalOrigins);
64
64
  }
65
65
 
@@ -74,9 +74,9 @@ const dynamicOriginCallbackHandler = (origins) => {
74
74
 
75
75
  const isAllowed = allowedOrigins.some((allowed) => {
76
76
  if (allowed instanceof RegExp) {
77
- return allowed.test(origin);
77
+ return allowed.test(requestOrigin);
78
78
  }
79
- return origin.includes(allowed);
79
+ return requestOrigin.includes(allowed);
80
80
  });
81
81
 
82
82
  if (isAllowed) {
@@ -93,10 +93,10 @@ const dynamicOriginCallbackHandler = (origins) => {
93
93
  //
94
94
 
95
95
  const corsHelper = (config = {}) => {
96
- const { origins, overrides = {} } = config;
96
+ const { origin, overrides = {} } = config;
97
97
 
98
98
  const options = {
99
- origin: dynamicOriginCallbackHandler(origins),
99
+ origin: dynamicOriginCallbackHandler(origin),
100
100
  // * The default behavior is to allow any headers and methods so they are not included here
101
101
  ...overrides,
102
102
  };
package/index.d.ts ADDED
@@ -0,0 +1,68 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ import { JaypieHandlerOptions } from "@jaypie/core";
3
+
4
+ export const EXPRESS: {
5
+ PATH: {
6
+ ANY: "*";
7
+ ID: "/:id";
8
+ ROOT: RegExp;
9
+ };
10
+ };
11
+
12
+ export interface CorsConfig {
13
+ origins?: string | string[];
14
+ overrides?: Record<string, unknown>;
15
+ }
16
+
17
+ export interface ExpressHandlerOptions extends JaypieHandlerOptions {
18
+ locals?: Record<string, unknown>;
19
+ name?: string;
20
+ setup?: ((req: Request, res: Response) => Promise<void>)[];
21
+ teardown?: ((req: Request, res: Response) => Promise<void>)[];
22
+ unavailable?: boolean;
23
+ }
24
+
25
+ export function cors(
26
+ config?: CorsConfig,
27
+ ): (req: Request, res: Response, next: NextFunction) => void;
28
+
29
+ export function expressHandler<T>(
30
+ handler: (req: Request, res: Response, ...params: unknown[]) => Promise<T>,
31
+ options?: ExpressHandlerOptions,
32
+ ): (req: Request, res: Response, ...params: unknown[]) => Promise<T>;
33
+
34
+ export function expressHttpCodeHandler(
35
+ statusCode?: number,
36
+ context?: ExpressHandlerOptions,
37
+ ): (req: Request, res: Response) => Promise<Record<string, unknown> | null>;
38
+
39
+ // Pre-configured routes
40
+ export const badRequestRoute: (
41
+ req: Request,
42
+ res: Response,
43
+ ) => Promise<Record<string, unknown>>;
44
+ export const echoRoute: (
45
+ req: Request,
46
+ res: Response,
47
+ ) => Promise<Record<string, unknown>>;
48
+ export const forbiddenRoute: (
49
+ req: Request,
50
+ res: Response,
51
+ ) => Promise<Record<string, unknown>>;
52
+ export const goneRoute: (
53
+ req: Request,
54
+ res: Response,
55
+ ) => Promise<Record<string, unknown>>;
56
+ export const methodNotAllowedRoute: (
57
+ req: Request,
58
+ res: Response,
59
+ ) => Promise<Record<string, unknown>>;
60
+ export const noContentRoute: (req: Request, res: Response) => Promise<null>;
61
+ export const notFoundRoute: (
62
+ req: Request,
63
+ res: Response,
64
+ ) => Promise<Record<string, unknown>>;
65
+ export const notImplementedRoute: (
66
+ req: Request,
67
+ res: Response,
68
+ ) => Promise<never>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaypie/express",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
4
4
  "license": "MIT",
5
5
  "author": "Finlayson Studio",
6
6
  "type": "module",
@@ -13,6 +13,7 @@
13
13
  }
14
14
  },
15
15
  "main": "src/index.js",
16
+ "types": "index.d.ts",
16
17
  "scripts": {
17
18
  "build": "rollup --config",
18
19
  "format": "npm run format:package && npm run format:lint",
@@ -43,5 +44,5 @@
43
44
  "publishConfig": {
44
45
  "access": "public"
45
46
  },
46
- "gitHead": "11eb680bba07d4885cc04dc173165e0b7982b780"
47
+ "gitHead": "358428010fb0d5f130295fbe38b1aab2c1f89e5f"
47
48
  }
@@ -23,16 +23,16 @@ const ensureProtocol = (url) => {
23
23
  return HTTPS_PROTOCOL + url;
24
24
  };
25
25
 
26
- export const dynamicOriginCallbackHandler = (origins) => {
27
- return (origin, callback) => {
26
+ export const dynamicOriginCallbackHandler = (origin) => {
27
+ return (requestOrigin, callback) => {
28
28
  // Handle wildcard origin
29
- if (origins === "*") {
29
+ if (origin === "*") {
30
30
  callback(null, true);
31
31
  return;
32
32
  }
33
33
 
34
34
  // Allow requests with no origin (like mobile apps, curl, etc)
35
- if (!origin) {
35
+ if (!requestOrigin) {
36
36
  callback(null, true);
37
37
  return;
38
38
  }
@@ -44,8 +44,8 @@ export const dynamicOriginCallbackHandler = (origins) => {
44
44
  if (process.env.PROJECT_BASE_URL) {
45
45
  allowedOrigins.push(ensureProtocol(process.env.PROJECT_BASE_URL));
46
46
  }
47
- if (origins) {
48
- const additionalOrigins = force.array(origins);
47
+ if (origin) {
48
+ const additionalOrigins = force.array(origin);
49
49
  allowedOrigins.push(...additionalOrigins);
50
50
  }
51
51
 
@@ -60,9 +60,9 @@ export const dynamicOriginCallbackHandler = (origins) => {
60
60
 
61
61
  const isAllowed = allowedOrigins.some((allowed) => {
62
62
  if (allowed instanceof RegExp) {
63
- return allowed.test(origin);
63
+ return allowed.test(requestOrigin);
64
64
  }
65
- return origin.includes(allowed);
65
+ return requestOrigin.includes(allowed);
66
66
  });
67
67
 
68
68
  if (isAllowed) {
@@ -79,10 +79,10 @@ export const dynamicOriginCallbackHandler = (origins) => {
79
79
  //
80
80
 
81
81
  const corsHelper = (config = {}) => {
82
- const { origins, overrides = {} } = config;
82
+ const { origin, overrides = {} } = config;
83
83
 
84
84
  const options = {
85
- origin: dynamicOriginCallbackHandler(origins),
85
+ origin: dynamicOriginCallbackHandler(origin),
86
86
  // * The default behavior is to allow any headers and methods so they are not included here
87
87
  ...overrides,
88
88
  };