@middy/http-json-body-parser
Advanced tools
Comparing version 5.0.0-alpha.0 to 5.0.0-alpha.1
import middy from '@middy/core' | ||
import { APIGatewayEvent } from 'aws-lambda' | ||
import { APIGatewayEvent, APIGatewayProxyEventV2 } from 'aws-lambda' | ||
import { JsonValue } from 'type-fest' | ||
@@ -10,3 +10,5 @@ | ||
export type Event = Omit<APIGatewayEvent, 'body'> & { | ||
type VersionedApiGatewayEvent = APIGatewayEvent | APIGatewayProxyEventV2 | ||
export type Event<APIGatewayEventType extends VersionedApiGatewayEvent = VersionedApiGatewayEvent> = Omit<APIGatewayEventType, 'body'> & { | ||
/** | ||
@@ -18,4 +20,4 @@ * The body of the HTTP request. | ||
declare function jsonBodyParser (options?: Options): middy.MiddlewareObj<Event> | ||
declare function jsonBodyParser<APIGatewayEventType extends VersionedApiGatewayEvent = VersionedApiGatewayEvent> (options?: Options): middy.MiddlewareObj<Event<APIGatewayEventType>> | ||
export default jsonBodyParser |
77
index.js
@@ -1,37 +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: true | ||
}; | ||
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: contentType | ||
}); | ||
} | ||
try { | ||
const data = request.event.isBase64Encoded ? Buffer.from(body, 'base64').toString() : body; | ||
request.event.body = JSON.parse(data, options.reviver); | ||
} catch (cause) { | ||
throw createError(415, 'Invalid or malformed JSON was provided', { | ||
cause | ||
}); | ||
} | ||
}; | ||
return { | ||
before: httpJsonBodyParserMiddlewareBefore | ||
}; | ||
}; | ||
export default httpJsonBodyParserMiddleware; | ||
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 |
{ | ||
"name": "@middy/http-json-body-parser", | ||
"version": "5.0.0-alpha.0", | ||
"version": "5.0.0-alpha.1", | ||
"description": "Http JSON body parser middleware for the middy framework", | ||
@@ -13,3 +13,2 @@ "type": "module", | ||
}, | ||
"main": "./index.cjs", | ||
"module": "./index.js", | ||
@@ -21,6 +20,2 @@ "exports": { | ||
"default": "./index.js" | ||
}, | ||
"require": { | ||
"types": "./index.d.ts", | ||
"default": "./index.cjs" | ||
} | ||
@@ -32,3 +27,2 @@ } | ||
"index.js", | ||
"index.cjs", | ||
"index.d.ts" | ||
@@ -74,10 +68,10 @@ ], | ||
"dependencies": { | ||
"@middy/util": "5.0.0-alpha.0" | ||
"@middy/util": "5.0.0-alpha.1" | ||
}, | ||
"devDependencies": { | ||
"@middy/core": "5.0.0-alpha.0", | ||
"@middy/core": "5.0.0-alpha.1", | ||
"@types/aws-lambda": "^8.10.101", | ||
"type-fest": "^3.0.0" | ||
"type-fest": "^4.0.0" | ||
}, | ||
"gitHead": "08c35e3dba9efdad0b86666ce206ce302cc65d07" | ||
"gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1" | ||
} |
@@ -22,4 +22,5 @@ <div align="center"> | ||
</a> | ||
<a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript"> | ||
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;"> | ||
<a href="https://github.com/middyjs/middy/actions/workflows/sast.yml"> | ||
<img src="https://github.com/middyjs/middy/actions/workflows/sast.yml/badge.svg | ||
?branch=main&event=push" alt="CodeQL" style="max-width:100%;"> | ||
</a> | ||
@@ -26,0 +27,0 @@ <a href="https://bestpractices.coreinfrastructure.org/projects/5280"> |
47
7735
5
52
+ Added@middy/util@5.0.0-alpha.1(transitive)
- Removed@middy/util@5.0.0-alpha.0(transitive)
Updated@middy/util@5.0.0-alpha.1