@middy/ws-router 3.0.0-alpha.8

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
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 ADDED
@@ -0,0 +1,111 @@
1
+ <div align="center">
2
+ <h1>Middy ws-router lambda handler</h1>
3
+ <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
4
+ <p><strong>WebSocket router for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
5
+ <p>
6
+ <a href="https://www.npmjs.com/package/@middy/ws-router?activeTab=versions">
7
+ <img src="https://badge.fury.io/js/%40middy%2Fws-router.svg" alt="npm version" style="max-width:100%;">
8
+ </a>
9
+ <a href="https://packagephobia.com/result?p=@middy/ws-router">
10
+ <img src="https://packagephobia.com/badge?p=@middy/ws-router" alt="npm install size" style="max-width:100%;">
11
+ </a>
12
+ <a href="https://github.com/middyjs/middy/actions">
13
+ <img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
14
+ </a>
15
+ <br/>
16
+ <a href="https://standardjs.com/">
17
+ <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
18
+ </a>
19
+ <a href="https://snyk.io/test/github/middyjs/middy">
20
+ <img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
21
+ </a>
22
+ <a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
23
+ <img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
24
+ </a>
25
+ <a href="https://bestpractices.coreinfrastructure.org/projects/5280">
26
+ <img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
27
+ </a>
28
+ <br/>
29
+ <a href="https://gitter.im/middyjs/Lobby">
30
+ <img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
31
+ </a>
32
+ <a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
33
+ <img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
34
+ </a>
35
+ </p>
36
+ </div>
37
+
38
+ This handler can route to requests to one of a nested handler based on `routeKey` of an WebSocket event from API Gateway (WebSocket).
39
+
40
+ ## Install
41
+
42
+ To install this middleware you can use NPM:
43
+
44
+ ```bash
45
+ npm install --save @middy/ws-router
46
+ ```
47
+
48
+ ## Options
49
+ - `routes` (array[{method, path, handler}]) (required): Array of route objects.
50
+ - `routeKey` (string) (required): AWS formatted route key. ie `$connect`, `$disconnect`, `$default`
51
+ - `handler` (function) (required): Any `handler(event, context, {signal})` function
52
+
53
+ NOTES:
54
+ - Errors should be handled as part of the router middleware stack **or** the lambdaHandler middleware stack. Handled errors in the later will trigger the `after` middleware stack of the former.
55
+ - Shared middlewares, connected to the router middleware stack, can only be run before the lambdaHandler middleware stack.
56
+
57
+ ## Sample usage
58
+
59
+ ```javascript
60
+ import middy from '@middy/core'
61
+ import wsRouterHandler from '@middy/ws-router'
62
+ import wsResponseMiddleware from '@middy/ws-response'
63
+ import validatorMiddleware from '@middy/validator'
64
+
65
+ const connectHandler = middy()
66
+ .use(validatorMiddleware({inputSchema: {...} }))
67
+ .handler((event, context) => {
68
+ return 'connected'
69
+ })
70
+
71
+ const disconnectHandler = middy()
72
+ .use(validatorMiddleware({inputSchema: {...} }))
73
+ .handler((event, context) => {
74
+ return 'disconnected'
75
+ })
76
+
77
+ handler = middy()
78
+ .use(wsResponseMiddleware())
79
+ .handler(wsRouterHandler([
80
+ {
81
+ routeKey: '$connect',
82
+ handler: connectHandler
83
+ },
84
+ {
85
+ routeKey: '$disconnect',
86
+ handler: disconnectHandler
87
+ }
88
+ ]))
89
+
90
+
91
+ module.exports = { handler }
92
+ ```
93
+
94
+
95
+ ## Middy documentation and examples
96
+
97
+ For more documentation and examples, refers to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org).
98
+
99
+
100
+ ## Contributing
101
+
102
+ Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls).
103
+
104
+
105
+ ## License
106
+
107
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
108
+
109
+ <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
110
+ <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
111
+ </a>
package/index.cjs ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _util=require("@middy/util");const wsRouteHandler=routes=>{const routesStatic={};for(const route of routes){const{routeKey,handler}=route;routesStatic[routeKey]=handler}return(event,context,abort)=>{const{routeKey}=event.requestContext??{};if(!routeKey){throw new Error("[ws-router] Unknown ws event format")}const handler=routesStatic[routeKey];if(handler!==undefined){return handler(event,context,abort)}throw(0,_util).createError(404,"Route does not exist")}};var _default=wsRouteHandler;exports.default=_default
2
+
3
+ //# sourceMappingURL=index.cjs.map
package/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ // import middy from '@middy/core'
2
+
3
+ declare function wsRouterHandler (): any
4
+
5
+ export default wsRouterHandler
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import{createError}from"@middy/util";const wsRouteHandler=routes=>{const routesStatic={};for(const route of routes){const{routeKey,handler}=route;routesStatic[routeKey]=handler}return(event,context,abort)=>{const{routeKey}=event.requestContext??{};if(!routeKey){throw new Error("[ws-router] Unknown ws event format")}const handler=routesStatic[routeKey];if(handler!==undefined){return handler(event,context,abort)}throw createError(404,"Route does not exist")}};export default wsRouteHandler
2
+
3
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@middy/ws-router",
3
+ "version": "3.0.0-alpha.8",
4
+ "description": "WebSocket event router for the middy framework",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=14"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "import": "./index.js",
16
+ "require": "./index.cjs",
17
+ "types": "./index.d.ts"
18
+ }
19
+ },
20
+ "types": "index.d.ts",
21
+ "files": [
22
+ "index.js",
23
+ "index.cjs",
24
+ "index.d.ts"
25
+ ],
26
+ "scripts": {
27
+ "test": "npm run test:unit",
28
+ "test:unit": "ava",
29
+ "test:benchmark": "node __benchmarks__/index.js"
30
+ },
31
+ "license": "MIT",
32
+ "keywords": [
33
+ "Lambda",
34
+ "Middleware",
35
+ "Serverless",
36
+ "Framework",
37
+ "AWS",
38
+ "AWS Lambda",
39
+ "Middy",
40
+ "WebSocket",
41
+ "WS",
42
+ "router"
43
+ ],
44
+ "author": {
45
+ "name": "Middy contributors",
46
+ "url": "https://github.com/middyjs/middy/graphs/contributors"
47
+ },
48
+ "repository": {
49
+ "type": "git",
50
+ "url": "github:middyjs/middy",
51
+ "directory": "packages/ws-router"
52
+ },
53
+ "bugs": {
54
+ "url": "https://github.com/middyjs/middy/issues"
55
+ },
56
+ "homepage": "https://middy.js.org",
57
+ "dependencies": {
58
+ "@middy/util": "^3.0.0-alpha.8"
59
+ },
60
+ "devDependencies": {
61
+ "@middy/core": "^3.0.0-alpha.8"
62
+ },
63
+ "gitHead": "c04b3a0c2f326906b34973878d6f823778a5ea99"
64
+ }