1.2.0 • Published 5 years ago

aws-lambda-reply v1.2.0

Weekly downloads
3
License
Apache-2.0
Repository
github
Last release
5 years ago

aws-lamba-reply

Build Status dependencies Status codecov

💬 A tiny utility for creating AWS Lambda response objects.

This package provides an easy way for you set default headers for your lambda functions. Helps reduce clutter and make your output more consistent in large scale applications.

Install

npm install aws-lambda-reply

Why

When developing large applications you often work across multiple repositories. I found myself following the same pattern of creating a factory in each repository every time I wanted set standards for a lambda's output. When you also have to add tests for this boilerplate it can quickly get annoying - this package intends to solve this problem by creating a first class, best version of this generic method, that is easily accessible as a module.

Usage

Set up aws-lambda-reply with your defaults:

import { LambdaReply } from 'aws-lambda-reply';

const reply = new LambdaReply({
  headers: {
    'x-powered-by': 'nodejs',
  }
});

And then use inside your function:

function handler(event, context) {
  return reply.make(200, '{"message": "Hello world!"}');
}

Your headers will automatically be added to the response object, along with any additional headers you pass.

API

// ES6
import { LambdaReply } from 'aws-lambda-reply';

// commonJS
const LambdaReply = require('aws-lambda-reply').LambdaReply;

const reply = new LambdaReply(defaults);

This API assumes that you name the instance reply, but you can call it anything you like.

Constructor

defaults

Type: object

Defaults for all replies made using this object.

headers default to { 'Content-Type': 'application/json' }.

multiValueHeaders default to {}.

{
  headers?: { [key: string]: string | boolean | number },
  multiValueHeaders?: { [key: string]: string[] }
}

reply.make(statusCode, body, options);

Params

statusCode

Type: number

HTTP status code

body

Type: string

The response body to return.

options

Type: object

Add additional headers or set the response as base64 encoded.

{
  headers?: { [key: string]: string | boolean | number },
  multiValueHeaders?: { [key: string]: Array<string | boolean | number> },
  isBase64Encoded?: boolean,
}

Returns

Type: object

{
  statusCode: number,
  body: string,
  headers?: { [key: string]: string | boolean | number },
  multiValueHeaders?: { [key: string]: Array<string | boolean | number> },
  isBase64Encoded?: boolean
}
1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago