@middy/http-json-body-parser 5.0.3 → 5.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.
- package/index.js +42 -41
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,43 +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
|
-
};
|
|
38
|
-
return {
|
|
39
|
-
before: httpJsonBodyParserMiddlewareBefore
|
|
40
|
-
};
|
|
41
|
-
};
|
|
42
|
-
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
|
+
}
|
|
43
39
|
|
|
40
|
+
return {
|
|
41
|
+
before: httpJsonBodyParserMiddlewareBefore
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export default httpJsonBodyParserMiddleware
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-json-body-parser",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.2.0",
|
|
4
4
|
"description": "Http JSON body parser middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -62,12 +62,12 @@
|
|
|
62
62
|
"url": "https://github.com/sponsors/willfarrell"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@middy/util": "5.0
|
|
65
|
+
"@middy/util": "5.2.0"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
|
-
"@middy/core": "5.0
|
|
68
|
+
"@middy/core": "5.2.0",
|
|
69
69
|
"@types/aws-lambda": "^8.10.101",
|
|
70
70
|
"type-fest": "^4.0.0"
|
|
71
71
|
},
|
|
72
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0"
|
|
73
73
|
}
|