This package has been deprecated

Author message:

WARNING: This project has been renamed to lambda-response-template. Install using lambda-response-template instead.

aws-lambda-reply
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

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
}

Package Sidebar

Install

npm i aws-lambda-reply

Weekly Downloads

9

Version

1.2.0

License

Apache-2.0

Unpacked Size

24.8 kB

Total Files

15

Last publish

Collaborators

  • c-bandy
  • furudean