@middy/http-response-serializer
Advanced tools
Comparing version 5.0.0-alpha.1 to 5.0.0-alpha.2
122
index.js
@@ -1,70 +0,62 @@ | ||
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) | ||
// 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 | ||
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; | ||
} | ||
request.response.headers['Content-Type'] = type | ||
const result = s.serializer(request.response) | ||
if (typeof result === 'object' && 'body' in result) { | ||
request.response = result | ||
// find accept value(s) | ||
let types; | ||
if (request.event.requiredContentType) { | ||
types = [ | ||
request.event.requiredContentType | ||
]; | ||
} else { | ||
// otherwise only replace the body attribute | ||
request.response.body = result | ||
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; | ||
} | ||
request.response.headers['Content-Type'] = type; | ||
const result = s.serializer(request.response); | ||
if (typeof result === 'object' && 'body' in result) { | ||
request.response = result; | ||
} else { | ||
// otherwise only replace the body attribute | ||
request.response.body = result; | ||
} | ||
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; | ||
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.1", | ||
"version": "5.0.0-alpha.2", | ||
"description": "The Http Serializer middleware lets you define serialization mechanisms based on the current content negotiation.", | ||
@@ -67,8 +67,8 @@ "type": "module", | ||
"@hapi/accept": "6.0.2", | ||
"@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" | ||
}, | ||
"gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1" | ||
} |
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
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
8431
72
0
+ 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