@middy/http-json-body-parser 3.0.0-alpha.3 → 3.0.0-alpha.7

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.
Files changed (3) hide show
  1. package/README.md +24 -14
  2. package/index.js +2 -33
  3. package/package.json +4 -4
package/README.md CHANGED
@@ -1,26 +1,36 @@
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
1
  <div align="center">
2
+ <h1>Middy http-json-body-parser middleware</h1>
3
+ <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
8
4
  <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
5
  <p>
13
- <a href="http://badge.fury.io/js/%40middy%2Fhttp-json-body-parser">
6
+ <a href="https://www.npmjs.com/package/@middy/http-json-body-parser?activeTab=versions">
14
7
  <img src="https://badge.fury.io/js/%40middy%2Fhttp-json-body-parser.svg" alt="npm version" style="max-width:100%;">
15
8
  </a>
9
+ <a href="https://packagephobia.com/result?p=@middy/http-json-body-parser">
10
+ <img src="https://packagephobia.com/badge?p=@middy/http-json-body-parser" 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>
16
19
  <a href="https://snyk.io/test/github/middyjs/middy">
17
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%;">
18
21
  </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%;">
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%;">
21
27
  </a>
28
+ <br/>
22
29
  <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%;">
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%;">
24
34
  </a>
25
35
  </p>
26
36
  </div>
@@ -46,7 +56,7 @@ npm install --save @middy/http-json-body-parser
46
56
 
47
57
  ## Options
48
58
 
49
- - 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.
59
+ - `reviver` (function) (default `undefined`): 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.
50
60
 
51
61
 
52
62
  ## Sample usage
package/index.js CHANGED
@@ -1,34 +1,3 @@
1
- import { createError } from '@middy/util'
2
- const mimePattern = /^application\/(.+\+)?json(;.*)?$/
1
+ import{createError}from'@middy/util';const mimePattern=/^application\/(.+\+)?json(;.*)?$/;const defaults={reviver:undefined};const httpJsonBodyParserMiddleware=(opts={})=>{const options={...defaults,...opts};const httpJsonBodyParserMiddlewareBefore=async request=>{const{headers,body}=request.event;const contentTypeHeader=headers['Content-Type']??headers['content-type'];if(mimePattern.test(contentTypeHeader)){try{const data=request.event.isBase64Encoded?Buffer.from(body,'base64').toString():body;request.event.rawBody=body;request.event.body=JSON.parse(data,options.reviver)}catch(cause){const error=createError(422,'Invalid or malformed JSON was provided');error.cause=cause;throw error}}};return{before:httpJsonBodyParserMiddlewareBefore}};export default httpJsonBodyParserMiddleware
3
2
 
4
- const defaults = {
5
- reviver: undefined
6
- }
7
-
8
- const httpJsonBodyParserMiddleware = (opts = {}) => {
9
- const options = { ...defaults, ...opts }
10
- const httpJsonBodyParserMiddlewareBefore = async (request) => {
11
- const { headers, body } = request.event
12
-
13
- const contentTypeHeader = headers['Content-Type'] ?? headers['content-type']
14
-
15
- if (mimePattern.test(contentTypeHeader)) {
16
- try {
17
- const data = request.event.isBase64Encoded
18
- ? Buffer.from(body, 'base64').toString()
19
- : body
20
-
21
- request.event.rawBody = body
22
- request.event.body = JSON.parse(data, options.reviver)
23
- } catch (e) {
24
- // UnprocessableEntity
25
- throw createError(422, 'Content type defined as JSON but an invalid JSON was provided')
26
- }
27
- }
28
- }
29
-
30
- return {
31
- before: httpJsonBodyParserMiddlewareBefore
32
- }
33
- }
34
- export default httpJsonBodyParserMiddleware
3
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-json-body-parser",
3
- "version": "3.0.0-alpha.3",
3
+ "version": "3.0.0-alpha.7",
4
4
  "description": "Http JSON body parser middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -50,10 +50,10 @@
50
50
  },
51
51
  "homepage": "https://github.com/middyjs/middy#readme",
52
52
  "dependencies": {
53
- "@middy/util": "^3.0.0-alpha.3"
53
+ "@middy/util": "^3.0.0-alpha.7"
54
54
  },
55
55
  "devDependencies": {
56
- "@middy/core": "^3.0.0-alpha.3"
56
+ "@middy/core": "^3.0.0-alpha.7"
57
57
  },
58
- "gitHead": "1441158711580313765e6d156046ef0fade0d156"
58
+ "gitHead": "5cef39ebe49c201f97d71bb0680004de4b82cb91"
59
59
  }