@middy/http-json-body-parser 2.5.2 → 3.0.0-alpha.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2021 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
3
+ Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
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
 
@@ -51,13 +53,16 @@ npm install --save @middy/http-json-body-parser
51
53
 
52
54
  ```javascript
53
55
  import middy from '@middy/core'
56
+ import httpHeaderNormalizer from '@middy/http-header-normalizer'
54
57
  import httpJsonBodyParser from '@middy/http-json-body-parser'
55
58
 
56
59
  const handler = middy((event, context) => {
57
60
  return {}
58
61
  })
59
62
 
60
- handler.use(httpJsonBodyParser())
63
+ handler
64
+ .use(httpHeaderNormalizer())
65
+ .use(httpJsonBodyParser())
61
66
 
62
67
  // invokes the handler
63
68
  const event = {
@@ -84,7 +89,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
84
89
 
85
90
  ## License
86
91
 
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).
92
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
88
93
 
89
94
  <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
90
95
  <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
package/index.js CHANGED
@@ -1,5 +1,4 @@
1
- "use strict";
2
-
1
+ import { createError } from '@middy/util';
3
2
  const mimePattern = /^application\/(.+\+)?json(;.*)?$/;
4
3
  const defaults = {
5
4
  reviver: undefined
@@ -11,24 +10,18 @@ const httpJsonBodyParserMiddleware = (opts = {}) => {
11
10
  };
12
11
 
13
12
  const httpJsonBodyParserMiddlewareBefore = async request => {
14
- var _headers$ContentType;
15
-
16
13
  const {
17
14
  headers,
18
15
  body
19
16
  } = 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'];
17
+ const contentTypeHeader = headers['Content-Type'] ?? headers['content-type'];
21
18
 
22
19
  if (mimePattern.test(contentTypeHeader)) {
23
20
  try {
24
21
  const data = request.event.isBase64Encoded ? Buffer.from(body, 'base64').toString() : body;
22
+ request.event.rawBody = body;
25
23
  request.event.body = JSON.parse(data, options.reviver);
26
- } catch (err) {
27
- const {
28
- createError
29
- } = require('@middy/util'); // UnprocessableEntity
30
-
31
-
24
+ } catch (e) {
32
25
  throw createError(422, 'Content type defined as JSON but an invalid JSON was provided');
33
26
  }
34
27
  }
@@ -39,4 +32,4 @@ const httpJsonBodyParserMiddleware = (opts = {}) => {
39
32
  };
40
33
  };
41
34
 
42
- module.exports = httpJsonBodyParserMiddleware;
35
+ export default httpJsonBodyParserMiddleware;
package/package.json CHANGED
@@ -1,18 +1,19 @@
1
1
  {
2
2
  "name": "@middy/http-json-body-parser",
3
- "version": "2.5.2",
3
+ "version": "3.0.0-alpha.1",
4
4
  "description": "Http JSON body parser middleware for the middy framework",
5
- "type": "commonjs",
5
+ "type": "module",
6
6
  "engines": {
7
- "node": ">=12"
7
+ "node": ">=14"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "index.js",
13
+ "exports": "./index.js",
14
14
  "types": "index.d.ts",
15
15
  "files": [
16
+ "index.js",
16
17
  "index.d.ts"
17
18
  ],
18
19
  "scripts": {
@@ -48,11 +49,10 @@
48
49
  },
49
50
  "homepage": "https://github.com/middyjs/middy#readme",
50
51
  "dependencies": {
51
- "@middy/util": "^2.5.2"
52
+ "@middy/util": "^3.0.0-alpha.1"
52
53
  },
53
54
  "devDependencies": {
54
- "@middy/core": "^2.5.2",
55
- "http-errors": "^1.8.0"
55
+ "@middy/core": "^3.0.0-alpha.1"
56
56
  },
57
- "gitHead": "a2bb757a7a13638ae64277f8eecfcf11c1af17d4"
57
+ "gitHead": "a14125c6b2e21b181824f9985a919a47f1e4711f"
58
58
  }