@middy/http-json-body-parser 6.1.6 → 6.2.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.
Files changed (3) hide show
  1. package/index.d.ts +13 -7
  2. package/index.js +48 -48
  3. package/package.json +71 -74
package/index.d.ts CHANGED
@@ -1,13 +1,19 @@
1
- import middy from '@middy/core'
2
- import { APIGatewayEvent, APIGatewayProxyEventV2, ALBEvent } from 'aws-lambda'
1
+ import type middy from "@middy/core";
2
+ import type {
3
+ ALBEvent,
4
+ APIGatewayEvent,
5
+ APIGatewayProxyEventV2,
6
+ } from "aws-lambda";
3
7
 
4
8
  interface Options {
5
- reviver?: (key: string, value: any) => any
6
- disableContentTypeError?: boolean
9
+ reviver?: (key: string, value: any) => any;
10
+ disableContentTypeError?: boolean;
7
11
  }
8
12
 
9
- export type RequestEvent = APIGatewayEvent | APIGatewayProxyEventV2 | ALBEvent
13
+ export type RequestEvent = APIGatewayEvent | APIGatewayProxyEventV2 | ALBEvent;
10
14
 
11
- declare function jsonBodyParser<EventType extends RequestEvent = RequestEvent> (options?: Options): middy.MiddlewareObj<EventType>
15
+ declare function jsonBodyParser<EventType extends RequestEvent = RequestEvent>(
16
+ options?: Options,
17
+ ): middy.MiddlewareObj<EventType>;
12
18
 
13
- export default jsonBodyParser
19
+ export default jsonBodyParser;
package/index.js CHANGED
@@ -1,53 +1,53 @@
1
- import { createError } from '@middy/util'
1
+ import { createError } from "@middy/util";
2
2
 
3
- const mimePattern = /^application\/(.+\+)?json($|;.+)/
3
+ const mimePattern = /^application\/(.+\+)?json($|;.+)/;
4
4
 
5
5
  const defaults = {
6
- reviver: undefined,
7
- disableContentTypeError: false
8
- }
6
+ reviver: undefined,
7
+ disableContentTypeError: false,
8
+ };
9
9
 
10
10
  const httpJsonBodyParserMiddleware = (opts = {}) => {
11
- const options = { ...defaults, ...opts }
12
- const httpJsonBodyParserMiddlewareBefore = async (request) => {
13
- const { headers, body } = request.event
14
- const contentType = headers?.['content-type'] ?? headers?.['Content-Type']
15
-
16
- if (!mimePattern.test(contentType)) {
17
- if (options.disableContentTypeError) {
18
- return
19
- }
20
- throw createError(415, 'Unsupported Media Type', {
21
- cause: { package: '@middy/http-json-body-parser', data: contentType }
22
- })
23
- }
24
-
25
- if (typeof body === 'undefined') {
26
- throw createError(415, 'Invalid or malformed JSON was provided', {
27
- cause: { package: '@middy/http-json-body-parser', data: body }
28
- })
29
- }
30
-
31
- try {
32
- const data = request.event.isBase64Encoded
33
- ? Buffer.from(body, 'base64').toString()
34
- : body
35
-
36
- request.event.body = JSON.parse(data, options.reviver)
37
- } catch (err) {
38
- // UnprocessableEntity
39
- throw createError(415, 'Invalid or malformed JSON was provided', {
40
- cause: {
41
- package: '@middy/http-json-body-parser',
42
- data: body,
43
- message: err.message
44
- }
45
- })
46
- }
47
- }
48
-
49
- return {
50
- before: httpJsonBodyParserMiddlewareBefore
51
- }
52
- }
53
- export default httpJsonBodyParserMiddleware
11
+ const options = { ...defaults, ...opts };
12
+ const httpJsonBodyParserMiddlewareBefore = async (request) => {
13
+ const { headers, body } = request.event;
14
+ const contentType = headers?.["content-type"] ?? headers?.["Content-Type"];
15
+
16
+ if (!mimePattern.test(contentType)) {
17
+ if (options.disableContentTypeError) {
18
+ return;
19
+ }
20
+ throw createError(415, "Unsupported Media Type", {
21
+ cause: { package: "@middy/http-json-body-parser", data: contentType },
22
+ });
23
+ }
24
+
25
+ if (typeof body === "undefined") {
26
+ throw createError(415, "Invalid or malformed JSON was provided", {
27
+ cause: { package: "@middy/http-json-body-parser", data: body },
28
+ });
29
+ }
30
+
31
+ try {
32
+ const data = request.event.isBase64Encoded
33
+ ? Buffer.from(body, "base64").toString()
34
+ : body;
35
+
36
+ request.event.body = JSON.parse(data, options.reviver);
37
+ } catch (err) {
38
+ // UnprocessableEntity
39
+ throw createError(415, "Invalid or malformed JSON was provided", {
40
+ cause: {
41
+ package: "@middy/http-json-body-parser",
42
+ data: body,
43
+ message: err.message,
44
+ },
45
+ });
46
+ }
47
+ };
48
+
49
+ return {
50
+ before: httpJsonBodyParserMiddlewareBefore,
51
+ };
52
+ };
53
+ export default httpJsonBodyParserMiddleware;
package/package.json CHANGED
@@ -1,76 +1,73 @@
1
1
  {
2
- "name": "@middy/http-json-body-parser",
3
- "version": "6.1.6",
4
- "description": "Http JSON body parser middleware for the middy framework",
5
- "type": "module",
6
- "engines": {
7
- "node": ">=20"
8
- },
9
- "engineStrict": true,
10
- "publishConfig": {
11
- "access": "public"
12
- },
13
- "module": "./index.js",
14
- "exports": {
15
- ".": {
16
- "import": {
17
- "types": "./index.d.ts",
18
- "default": "./index.js"
19
- },
20
- "require": {
21
- "default": "./index.js"
22
- }
23
- }
24
- },
25
- "types": "index.d.ts",
26
- "files": [
27
- "index.js",
28
- "index.d.ts"
29
- ],
30
- "scripts": {
31
- "test": "npm run test:unit && npm run test:fuzz",
32
- "test:unit": "node --test __tests__/index.js",
33
- "test:fuzz": "node --test __tests__/fuzz.js",
34
- "test:benchmark": "node __benchmarks__/index.js"
35
- },
36
- "license": "MIT",
37
- "keywords": [
38
- "Lambda",
39
- "Middleware",
40
- "Serverless",
41
- "Framework",
42
- "AWS",
43
- "AWS Lambda",
44
- "Middy",
45
- "HTTP",
46
- "API",
47
- "JSON",
48
- "Body Parser",
49
- "JSON Body Parser"
50
- ],
51
- "author": {
52
- "name": "Middy contributors",
53
- "url": "https://github.com/middyjs/middy/graphs/contributors"
54
- },
55
- "repository": {
56
- "type": "git",
57
- "url": "git+https://github.com/middyjs/middy.git",
58
- "directory": "packages/http-json-body-parser"
59
- },
60
- "bugs": {
61
- "url": "https://github.com/middyjs/middy/issues"
62
- },
63
- "homepage": "https://middy.js.org",
64
- "funding": {
65
- "type": "github",
66
- "url": "https://github.com/sponsors/willfarrell"
67
- },
68
- "dependencies": {
69
- "@middy/util": "6.1.6"
70
- },
71
- "devDependencies": {
72
- "@types/aws-lambda": "^8.10.101",
73
- "type-fest": "^4.0.0"
74
- },
75
- "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
2
+ "name": "@middy/http-json-body-parser",
3
+ "version": "6.2.0",
4
+ "description": "Http JSON body parser middleware for the middy framework",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=20"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "module": "./index.js",
14
+ "exports": {
15
+ ".": {
16
+ "import": {
17
+ "types": "./index.d.ts",
18
+ "default": "./index.js"
19
+ },
20
+ "require": {
21
+ "default": "./index.js"
22
+ }
23
+ }
24
+ },
25
+ "types": "index.d.ts",
26
+ "files": ["index.js", "index.d.ts"],
27
+ "scripts": {
28
+ "test": "npm run test:unit && npm run test:fuzz",
29
+ "test:unit": "node --test",
30
+ "test:fuzz": "node --test index.fuzz.js",
31
+ "test:perf": "node --test index.perf.js"
32
+ },
33
+ "license": "MIT",
34
+ "keywords": [
35
+ "Lambda",
36
+ "Middleware",
37
+ "Serverless",
38
+ "Framework",
39
+ "AWS",
40
+ "AWS Lambda",
41
+ "Middy",
42
+ "HTTP",
43
+ "API",
44
+ "JSON",
45
+ "Body Parser",
46
+ "JSON Body Parser"
47
+ ],
48
+ "author": {
49
+ "name": "Middy contributors",
50
+ "url": "https://github.com/middyjs/middy/graphs/contributors"
51
+ },
52
+ "repository": {
53
+ "type": "git",
54
+ "url": "git+https://github.com/middyjs/middy.git",
55
+ "directory": "packages/http-json-body-parser"
56
+ },
57
+ "bugs": {
58
+ "url": "https://github.com/middyjs/middy/issues"
59
+ },
60
+ "homepage": "https://middy.js.org",
61
+ "funding": {
62
+ "type": "github",
63
+ "url": "https://github.com/sponsors/willfarrell"
64
+ },
65
+ "dependencies": {
66
+ "@middy/util": "6.2.0"
67
+ },
68
+ "devDependencies": {
69
+ "@types/aws-lambda": "^8.10.101",
70
+ "type-fest": "^4.0.0"
71
+ },
72
+ "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
76
73
  }