@middy/http-json-body-parser 2.5.2 → 2.5.6

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 +2 -0
  2. package/index.js +20 -27
  3. package/package.json +4 -5
package/README.md CHANGED
@@ -32,6 +32,8 @@ if used in combination with `httpErrorHandler`.
32
32
  It can also be used in combination with validator as a prior step to normalize the
33
33
  event body input as an object so that the content can be validated.
34
34
 
35
+ If the body has been parsed as JSON, you can access the original body (e.g. for webhook signature validation) through the `request.event.rawBody`.
36
+
35
37
 
36
38
  ## Install
37
39
 
package/index.js CHANGED
@@ -1,42 +1,35 @@
1
- "use strict";
1
+ const mimePattern = /^application\/(.+\+)?json(;.*)?$/
2
2
 
3
- const mimePattern = /^application\/(.+\+)?json(;.*)?$/;
4
3
  const defaults = {
5
4
  reviver: undefined
6
- };
5
+ }
7
6
 
8
7
  const httpJsonBodyParserMiddleware = (opts = {}) => {
9
- const options = { ...defaults,
10
- ...opts
11
- };
8
+ const options = { ...defaults, ...opts }
9
+ const httpJsonBodyParserMiddlewareBefore = async (request) => {
10
+ const { headers, body } = request.event
12
11
 
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'];
12
+ const contentTypeHeader =
13
+ headers?.['Content-Type'] ?? headers?.['content-type']
21
14
 
22
15
  if (mimePattern.test(contentTypeHeader)) {
23
16
  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
-
17
+ const data = request.event.isBase64Encoded
18
+ ? Buffer.from(body, 'base64').toString()
19
+ : body
31
20
 
32
- throw createError(422, 'Content type defined as JSON but an invalid JSON was provided');
21
+ request.event.rawBody = body
22
+ request.event.body = JSON.parse(data, options.reviver)
23
+ } catch (err) {
24
+ const { createError } = require('@middy/util')
25
+ // UnprocessableEntity
26
+ throw createError(422, 'Content type defined as JSON but an invalid JSON was provided')
33
27
  }
34
28
  }
35
- };
29
+ }
36
30
 
37
31
  return {
38
32
  before: httpJsonBodyParserMiddlewareBefore
39
- };
40
- };
41
-
42
- module.exports = httpJsonBodyParserMiddleware;
33
+ }
34
+ }
35
+ module.exports = httpJsonBodyParserMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-json-body-parser",
3
- "version": "2.5.2",
3
+ "version": "2.5.6",
4
4
  "description": "Http JSON body parser middleware for the middy framework",
5
5
  "type": "commonjs",
6
6
  "engines": {
@@ -48,11 +48,10 @@
48
48
  },
49
49
  "homepage": "https://github.com/middyjs/middy#readme",
50
50
  "dependencies": {
51
- "@middy/util": "^2.5.2"
51
+ "@middy/util": "^2.5.6"
52
52
  },
53
53
  "devDependencies": {
54
- "@middy/core": "^2.5.2",
55
- "http-errors": "^1.8.0"
54
+ "@middy/core": "^2.5.6"
56
55
  },
57
- "gitHead": "a2bb757a7a13638ae64277f8eecfcf11c1af17d4"
56
+ "gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b"
58
57
  }