npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@da-fat-company/lambda-wrapper

v1.0.1

Published

NodeJS App Wrapper to help developper fastly build AWS Lambda based application

Downloads

5

Readme

Lambda Wrapper

NPM Badge build status coverage report JsDoc report Plato report

NodeJS App Wrapper to help developper fastly build AWS Lambda based application.

This package provide a helper class base on promise chaining and manage the lambda response.

The wrapper send data in JSON in response to a lambda call. (Default is { "success": true } ).

Install

    $ npm i --save @da-fat-company/lambda-wrapper

Usage

Basics

'use strict';

const LambdaWrapper = require('@da-fat-company/lambda-wrapper');

module.exports.run = (event, context, callback) => {

  const app = new LambdaWrapper(callback);

  const bar = [];

  // Each run is a chained promise
  app
  .run(() => { console.log('Do something here'); })
  .run(() => { 
    console.log('Then return a value here');
    return 'foo'; 
  })  
  .run((data) => { 
    console.log('Then retreive the previously returned value as first arg', data); // foo
    return Promise.resolve(data);
  })
  .run((data, resolve, rejeect) => { 
    console.log('Then eventually do something more complexe or async, that require a resolver/rejecter');
    setTimeout(() => resolve(data), 1000);
  })
  .runAll([
    () => console.log('Do something async'),
    () => bar.push(1),
    () => bar.push(2),
    () => bar.push(3)
  ])
  .run(() => console.log(bar)) // [1, 2, 3]
  .end(); // End function handle error and finally  
};

By default end function catch errors and convert them into AdvancedError, then set the lambda body to:

If AdvancedError privacy is private (default case):

{
  "success": false,
  "error": "Internal Server Error"
}

If AdvancedError privacy is public:

{
  "success": false,
  "error": "Error Title",
  "error_message": "Error Message"
}

Provide custom catch / finally function

The end function can take two arguments, the first one is a custom catch function, the second one a finally function.

'use strict';

const LambdaWrapper = require('@da-fat-company/lambda-wrapper');

module.exports.run = (event, context, callback) => {

  const app = new LambdaWrapper(callback);

  // Function called on catch
  const myErrorHandler = (error) => {
    console.log('Do something with the error: ', error);
    
    app.body.success = false;
  };
  
  // Function called at the end (even if the catch function has been invoked)
  const myFinalFunction = (data) => {
    console.log('This is the end!', data);
  };

  app
  .run(() => { console.log('Do something here'); })  
  .end(myErrorHandler, myFinalFunction);
};

Change default success body

By default the wrapper send as JSON output the folowing object:

{
    "success": true    
}

You can change it using setDefaultSuccessBody():

'use strict';

const LambdaWrapper = require('@da-fat-company/lambda-wrapper');

module.exports.run = (event, context, callback) => {

  const app = new LambdaWrapper(callback);

  app
  .setDefaultSuccessBody({
    foo: 'bar'
  })
  .run(() => { console.log('Do something here'); })  
  .end(); // Response will be { "foo": "bar" }
};

But if the last run() call resolve or return an object, this data is passed directly to the body. Note that a JSON.stringify() is applyied to the body before sending it to lambda callback.

'use strict';

const LambdaWrapper = require('@da-fat-company/lambda-wrapper');

module.exports.run = (event, context, callback) => {

  const app = new LambdaWrapper(callback);

  app
  .run(() => { 
    return {
      foo: "bar"
    }
  })
  .end(); // Response will be { "foo": "bar" }
};

Change headers

By default, the wrapper set the following header to lambda response:

  • Content-Type: application/json
  • Access-Control-Allow-Origin: *

But you can change them using setHeaders():

'use strict';

const LambdaWrapper = require('@da-fat-company/lambda-wrapper');

module.exports.run = (event, context, callback) => {

  const app = new LambdaWrapper(callback);

  app.setHeaders({
    'Content-Type': 'application/json',
    'Access-Control-Allow-Origin': 'example.io'
  })
  .run(() => { console.log('Do something here'); })  
  .end();
};