@middy/http-json-body-parser 6.1.6 → 6.2.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/index.d.ts +13 -7
- package/index.js +48 -48
- package/package.json +71 -74
package/index.d.ts
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
import middy from
|
|
2
|
-
import {
|
|
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
|
-
|
|
6
|
-
|
|
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>
|
|
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
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
6
|
+
reviver: undefined,
|
|
7
|
+
disableContentTypeError: false,
|
|
8
|
+
};
|
|
9
9
|
|
|
10
10
|
const httpJsonBodyParserMiddleware = (opts = {}) => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
"type-fest": "^4.0.0"
|
|
74
|
-
},
|
|
75
|
-
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
2
|
+
"name": "@middy/http-json-body-parser",
|
|
3
|
+
"version": "6.2.1",
|
|
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.1"
|
|
67
|
+
},
|
|
68
|
+
"devDependencies": {
|
|
69
|
+
"@types/aws-lambda": "^8.10.101",
|
|
70
|
+
"type-fest": "^4.0.0"
|
|
71
|
+
},
|
|
72
|
+
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
76
73
|
}
|