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 🙏

© 2026 – Pkg Stats / Ryan Hefner

fp-ts-lambda-routing

v1.1.0

Published

Library that helps with routing lambda events to the right function, wrapped in an fp-ts monad

Readme

fp-ts-lambda-routing

This library offers some simple routing capabilities for AWS Lambda using the fp-ts library.

Its primary use case is when you have an API Gateway that forwards all calls to a single ('fat') Lambda. There are bigger, more complex libraries to serve this purpose (lambda-api, serverless-express) - but most of them have the (Express) habit of passing on additional parameters (request, response, next), something I prefer to avoid.

How it works

You pass in a list of functions that return a TaskEither. The router will return that TaskEither if a route is found.

Else, it will return an TaskEither left with a (customizable) error.

Examples

Also see the project unit tests.

Simple example:

import * as TE from 'fp-ts/TaskEither';
import * as T from 'fp-ts/Task';
import {pipe} from "fp-ts/function";
import type {APIGatewayEvent} from "aws-lambda";
import { router } from 'fp-ts-lambda-routing';

const endpoints = {
    '/foo': {
        'POST': () => TE.right('OK')
    }
};
const ourRouter = router(endpoints);

const example = () => {
    return pipe(
        ourRouter({path: '/foo', httpMethod: 'POST'} as APIGatewayEvent),
        TE.getOrElse((r) => T.of(`Got back some error: ${r}`))
    )();
};

// prints 'OK'
example().then(console.log);

The code will also work with the more recent event payloads (APIGatewayProxyEventV2).

import * as TE from 'fp-ts/TaskEither';
import * as T from 'fp-ts/Task';
import {pipe} from "fp-ts/function";
import type {APIGatewayEvent} from "aws-lambda";
import { router } from 'fp-ts-lambda-routing';

const endpoints = {
    '/foo': {
        'POST': () => TE.right('OK')
    }
};

const exampleEvent = {
    requestContext: {
        http: {
            path: '/foo',
            method: 'POST',
        }
    }
} as APIGatewayProxyEventV2;

const ourRouter = router(endpoints);

const example = () => {
    return pipe(
        ourRouter({path: '/foo', httpMethod: 'POST'} as APIGatewayEvent),
        TE.getOrElse((r) => T.of(`Got back some error: ${r}`))
    )();
};

// prints 'OK'
example().then(console.log);

A left is returned if there is an error.

import * as TE from 'fp-ts/TaskEither';
import { router } from 'fp-ts-lambda-routing';

const handleFirstRoute = () => TE.right('OK');

const endpoints = {
    '/one': {
        'POST': handleFirstRoute,
    },
};

const event = {
    path: '/fake',
    // other properties
};

const routerForDict = router(endpoints);

// will return TaskEither left containing an error message
const result = routerForDict(event);

Endpoints can end with a wildcard.

import * as TE from 'fp-ts/TaskEither';
import { router } from 'fp-ts-lambda-routing';

const handleFirstRoute = () => TE.right('OK');

const endpoints = {
    '/one/*': {
        'POST': handleFirstRoute,
    },
};

const event = {
    path: '/one/two',
    httpMethod: 'POST',
    // other properties
};

const routerForDict = router(endpoints);

// will return TaskEither containing 'OK'
const result = routerForDict(event);

Endpoints without wildcard are preferred above those with a wildcard.

import * as TE from 'fp-ts/TaskEither';
import { router } from 'fp-ts-lambda-routing';

const handleFirstRoute = () => TE.right('wildcard');
const handleSecondRoute = () => TE.left('no wildcard');

const endpoints = {
    '/one/*': {
        'POST': handleFirstRoute,
    },
    '/one/two': {
        'POST': handleSecondRoute,
    },
};

const event = {
    path: '/one/two',
    httpMethod: '/one/two'
    // other properties
};

const routerForDict = router(endpoints);

// will return TaskEither containing 'no wildcard'
const result = routerForDict(event);

Custom Error Constructor

There is a default error constructor, but it can be overwritten by passing a second argument to the router.

This second argument should be a function that takes a string:

// ...

const errorConstructor = (path: string) => {
    return {
        status: 400,
        message: path,
    }
}

// any errors will now return an object with status 400 and a message
const routerForDict = router(dict, errorConstructor);

Requirements

fp-ts is used both within the library and as part of the types.

Possible improvements

  • should ideally also support other monads, definitely Either
  • allow 'ANY' as a catch-all http method