@middy/http-json-body-parser
Advanced tools
Comparing version 5.0.0-alpha.1 to 5.0.0-alpha.2
84
index.js
@@ -1,44 +0,44 @@ | ||
import { createError } from '@middy/util' | ||
const mimePattern = /^application\/(.+\+)?json($|;.+)/ | ||
import { createError } from '@middy/util'; | ||
const mimePattern = /^application\/(.+\+)?json($|;.+)/; | ||
const defaults = { | ||
reviver: undefined, | ||
disableContentTypeError: false | ||
} | ||
reviver: undefined, | ||
disableContentTypeError: false | ||
}; | ||
const httpJsonBodyParserMiddleware = (opts = {})=>{ | ||
const options = { | ||
...defaults, | ||
...opts | ||
}; | ||
const httpJsonBodyParserMiddlewareBefore = async (request)=>{ | ||
const { headers, body } = request.event; | ||
const contentType = headers?.['Content-Type'] ?? headers?.['content-type']; | ||
if (!mimePattern.test(contentType)) { | ||
if (options.disableContentTypeError) { | ||
return; | ||
} | ||
throw createError(415, 'Unsupported Media Type', { | ||
cause: { | ||
package: '@middy/http-json-body-parser', | ||
data: contentType | ||
} | ||
}); | ||
} | ||
try { | ||
const data = request.event.isBase64Encoded ? Buffer.from(body, 'base64').toString() : body; | ||
request.event.body = JSON.parse(data, options.reviver); | ||
} catch (err) { | ||
// UnprocessableEntity | ||
throw createError(415, 'Invalid or malformed JSON was provided', { | ||
cause: { | ||
package: '@middy/http-json-body-parser', | ||
data: err | ||
} | ||
}); | ||
} | ||
}; | ||
return { | ||
before: httpJsonBodyParserMiddlewareBefore | ||
}; | ||
}; | ||
export default httpJsonBodyParserMiddleware; | ||
const httpJsonBodyParserMiddleware = (opts = {}) => { | ||
const options = { ...defaults, ...opts } | ||
const httpJsonBodyParserMiddlewareBefore = async (request) => { | ||
const { headers, body } = request.event | ||
const contentType = headers?.['Content-Type'] ?? headers?.['content-type'] | ||
if (!mimePattern.test(contentType)) { | ||
if (options.disableContentTypeError) { | ||
return | ||
} | ||
throw createError(415, 'Unsupported Media Type', { | ||
cause: { package: '@middy/http-json-body-parser', data: contentType } | ||
}) | ||
} | ||
try { | ||
const data = request.event.isBase64Encoded | ||
? Buffer.from(body, 'base64').toString() | ||
: body | ||
request.event.body = JSON.parse(data, options.reviver) | ||
} catch (err) { | ||
// UnprocessableEntity | ||
throw createError(415, 'Invalid or malformed JSON was provided', { | ||
cause: { package: '@middy/http-json-body-parser', data: err } | ||
}) | ||
} | ||
} | ||
return { | ||
before: httpJsonBodyParserMiddlewareBefore | ||
} | ||
} | ||
export default httpJsonBodyParserMiddleware |
{ | ||
"name": "@middy/http-json-body-parser", | ||
"version": "5.0.0-alpha.1", | ||
"version": "5.0.0-alpha.2", | ||
"description": "Http JSON body parser middleware for the middy framework", | ||
@@ -65,6 +65,6 @@ "type": "module", | ||
"dependencies": { | ||
"@middy/util": "5.0.0-alpha.1" | ||
"@middy/util": "5.0.0-alpha.2" | ||
}, | ||
"devDependencies": { | ||
"@middy/core": "5.0.0-alpha.1", | ||
"@middy/core": "5.0.0-alpha.2", | ||
"@types/aws-lambda": "^8.10.101", | ||
@@ -71,0 +71,0 @@ "type-fest": "^4.0.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7979
59
+ Added@middy/util@5.0.0-alpha.2(transitive)
- Removed@middy/util@5.0.0-alpha.1(transitive)
Updated@middy/util@5.0.0-alpha.2