@middy/http-json-body-parser 2.5.2

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-2021 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,91 @@
1
+ # Middy http-json-body-parser middleware
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 json body parser middleware 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-json-body-parser">
14
+ <img src="https://badge.fury.io/js/%40middy%2Fhttp-json-body-parser.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 middleware automatically parses HTTP requests with a JSON body and converts the body into an
29
+ object. Also handles gracefully broken JSON as *UnprocessableEntity* (422 errors)
30
+ if used in combination with `httpErrorHandler`.
31
+
32
+ It can also be used in combination with validator as a prior step to normalize the
33
+ event body input as an object so that the content can be validated.
34
+
35
+
36
+ ## Install
37
+
38
+ To install this middleware you can use NPM:
39
+
40
+ ```bash
41
+ npm install --save @middy/http-json-body-parser
42
+ ```
43
+
44
+
45
+ ## Options
46
+
47
+ - reviver (function) (optional): A [reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Parameters) parameter may be passed which will be used `JSON.parse`ing the body.
48
+
49
+
50
+ ## Sample usage
51
+
52
+ ```javascript
53
+ import middy from '@middy/core'
54
+ import httpJsonBodyParser from '@middy/http-json-body-parser'
55
+
56
+ const handler = middy((event, context) => {
57
+ return {}
58
+ })
59
+
60
+ handler.use(httpJsonBodyParser())
61
+
62
+ // invokes the handler
63
+ const event = {
64
+ headers: {
65
+ 'Content-Type': 'application/json' // It is important that the request has the proper content type.
66
+ },
67
+ body: JSON.stringify({foo: 'bar'})
68
+ }
69
+ handler(event, {}, (_, body) => {
70
+ t.is(body,{foo: 'bar'})
71
+ })
72
+ ```
73
+
74
+
75
+ ## Middy documentation and examples
76
+
77
+ 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).
78
+
79
+
80
+ ## Contributing
81
+
82
+ 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).
83
+
84
+
85
+ ## License
86
+
87
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2021 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
88
+
89
+ <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
90
+ <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
91
+ </a>
package/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import middy from '@middy/core'
2
+
3
+ interface Options {
4
+ reviver?: (key: string, value: any) => any
5
+ }
6
+
7
+ declare function jsonBodyParser (options?: Options): middy.MiddlewareObj
8
+
9
+ export default jsonBodyParser
package/index.js ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ const mimePattern = /^application\/(.+\+)?json(;.*)?$/;
4
+ const defaults = {
5
+ reviver: undefined
6
+ };
7
+
8
+ const httpJsonBodyParserMiddleware = (opts = {}) => {
9
+ const options = { ...defaults,
10
+ ...opts
11
+ };
12
+
13
+ const httpJsonBodyParserMiddlewareBefore = async request => {
14
+ var _headers$ContentType;
15
+
16
+ const {
17
+ headers,
18
+ body
19
+ } = request.event;
20
+ const contentTypeHeader = (_headers$ContentType = headers === null || headers === void 0 ? void 0 : headers['Content-Type']) !== null && _headers$ContentType !== void 0 ? _headers$ContentType : headers === null || headers === void 0 ? void 0 : headers['content-type'];
21
+
22
+ if (mimePattern.test(contentTypeHeader)) {
23
+ try {
24
+ const data = request.event.isBase64Encoded ? Buffer.from(body, 'base64').toString() : body;
25
+ request.event.body = JSON.parse(data, options.reviver);
26
+ } catch (err) {
27
+ const {
28
+ createError
29
+ } = require('@middy/util'); // UnprocessableEntity
30
+
31
+
32
+ throw createError(422, 'Content type defined as JSON but an invalid JSON was provided');
33
+ }
34
+ }
35
+ };
36
+
37
+ return {
38
+ before: httpJsonBodyParserMiddlewareBefore
39
+ };
40
+ };
41
+
42
+ module.exports = httpJsonBodyParserMiddleware;
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@middy/http-json-body-parser",
3
+ "version": "2.5.2",
4
+ "description": "Http JSON body parser middleware for the middy framework",
5
+ "type": "commonjs",
6
+ "engines": {
7
+ "node": ">=12"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "main": "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
+ "JSON",
34
+ "Body Parser",
35
+ "JSON Body Parser"
36
+ ],
37
+ "author": {
38
+ "name": "Middy contributors",
39
+ "url": "https://github.com/middyjs/middy/graphs/contributors"
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "github:middyjs/middy",
44
+ "directory": "packages/http-json-body-parser"
45
+ },
46
+ "bugs": {
47
+ "url": "https://github.com/middyjs/middy/issues"
48
+ },
49
+ "homepage": "https://github.com/middyjs/middy#readme",
50
+ "dependencies": {
51
+ "@middy/util": "^2.5.2"
52
+ },
53
+ "devDependencies": {
54
+ "@middy/core": "^2.5.2",
55
+ "http-errors": "^1.8.0"
56
+ },
57
+ "gitHead": "a2bb757a7a13638ae64277f8eecfcf11c1af17d4"
58
+ }