Skip to content

A small lambda wrapper that lets you write cleaner and maintainable lambda function

License

Notifications You must be signed in to change notification settings

serverless-guy/lambda

Repository files navigation

@serverless-guy/lambda

A small lambda wrapper that lets you write cleaner and maintainable lambda function

Version 3.x focuses on removing stuffs that does not make sense and providing a little bit "lambda"-ish familiarity.

Status

Build Status Bundle Size install size NPM Version Downloads Stats Snyk CodeFactor Debt Issues Libraries.io SourceRank License Donate

Installation

You can start by installing this library using the command below:

npm i --save @serverless-guy/lambda

Basic Usage

In the example below, the handler would log the event first, then context. Afterwards, it will return the event as response.

import { wrapper } from "@serverless-guy/lambda";

export const handler = wrapper(function someHandler(event, context, response) {

  console.log(event);
  console.log(context);

  return response(event);
});

Using custom response function

import { wrapper } from "@serverless-guy/lambda";

export const handler = wrapper(function someHandler(event, context, response) {
  console.log(event);
  console.log(context);

  return response(event);
});

handler.setResponseFunction(customResponseTemplate);

function customResponseTemplate(data, statusCode = 200, headers = {}) {
    // do something
    data.returnedOn = new Date();

    return {
    body: JSON.stringify(data),
    headers: {
      "Access-Control-Allow-Origin": "*",
      ...headers
    },
    statusCode
  };
}

Using custom error response function

import { wrapper } from "@serverless-guy/lambda";
 
export const handler = wrapper(function someHandler(event, context, response) {
  console.log(event);
  console.log(context);

  return response(event);
});

handler.setCatchFunction(customCatchResponseTemplate);

function customCatchResponseTemplate(error, event, context, responseFunction) {
  const errorResponseObject = {
    errorCode:    error.name,
    errorMessage: error.message
  };

  return response(errorResponseObject, 418); /** I'm a f***ing teapot */
}

Check out our documentation page to see more examples.

About

A small lambda wrapper that lets you write cleaner and maintainable lambda function

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published