@middy/http-json-body-parser 4.6.5 → 5.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/index.d.ts +5 -3
- package/index.js +42 -36
- package/package.json +5 -11
- package/index.cjs +0 -48
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import middy from '@middy/core'
|
|
2
|
-
import { APIGatewayEvent } from 'aws-lambda'
|
|
2
|
+
import { APIGatewayEvent, APIGatewayProxyEventV2 } from 'aws-lambda'
|
|
3
3
|
import { JsonValue } from 'type-fest'
|
|
4
4
|
|
|
5
5
|
interface Options {
|
|
@@ -7,13 +7,15 @@ interface Options {
|
|
|
7
7
|
disableContentTypeError?: boolean
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
type VersionedApiGatewayEvent = APIGatewayEvent | APIGatewayProxyEventV2
|
|
11
|
+
|
|
12
|
+
export type Event<APIGatewayEventType extends VersionedApiGatewayEvent = VersionedApiGatewayEvent> = Omit<APIGatewayEventType, 'body'> & {
|
|
11
13
|
/**
|
|
12
14
|
* The body of the HTTP request.
|
|
13
15
|
*/
|
|
14
16
|
body: JsonValue
|
|
15
17
|
}
|
|
16
18
|
|
|
17
|
-
declare function jsonBodyParser (options?: Options): middy.MiddlewareObj<Event
|
|
19
|
+
declare function jsonBodyParser<APIGatewayEventType extends VersionedApiGatewayEvent = VersionedApiGatewayEvent> (options?: Options): middy.MiddlewareObj<Event<APIGatewayEventType>>
|
|
18
20
|
|
|
19
21
|
export default jsonBodyParser
|
package/index.js
CHANGED
|
@@ -1,38 +1,44 @@
|
|
|
1
|
-
import { createError } from '@middy/util'
|
|
2
|
-
|
|
1
|
+
import { createError } from '@middy/util'
|
|
2
|
+
|
|
3
|
+
const mimePattern = /^application\/(.+\+)?json($|;.+)/
|
|
4
|
+
|
|
3
5
|
const defaults = {
|
|
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
|
-
export default httpJsonBodyParserMiddleware;
|
|
6
|
+
reviver: undefined,
|
|
7
|
+
disableContentTypeError: false
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const httpJsonBodyParserMiddleware = (opts = {}) => {
|
|
11
|
+
const options = { ...defaults, ...opts }
|
|
12
|
+
const httpJsonBodyParserMiddlewareBefore = async (request) => {
|
|
13
|
+
const { headers, body } = request.event
|
|
14
|
+
|
|
15
|
+
const contentType = headers?.['Content-Type'] ?? headers?.['content-type']
|
|
16
|
+
|
|
17
|
+
if (!mimePattern.test(contentType)) {
|
|
18
|
+
if (options.disableContentTypeError) {
|
|
19
|
+
return
|
|
20
|
+
}
|
|
21
|
+
throw createError(415, 'Unsupported Media Type', {
|
|
22
|
+
cause: { package: '@middy/http-json-body-parser', data: contentType }
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
const data = request.event.isBase64Encoded
|
|
28
|
+
? Buffer.from(body, 'base64').toString()
|
|
29
|
+
: body
|
|
30
|
+
|
|
31
|
+
request.event.body = JSON.parse(data, options.reviver)
|
|
32
|
+
} catch (err) {
|
|
33
|
+
// UnprocessableEntity
|
|
34
|
+
throw createError(415, 'Invalid or malformed JSON was provided', {
|
|
35
|
+
cause: { package: '@middy/http-json-body-parser', data: err }
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
}
|
|
38
39
|
|
|
40
|
+
return {
|
|
41
|
+
before: httpJsonBodyParserMiddlewareBefore
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export default httpJsonBodyParserMiddleware
|
package/package.json
CHANGED
|
@@ -1,33 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-json-body-parser",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-alpha.1",
|
|
4
4
|
"description": "Http JSON body parser middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=18"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"main": "./index.cjs",
|
|
14
13
|
"module": "./index.js",
|
|
15
14
|
"exports": {
|
|
16
15
|
".": {
|
|
17
16
|
"import": {
|
|
18
17
|
"types": "./index.d.ts",
|
|
19
18
|
"default": "./index.js"
|
|
20
|
-
},
|
|
21
|
-
"require": {
|
|
22
|
-
"types": "./index.d.ts",
|
|
23
|
-
"default": "./index.cjs"
|
|
24
19
|
}
|
|
25
20
|
}
|
|
26
21
|
},
|
|
27
22
|
"types": "index.d.ts",
|
|
28
23
|
"files": [
|
|
29
24
|
"index.js",
|
|
30
|
-
"index.cjs",
|
|
31
25
|
"index.d.ts"
|
|
32
26
|
],
|
|
33
27
|
"scripts": {
|
|
@@ -68,12 +62,12 @@
|
|
|
68
62
|
"url": "https://github.com/sponsors/willfarrell"
|
|
69
63
|
},
|
|
70
64
|
"dependencies": {
|
|
71
|
-
"@middy/util": "
|
|
65
|
+
"@middy/util": "5.0.0-alpha.1"
|
|
72
66
|
},
|
|
73
67
|
"devDependencies": {
|
|
74
|
-
"@middy/core": "
|
|
68
|
+
"@middy/core": "5.0.0-alpha.1",
|
|
75
69
|
"@types/aws-lambda": "^8.10.101",
|
|
76
70
|
"type-fest": "^4.0.0"
|
|
77
71
|
},
|
|
78
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
|
|
79
73
|
}
|
package/index.cjs
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(module, "exports", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return _default;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _util = require("@middy/util");
|
|
12
|
-
const mimePattern = /^application\/(.+\+)?json($|;.+)/;
|
|
13
|
-
const defaults = {
|
|
14
|
-
reviver: undefined,
|
|
15
|
-
disableContentTypeError: true
|
|
16
|
-
};
|
|
17
|
-
const httpJsonBodyParserMiddleware = (opts = {})=>{
|
|
18
|
-
const options = {
|
|
19
|
-
...defaults,
|
|
20
|
-
...opts
|
|
21
|
-
};
|
|
22
|
-
const httpJsonBodyParserMiddlewareBefore = async (request)=>{
|
|
23
|
-
const { headers, body } = request.event;
|
|
24
|
-
const contentType = headers?.['Content-Type'] ?? headers?.['content-type'];
|
|
25
|
-
if (!mimePattern.test(contentType)) {
|
|
26
|
-
if (options.disableContentTypeError) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
throw (0, _util.createError)(415, 'Unsupported Media Type', {
|
|
30
|
-
cause: contentType
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
try {
|
|
34
|
-
const data = request.event.isBase64Encoded ? Buffer.from(body, 'base64').toString() : body;
|
|
35
|
-
request.event.body = JSON.parse(data, options.reviver);
|
|
36
|
-
} catch (cause) {
|
|
37
|
-
// UnprocessableEntity
|
|
38
|
-
throw (0, _util.createError)(415, 'Invalid or malformed JSON was provided', {
|
|
39
|
-
cause
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
return {
|
|
44
|
-
before: httpJsonBodyParserMiddlewareBefore
|
|
45
|
-
};
|
|
46
|
-
};
|
|
47
|
-
const _default = httpJsonBodyParserMiddleware;
|
|
48
|
-
|