@middy/http-response-serializer
Advanced tools
Comparing version 5.0.0-alpha.0 to 5.0.0-alpha.1
119
index.js
@@ -1,59 +0,70 @@ | ||
import { normalizeHttpResponse } from '@middy/util'; | ||
import Accept from '@hapi/accept'; | ||
import { normalizeHttpResponse } from '@middy/util' | ||
import Accept from '@hapi/accept' | ||
const defaults = { | ||
serializers: [], | ||
defaultContentType: undefined | ||
}; | ||
const httpResponseSerializerMiddleware = (opts = {})=>{ | ||
const { serializers , defaultContentType } = { | ||
...defaults, | ||
...opts | ||
}; | ||
const httpResponseSerializerMiddlewareAfter = async (request)=>{ | ||
normalizeHttpResponse(request); | ||
if (request.response.headers['Content-Type'] ?? request.response.headers['content-type']) { | ||
return; | ||
serializers: [], | ||
defaultContentType: undefined | ||
} | ||
const httpResponseSerializerMiddleware = (opts = {}) => { | ||
const { serializers, defaultContentType } = { ...defaults, ...opts } | ||
const httpResponseSerializerMiddlewareAfter = async (request) => { | ||
normalizeHttpResponse(request) | ||
// skip serialization when Content-Type or content-type is already set | ||
if ( | ||
request.response.headers['Content-Type'] ?? | ||
request.response.headers['content-type'] | ||
) { | ||
return | ||
} | ||
// find accept value(s) | ||
let types | ||
if (request.event.requiredContentType) { | ||
types = [request.event.requiredContentType] | ||
} else { | ||
const acceptHeader = | ||
request.event.headers.Accept ?? request.event.headers.accept | ||
types = [ | ||
...((acceptHeader && Accept.mediaTypes(acceptHeader)) ?? []), | ||
request.event.preferredContentType, | ||
defaultContentType | ||
] | ||
} | ||
for (const type of types) { | ||
let breakTypes | ||
for (const s of serializers) { | ||
if (!s.regex.test(type)) { | ||
continue | ||
} | ||
let types; | ||
if (request.event.requiredContentType) { | ||
types = [ | ||
request.event.requiredContentType | ||
]; | ||
request.response.headers['Content-Type'] = type | ||
const result = s.serializer(request.response) | ||
if (typeof result === 'object' && 'body' in result) { | ||
request.response = result | ||
} else { | ||
const acceptHeader = request.event.headers.Accept ?? request.event.headers.accept; | ||
types = [ | ||
...(acceptHeader && Accept.mediaTypes(acceptHeader)) ?? [], | ||
request.event.preferredContentType, | ||
defaultContentType | ||
]; | ||
// otherwise only replace the body attribute | ||
request.response.body = result | ||
} | ||
for (const type of types){ | ||
let breakTypes; | ||
for (const s of serializers){ | ||
if (!s.regex.test(type)) { | ||
continue; | ||
} | ||
request.response.headers['Content-Type'] = type; | ||
const result = s.serializer(request.response); | ||
if (typeof result === 'object' && 'body' in result) { | ||
request.response = result; | ||
} else { | ||
request.response.body = result; | ||
} | ||
breakTypes = true; | ||
break; | ||
} | ||
if (breakTypes) break; | ||
} | ||
}; | ||
const httpResponseSerializerMiddlewareOnError = async (request)=>{ | ||
if (request.response === undefined) return; | ||
return httpResponseSerializerMiddlewareAfter(request); | ||
}; | ||
return { | ||
after: httpResponseSerializerMiddlewareAfter, | ||
onError: httpResponseSerializerMiddlewareOnError | ||
}; | ||
}; | ||
export default httpResponseSerializerMiddleware; | ||
breakTypes = true | ||
break | ||
} | ||
if (breakTypes) break | ||
} | ||
} | ||
const httpResponseSerializerMiddlewareOnError = async (request) => { | ||
if (request.response === undefined) return | ||
await httpResponseSerializerMiddlewareAfter(request) | ||
} | ||
return { | ||
after: httpResponseSerializerMiddlewareAfter, | ||
onError: httpResponseSerializerMiddlewareOnError | ||
} | ||
} | ||
export default httpResponseSerializerMiddleware |
{ | ||
"name": "@middy/http-response-serializer", | ||
"version": "5.0.0-alpha.0", | ||
"version": "5.0.0-alpha.1", | ||
"description": "The Http Serializer middleware lets you define serialization mechanisms based on the current content negotiation.", | ||
@@ -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" | ||
@@ -75,9 +69,9 @@ ], | ||
"dependencies": { | ||
"@hapi/accept": "6.0.1", | ||
"@middy/util": "5.0.0-alpha.0" | ||
"@hapi/accept": "6.0.2", | ||
"@middy/util": "5.0.0-alpha.1" | ||
}, | ||
"devDependencies": { | ||
"@middy/core": "5.0.0-alpha.0" | ||
"@middy/core": "5.0.0-alpha.1" | ||
}, | ||
"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"> |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
47
8132
5
71
1
+ Added@hapi/accept@6.0.2(transitive)
+ Added@middy/util@5.0.0-alpha.1(transitive)
- Removed@hapi/accept@6.0.1(transitive)
- Removed@middy/util@5.0.0-alpha.0(transitive)
Updated@hapi/accept@6.0.2
Updated@middy/util@5.0.0-alpha.1