warhead-lambda

0.0.0-alpha.1 • Public • Published

warhead-lambda

npm version Build Status Coverage Status Greenkeeper badge

Warhead uses adapters to provide an abstraction layer with a unified interface atop any given platform.

adapter(handler)

A Warhead adapter for AWS Lambda functions.

const { adapter } = require('warhead-lambda')
exports.handler = adapter(function(request, context){
    // Do things
    // Return value or Promise
})

Arguments:

  1. Handler function with up to 2 arguments.
    1. An instance of Request which contains all the request information .
    2. The context object provided by AWS Lambda which can also be access via the this variable.

For synchronous operations, you may return a value or Response.

return {foo: 'bar'}

For asynchronous operations, you may return a Promise that resolves a value or a Response.

return Promise.resolve({foo: 'bar'})

If the value returned by the handler is not an instance of Response, it will be wrapped in a Response with a default response code of 200 and a content-type header of application/json automatically.

If an error is thrown or a Promise is rejected with a value other than a Response, it will be wrapped in a Response with a default response code of 500 and a content-type header of application/json automatically.

Response([body])

A constructor for building a proper response for AWS Lambda's proxy integration.

Arguments:

  1. The desired response body.
  2. A transformation function used to stringify the body, defaults to JSON.stringify.

If an object is provided, it will automatically be stringified. New instances default to a response code of 200 and a content-type header of application/json.

const { adapter, Response } = require('warhead-lambda')
 
exports.handler = adapter(function(request){
    return new Response({message: 'Hello World!'})
})

response.status([code])

Set or get status code of a response.

const { Response } = require('warhead-lambda')
const response = new Response({message: 'Hello World!'})
response.status(200)

response.header(name, [value])

Set or get a header of a response.

const { Response } = require('warhead-lambda')
const response = new Response({message: 'Hello World!'})
response.header('foo', 'bar')

test-helpers

Helpers for testing functions written with the warhead-lambda adapter.

harness()

A harness for testing a lambda function.

import test from 'ava'
import { adapter, Response } 'warhead-lambda'
import { harness } 'warhead-lambda/test-helpers'
 
const request = {
    body: {foo: 'bar'}
}
const handler = adapter(request => {
    return new Response({message: request.body.foo})
})
 
test(t => {
    return harness(handler, request)
        .then(result => {
            t.true(result instanceof Response)
            t.is('bar', result.body.message)
        })
})

Package Sidebar

Install

npm i warhead-lambda

Weekly Downloads

1

Version

0.0.0-alpha.1

License

MIT

Unpacked Size

256 kB

Total Files

17

Last publish

Collaborators

  • blackmarket
  • mattchewone