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
10
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 1.0.0-alpha.31 to 1.0.0-alpha.32

75

__tests__/index.js

@@ -37,3 +37,3 @@ const createError = require('http-errors')

'%s skips response serialization',
(key) => {
(key, done) => {
const handlerResponse = Object.assign({}, createHttpResponse(), {

@@ -50,2 +50,3 @@ headers: {

expect(response).toEqual(handlerResponse)
done()
})

@@ -62,3 +63,3 @@ })

'%s returns %s',
(accept, result) => {
(accept, result, done) => {
const handler = middy((event, context, cb) => cb(null, createHttpResponse()))

@@ -76,2 +77,3 @@

expect(response.body).toEqual(result)
done()
})

@@ -81,3 +83,3 @@ })

test('It should use `event.requiredContentType` instead of accept headers', () => {
test('It should use `event.requiredContentType` instead of accept headers', done => {
const handler = middy((event, context, cb) => {

@@ -105,6 +107,7 @@ event.requiredContentType = 'text/plain'

})
done()
})
})
test('It should use the default when no accept preferences are given', () => {
test('It should use the default when no accept preferences are given', done => {
const handler = middy((event, context, cb) =>

@@ -124,6 +127,7 @@ cb(null, createHttpResponse())

})
done()
})
})
test('It should use `event.preferredContentType` instead of the default', () => {
test('It should use `event.preferredContentType` instead of the default', done => {
const handler = middy((event, context, cb) => {

@@ -145,6 +149,7 @@ event.preferredContentType = 'text/plain'

})
done()
})
})
test('It should pass-through when no preference or default is found', () => {
test('It should pass-through when no preference or default is found', done => {
const handler = middy((event, context, cb) =>

@@ -163,6 +168,7 @@ cb(null, createHttpResponse())

})
done()
})
})
test('It should replace the response object when the serializer returns an object', () => {
test('It should not pass-through when request content-type is set', done => {
const handler = middy((event, context, cb) =>

@@ -172,2 +178,28 @@ cb(null, createHttpResponse())

handler.use(httpResponseSerializer(standardConfiguration))
const event = {
headers: {
'Content-Type': 'application/xml'
}
}
handler(event, {}, (err, response) => {
if (err) throw err
expect(response).toEqual({
statusCode: 200,
headers: {
'Content-Type': standardConfiguration.default
},
body: '{"message":"Hello World"}'
})
done()
})
})
test('It should replace the response object when the serializer returns an object', done => {
const handler = middy((event, context, cb) =>
cb(null, createHttpResponse())
)
handler.use(httpResponseSerializer({

@@ -195,6 +227,7 @@ serializers: [

})
done()
})
})
test('It should work with `http-error-handler` middleware', () => {
test('It should work with `http-error-handler` middleware', done => {
const handler = middy((event, context, cb) => {

@@ -216,4 +249,30 @@ throw new createError.UnprocessableEntity()

})
done()
})
})
test('It should not crash if the response is undefined', done => {
const handler = middy((event, context, cb) =>
cb(null, undefined)
)
handler.use(httpResponseSerializer(standardConfiguration))
const event = {
headers: {
'Content-Type': 'application/xml'
}
}
handler(event, {}, (err, response) => {
if (err) throw err
expect(response).toEqual({
headers: {
'Content-Type': standardConfiguration.default
},
body: '{}'
})
done()
})
})
})

10

index.js

@@ -13,6 +13,7 @@ const Accept = require('accept')

// normalise headers for internal use only
const headers = getNormalisedHeaders(handler.event.headers || {})
const requestHeaders = getNormalisedHeaders((handler.event && handler.event.headers) || {})
const responseHeaders = getNormalisedHeaders((handler.response && handler.response.headers) || {})
// skip serialization when content-type is already set
if (headers['content-type']) {
if (responseHeaders['content-type']) {
return next()

@@ -28,3 +29,3 @@ }

types = [].concat(
(headers['accept'] && Accept.mediaTypes(headers['accept'])) || [],
(requestHeaders['accept'] && Accept.mediaTypes(requestHeaders['accept'])) || [],
handler.event.preferredContentType || [],

@@ -46,2 +47,5 @@ opts.default || []

// if the response is null or undefined, normalizes it back to an object
handler.response = handler.response || {}
// set header

@@ -48,0 +52,0 @@ handler.response.headers = Object.assign({}, handler.response.headers, { 'Content-Type': type })

{
"name": "@middy/http-response-serializer",
"version": "1.0.0-alpha.31",
"version": "1.0.0-alpha.32",
"description": "Http response serializer middleware for the middy framework",

@@ -50,3 +50,3 @@ "engines": {

},
"gitHead": "7386e91d136cdf3e2725de1010e919e34ec87605"
"gitHead": "da0b42acc3e5fae23e634dbe14c43d7976da8344"
}
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