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

@keboola/serverless-request-handler

v2.1.4

Published

Wrapper for lambda functions creating unified response for error states

Downloads

18

Readme

serverless-request-handler

serverless Build Status

Wrapper for AWS Lambda functions with API Gateway creating unified response for error states.

By default the handler adds 'Access-Control-Allow-Origin': '*' and 'Content-Type': 'application/json; charset=utf-8' headers to the response.

Installation

  1. Install npm package: yarn add @keboola/serverless-request-handler
  2. Wrap each of your lambda handlers with this code:
const { RequestHandler } = require('@keboola/serverless-request-handler');

module.exports.handler = (event, context, callback) => RequestHandler.handler(() => {
  const promise = new Promise(res => res());
  const code = 204;
  return RequestHandler.responsePromise(promise, event, context, callback, code);
}, event, context, callback);
  • RequestHandler.handler() catches uncaught exceptions
  • RequestHandler.responsePromise() catches rejected promises and formats output for resolved promise chain

You can also return status code and headers in the resolved promise:

module.exports.handler = (event, context, callback) => RequestHandler.handler(() => {
  const promise = new Promise((res) => {
    // ...
    res({
      headers: { 'Access-Control-Allow-Headers': 'Token' },
      body: {
        message: 'Resource was created',
      },
      statusCode: 201,
    });
  });
  return RequestHandler.responsePromise(promise, event, context, callback);
}, event, context, callback);

Request logging

Each request is logged to Cloudwatch in this json format:

{
  "event": {
    "requestId": "4d4f1e1e-ca29-11e7-87c4-2fcc57c44b2d",
    "apiRequestId": "369391fb-ca29-11e7-90b1-95afd12f3597",
    "function": "developer-portal-prod-users",
    "httpMethod": "GET",
    "path": "/users/[email protected]"
  },
  "statusCode": 200
}

User Errors

If you want to return a user error, use UserError class which is handled by the handler automatically and the output is formatted in appropriate way. The class offer several static functions as shortcuts for most common user errors:

  • badRequest(msg = 'Bad Request') - for 400 errors
  • unauthorized(msg = 'Unauthorized') - for 401 errors
  • notFound(msg = 'Not Found') - for 404 errors
  • unauthorized(msg = 'Unauthorized') - for 401 errors
  • unprocessable(msg = 'Unprocessable') - for 422 errors
  • error(msg = 'Error', code = 400) - for other errors

Example:

const { UserError } = require('@keboola/serverless-request-handler');

if (!token) {
  throw UserError.unauthorized('Token is missing');
}

User then gets such response with status code 401:

{
  "errorMessage": "Token is missing",
  "errorType": "Unauthorized",
  "requestId": "4d4f1e1e-ca29-11e7-87c4-2fcc57c44b2d"
}

And CloudWatch log of the request is enriched with error field containing the error message.