Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@middy/http-json-body-parser

Package Overview
Dependencies
Maintainers
3
Versions
223
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@middy/http-json-body-parser - npm Package Compare versions

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"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc