jeremydaly / lambda-api

Lightweight web framework for your serverless applications
https://serverless-api.com
MIT License
1.41k stars 125 forks source link

Support Custom Serializer with Base64 Encoded Response #177

Closed AndrewBarba closed 3 years ago

AndrewBarba commented 3 years ago

This adds the ability to return a gzipped response to lambda:

const api = require('lambda-api')({
  isBase64: true,
  headers: {
    'content-encoding': ['gzip']
  },
  serializer: body => {
    const json = JSON.stringify(body)
    return zlib.gzipSync(json).toString('base64')
  }
})

Inspiration: https://medium.com/@vanbergstephane/gzip-aws-lambda-http-api-gateway-6b9c6431c564

jeremydaly commented 3 years ago

Thanks @AndrewBarba. I'll take a look and get this merged.

AndrewBarba commented 3 years ago

@jeremydaly Thanks a lot. It should be good to go at this point. I have this deployed and its working as expected, all tests (including a new one) pass, and I added compression docs to the readme.

jeremydaly commented 3 years ago

I was just going to ask you to add documentation! 🙌 I’ve also added another test to the error handling to ensure base64 was flagged correctly. I’ll check that in soon, merge this all, and push to npm.

jeremydaly commented 3 years ago

v0.10.7 is now in npm.