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

@middy/http-response-serializer

Package Overview
Dependencies
Maintainers
3
Versions
197
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@middy/http-response-serializer - npm Package Compare versions

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"
}
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