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

@ori-sh/lambda-api-helpers

v1.0.3

Published

Boilerplate for exposing AWS Lambda API

Downloads

2

Readme

lambda-api-helpers

Set of utilities to implement APIs using AWS Lambda. The package provides the following features:

  1. Simplify the signature of the Lambda handler
  2. Error Handling & set of common HTTP errors to be used as Node JS Errors

1. Simplify the signature of the Lambda handler

Usage

import asLambdaHandler from '@ori-sh/lambda-api';

export const handler = asLambdaHandler(async request => ({
  greeting: `Hello ${request.body?.name ?? 'World'}!`
}));

Request

type HttpRequest = {
  method: 'GET' | 'HEAD' | 'PUT' | 'POST' | 'DELETE' | 'PATCH'
  body: JsonObject | undefined
  queryParams: Record<string, string | undefined>
  pathParams: Record<string, string | undefined>
  headers: Record<string, string | undefined>
}

Response

type HttpResponse = JsonObject | undefined

2. Error Handling & set of common HTTP errors to be used as Node JS Errors

When using the asLambdaHandler helper it's automatically wires error handling for you. This will automatically catch any unexpected error thrown in the handler and normalize it as 500 HTTP response.

Additionally, it will catch any error extending HttpError and normalize it as HTTP response. The following errors are available out of the box:

BadRequest - 400

The server could not understand the request. Can happen when request failed validation or received in a malformed format.

Unauthorized - 401

Request has not been applied because it lacks valid authentication credentials. Can happen when the Authorization token is expired, invalid or missing.

Forbidden - 403

The server understood the request but authenticated user isn't authorized to perform it. Happens when the user don't have sufficient permissions to perform the request.

NotFound - 404

The requested resource has not been found. Can happen when querying resource with wrong id or resource that has been deleted.

Conflict - 409

The server can not process your request due a conflict with a existing result. Might happen when trying to create a resource which already exist.

TooManyRequests - 429

The user has sent too many requests in a given amount of time. Might happen when there is a throttling policy applied.

Note that the response error will expose stack traces unless explicitly setting NODE_ENV to 'production'. Make sure to add the NODE_ENV environment variable in production to prevent leakage of the server internals with stack traces.

Usage Example:

import asLambdaHandler, { NotFound } from '@ori-sh/lambda-api';

export const handler = asLambdaHandler(async request => {
  const item = queryItemFromDatabase(request.pathParams.id);
  // If no item found in the database a 404 status HTTP response will be returned. 
  if (item === null || item === undefined)
    throw new NotFound(`No item with the id "${request.pathParams.id}"`)
  return item;
});