Lambda API: v0.4 Released

Posted in #lambda-api

A lot of work has gone into v0.4 to add support for standard use cases while also keeping it as lightweight and unopinionated as possible. This new release adds Static File Support including serving binary files like images and PDFs. Route Prefixing was also added, giving you more control over code structure and easier code maintenance.

There is still more work to be done, but the goal is to make Lambda API a full-featured web framework for serverless applications. ⚡

NPM: https://www.npmjs.com/package/lambda-api

GitHub: https://github.com/jeremydaly/lambda-api

Binary & Static File Support

Lambda API now has Binary Support! This allows you to both send and receive binary & static files from API Gateway.

In addition, four new methods have been added to make delivering binary & static files a snap:

  • sendFile(file [, options] [, callback]): This method lets you serve static files by automatically base64 encoding them and sending them to API Gateway. The file input supports local paths (i.e. files deployed with your lambda function code), JavaScript Buffer objects (allowing you to load your own files and pass them through), and S3 paths in the form of s3://my-bucket/path/to/file.png, which automatically loads and transfers files from your S3 buckets (appropriate S3 permission required). See sendFile documentation for more information.
  • attachment([filename]): The attachment() method adds Content-Disposition headers allowing you to prompt the download of the file instead of the display. See attachment documentation.
  • download(file [, filename] [, options] [, callback]): This shortcut method combines the attachment and sendFile calls into one.  See download documentation.
  • type(type): The new type method makes setting Content-Type headers easy. Simply specify a common file extension and Lambda API will add the proper header. See attachment documentation.

IMPORTANT: To enable binary support in API Gateway, you need to add */* under "Binary Media Types" in API Gateway -> APIs -> [ your api ] -> Settings. This will also base64 encode all body content, but Lambda API will automatically decode it for you.

Route Prefixing

Lambda API now makes it super easy to create multiple versions of the same api without changing routes by hand. The new register() method allows you to load routes from an external file and prefix all of those routes using the prefix option. For example:

javascript
// handler.js const api = require('lambda-api')() api.register(require('./routes/v1/products'), { prefix: '/v1' }) api.register(require('./routes/v2/products'), { prefix: '/v2' }) module.exports.handler = (event, context, callback) => { api.run(event, context, callback) }
javascript
// routes/v1/products.js module.exports = (api, opts) => { api.get('/product', handler_v1) }
javascript
// routes/v2/products.js module.exports = (api, opts) => { api.get('/product', handler_v2) }

Even though both modules create a /product route, Lambda API will add the prefix to them, creating two unique routes. Your users can now access:

  • /v1/product
  • /v2/product

You can use register() as many times as you want AND it is recursive, so if you nest register() methods, the routes will build upon each other. For example:

javascript
module.exports = (api, opts) => { api.get('/product', handler_v1) api.register(require('./v2/products.js'), { prefix: '/v2'} ) }

This would create a /v1/product and /v1/v2/product route. You can also use register() to load routes from an external file without the prefix. This will just add routes to your base path. NOTE: Prefixed routes are built off of your base path if one is set. If your base was set to /api, then the first example above would produce the routes: /api/v1/product and /api/v2/product.

Release Notes:

Feature Release

v0.4 adds Binary Support, Path Prefixing and more.

Binary Support

  • Add base64 body parsing if isBase64Encoded header is available 75284c5
  • Add mimeLookup and mimemap 6b0d2d8, 928c7e3
  • Add attachment(), download(), and sendFile() methods 9a73b68, 6c745d6, a0f8a78
  • Add custom mimeTypes config option e8f6686
  • Add auto reset of isBase64 and add strip headers on error 86c0a17, 91eea99
  • Add s3 getObject support w/ tests 28b9c33
  • Add aws-sdk as dev dependency 591da6d

Path Prefixing

  • Add register function to close #11, rework base property bc2a00b
  • Update tests with new base config and add register tests bfa26be
  • Add support for root paths 49476ab

Additional Updates

  • Close #18 by adding patch() convenience method 9012dc6

Tests

  • Add mimeLookup tests 7926f21
  • Add attachment() method tests 96a64d7
  • Update tests to support default isBase64Encoded flag f62eb12
  • Add tests for sendFile() and test.txt file e27c672
  • Add tests for download() method 3643713
  • Add additional attachment() tests f86af2d

Documentation

  • Update lambda api image inclusion method for npm fix 4e0063e
  • Add notes for root paths and binary support 074ee79
  • Additional notes about binary support 0a6de47
  • Initial documentation for sendFile and new config options 229b93f
  • Add patch() documentation 71316e9
  • Additional documentation for download/sendfile and other updates aed99f6
  • Add register() documentation 8530199
  • Add attachment() documentation aa9bcaa
  • Add binary support documentation e42739f
  • Additional documentation and cleanup 0127fb8, 7e5ee40

Full Release Notes: https://github.com/jeremydaly/lambda-api/releases/tag/v0.4.0

NPM: https://www.npmjs.com/package/lambda-api

GitHub: https://github.com/jeremydaly/lambda-api