@middy/ws-router 6.1.6 → 6.2.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/index.d.ts +6 -6
- package/index.js +38 -36
- package/package.json +69 -72
package/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import middy from
|
|
2
|
-
import { APIGatewayProxyWebsocketHandlerV2 } from
|
|
1
|
+
import type middy from "@middy/core";
|
|
2
|
+
import type { APIGatewayProxyWebsocketHandlerV2 } from "aws-lambda";
|
|
3
3
|
|
|
4
4
|
interface Route<T = never> {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
routeKey: string;
|
|
6
|
+
handler: APIGatewayProxyWebsocketHandlerV2<T>;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
declare function wsRouterHandler
|
|
9
|
+
declare function wsRouterHandler(routes: Route[]): middy.MiddyfiedHandler;
|
|
10
10
|
|
|
11
|
-
export default wsRouterHandler
|
|
11
|
+
export default wsRouterHandler;
|
package/index.js
CHANGED
|
@@ -1,44 +1,46 @@
|
|
|
1
|
-
import { createError } from
|
|
1
|
+
import { createError } from "@middy/util";
|
|
2
2
|
const defaults = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
3
|
+
routes: [],
|
|
4
|
+
notFoundResponse: ({ routeKey }) => {
|
|
5
|
+
const err = createError(404, "Route does not exist", {
|
|
6
|
+
cause: { package: "@middy/ws-router", data: { routeKey } },
|
|
7
|
+
});
|
|
8
|
+
throw err;
|
|
9
|
+
},
|
|
10
|
+
};
|
|
11
11
|
const wsRouteHandler = (opts = {}) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
let options;
|
|
13
|
+
if (Array.isArray(opts)) {
|
|
14
|
+
options = { routes: opts };
|
|
15
|
+
}
|
|
16
|
+
options ??= opts;
|
|
17
|
+
const { routes, notFoundResponse } = { ...defaults, ...options };
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
const routesStatic = {};
|
|
20
|
+
for (const route of routes) {
|
|
21
|
+
const { routeKey, handler } = route;
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
// Static
|
|
24
|
+
routesStatic[routeKey] = handler;
|
|
25
|
+
}
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
return (event, context, abort) => {
|
|
28
|
+
const { routeKey } = event.requestContext ?? {};
|
|
29
|
+
if (!routeKey) {
|
|
30
|
+
throw createError(400, "Unknown WebSocket event format", {
|
|
31
|
+
cause: { package: "@middy/ws-router", data: { routeKey } },
|
|
32
|
+
});
|
|
33
|
+
}
|
|
32
34
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
// Static
|
|
36
|
+
if (Object.hasOwnProperty.call(routesStatic, routeKey)) {
|
|
37
|
+
const handler = routesStatic[routeKey];
|
|
38
|
+
return handler(event, context, abort);
|
|
39
|
+
}
|
|
38
40
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
41
|
+
// Not Found
|
|
42
|
+
return notFoundResponse({ routeKey });
|
|
43
|
+
};
|
|
44
|
+
};
|
|
43
45
|
|
|
44
|
-
export default wsRouteHandler
|
|
46
|
+
export default wsRouteHandler;
|
package/package.json
CHANGED
|
@@ -1,74 +1,71 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
"@types/aws-lambda": "^8.10.100"
|
|
72
|
-
},
|
|
73
|
-
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
2
|
+
"name": "@middy/ws-router",
|
|
3
|
+
"version": "6.2.1",
|
|
4
|
+
"description": "WebSocket event router for the middy framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"types": "index.d.ts",
|
|
26
|
+
"files": ["index.js", "index.d.ts"],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "npm run test:unit && npm run test:fuzz",
|
|
29
|
+
"test:unit": "node --test",
|
|
30
|
+
"test:fuzz": "node --test index.fuzz.js",
|
|
31
|
+
"test:perf": "node --test index.perf.js"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"Lambda",
|
|
36
|
+
"Middleware",
|
|
37
|
+
"Serverless",
|
|
38
|
+
"Framework",
|
|
39
|
+
"AWS",
|
|
40
|
+
"AWS Lambda",
|
|
41
|
+
"Middy",
|
|
42
|
+
"WebSocket",
|
|
43
|
+
"WS",
|
|
44
|
+
"router"
|
|
45
|
+
],
|
|
46
|
+
"author": {
|
|
47
|
+
"name": "Middy contributors",
|
|
48
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
49
|
+
},
|
|
50
|
+
"repository": {
|
|
51
|
+
"type": "git",
|
|
52
|
+
"url": "git+https://github.com/middyjs/middy.git",
|
|
53
|
+
"directory": "packages/ws-router"
|
|
54
|
+
},
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://middy.js.org",
|
|
59
|
+
"funding": {
|
|
60
|
+
"type": "github",
|
|
61
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
62
|
+
},
|
|
63
|
+
"dependencies": {
|
|
64
|
+
"@middy/util": "6.2.1"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@middy/core": "6.2.1",
|
|
68
|
+
"@types/aws-lambda": "^8.10.100"
|
|
69
|
+
},
|
|
70
|
+
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
74
71
|
}
|