@middy/http-router 3.0.0-alpha.0
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 +21 -0
- package/README.md +108 -0
- package/index.d.ts +5 -0
- package/package.json +56 -0
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,108 @@
|
|
|
1
|
+
# Middy http-router lambda handler
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
|
|
5
|
+
</div>
|
|
6
|
+
|
|
7
|
+
<div align="center">
|
|
8
|
+
<p><strong>HTTP router for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
<div align="center">
|
|
12
|
+
<p>
|
|
13
|
+
<a href="http://badge.fury.io/js/%40middy%2Fhttp-router">
|
|
14
|
+
<img src="https://badge.fury.io/js/%40middy%2Fhttp-router.svg" alt="npm version" style="max-width:100%;">
|
|
15
|
+
</a>
|
|
16
|
+
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
17
|
+
<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%;">
|
|
18
|
+
</a>
|
|
19
|
+
<a href="https://standardjs.com/">
|
|
20
|
+
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
|
|
21
|
+
</a>
|
|
22
|
+
<a href="https://gitter.im/middyjs/Lobby">
|
|
23
|
+
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
|
|
24
|
+
</a>
|
|
25
|
+
</p>
|
|
26
|
+
</div>
|
|
27
|
+
|
|
28
|
+
This handler can route to requests to one of a nested hander based on `method` and `path` of an http event from API Gateway (REST or HTTP) or Elastic Load Balancer.
|
|
29
|
+
|
|
30
|
+
## Install
|
|
31
|
+
|
|
32
|
+
To install this middleware you can use NPM:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install --save @middy/http-router
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Options
|
|
39
|
+
- `routes` (array[{method, path, handler}]) (required): Array of route objects.
|
|
40
|
+
- `method` (string) (required): One of `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `OPTIONS` and `ANY` that will match to any method passed in
|
|
41
|
+
- `path` (string) (required): AWS formatted path starting with `/`. Variable: `/{id}/`, Wildcard: `/{proxy+}`
|
|
42
|
+
- `handler` (function) (required): Any `handler(event, context)` function
|
|
43
|
+
|
|
44
|
+
NOTES:
|
|
45
|
+
- Errors should be handled as part of the router middleware stack **or** the baseHandler middleware stack. Handled errors in the later will trigger the `after` middleware stack of the former.
|
|
46
|
+
- Shared middlewares, connected to the router middleware stack, can only be run before the baseHandler middleware stack.
|
|
47
|
+
|
|
48
|
+
## Sample usage
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
import middy from '@middy/core'
|
|
52
|
+
import validatorMiddleware from '@middy/validator'
|
|
53
|
+
|
|
54
|
+
const getHandler = middy()
|
|
55
|
+
.use(validatorMiddleware({inputSchema: {...} }))
|
|
56
|
+
.handler((event, context) => {
|
|
57
|
+
return {
|
|
58
|
+
statusCode: 200,
|
|
59
|
+
body: '{...}'
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
const postHandler = middy()
|
|
64
|
+
.use(validatorMiddleware({inputSchema: {...} }))
|
|
65
|
+
.handler((event, context) => {
|
|
66
|
+
return {
|
|
67
|
+
statusCode: 200,
|
|
68
|
+
body: '{...}'
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
handler = middy()
|
|
73
|
+
.use(httpHeaderNormalizer())
|
|
74
|
+
.handler(httpRouterHandler([
|
|
75
|
+
{
|
|
76
|
+
method: 'GET',
|
|
77
|
+
path: '/user/{id}',
|
|
78
|
+
handler: getHandler
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
method: 'POST',
|
|
82
|
+
path: '/user',
|
|
83
|
+
handler: postHandler
|
|
84
|
+
}
|
|
85
|
+
]))
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
module.exports = { handler }
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
## Middy documentation and examples
|
|
93
|
+
|
|
94
|
+
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).
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
## Contributing
|
|
98
|
+
|
|
99
|
+
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).
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
105
|
+
|
|
106
|
+
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
107
|
+
<img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
|
|
108
|
+
</a>
|
package/index.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@middy/http-router",
|
|
3
|
+
"version": "3.0.0-alpha.0",
|
|
4
|
+
"description": "http 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": "./index.js",
|
|
14
|
+
"types": "index.d.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"index.d.ts"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "npm run test:unit",
|
|
20
|
+
"test:unit": "ava"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"keywords": [
|
|
24
|
+
"Lambda",
|
|
25
|
+
"Middleware",
|
|
26
|
+
"Serverless",
|
|
27
|
+
"Framework",
|
|
28
|
+
"AWS",
|
|
29
|
+
"AWS Lambda",
|
|
30
|
+
"Middy",
|
|
31
|
+
"HTTP",
|
|
32
|
+
"API",
|
|
33
|
+
"Urlencode",
|
|
34
|
+
"Body Parser"
|
|
35
|
+
],
|
|
36
|
+
"author": {
|
|
37
|
+
"name": "Middy contributors",
|
|
38
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "github:middyjs/middy",
|
|
43
|
+
"directory": "packages/http-router"
|
|
44
|
+
},
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/middyjs/middy#readme",
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@middy/util": "^3.0.0-alpha.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@middy/core": "^3.0.0-alpha.0"
|
|
54
|
+
},
|
|
55
|
+
"gitHead": "c533f62841c8a39d061d7b94f30ba178f002c8db"
|
|
56
|
+
}
|