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

@beesolve/lambda-fetch-api

v1.0.0

Published

[![npm version](https://img.shields.io/npm/v/@beesolve/lambda-fetch-api)](https://www.npmjs.com/package/@beesolve/lambda-fetch-api) [![license](https://img.shields.io/npm/l/@beesolve/lambda-fetch-api)](./LICENSE)

Downloads

648

Readme

@beesolve/lambda-fetch-api

npm version license

Run standard Fetch API handlers inside AWS Lambda. Converts API Gateway v1 (REST) and v2 (HTTP) events into Request objects and converts Response objects back into Lambda proxy integration results.

Installation

npm install @beesolve/lambda-fetch-api

Usage

Basic handlers

Wrap any (request: Request) => Promise<Response> function:

import { asHttpV2Handler } from "@beesolve/lambda-fetch-api";

export const handler = asHttpV2Handler(async (request) => {
  const body = await request.json();
  return Response.json({ ok: true, received: body }, { status: 201 });
});
import { asHttpV1Handler } from "@beesolve/lambda-fetch-api";

export const handler = asHttpV1Handler(async (request) => {
  const url = new URL(request.url);
  return Response.json({ path: url.pathname });
});

Response streaming

import { asResponseStreamHandler } from "@beesolve/lambda-fetch-api";

export const handler = asResponseStreamHandler(async () => {
  const stream = new ReadableStream({
    async start(controller) {
      controller.enqueue("chunk one\n");
      controller.enqueue("chunk two\n");
      controller.close();
    },
  });
  return new Response(stream, { headers: { "content-type": "text/plain" } });
});

Accessing the AWS event and context

The original event and context are stored per-invocation via AsyncLocalStorage. Call the getters from anywhere inside your handler — no need to thread parameters:

import {
  asHttpV2Handler,
  getAwsEvent,
  getAwsV2Event,
  getAwsContext,
} from "@beesolve/lambda-fetch-api";

export const handler = asHttpV2Handler(async (request) => {
  const event = getAwsV2Event();         // APIGatewayProxyEventV2
  const context = getAwsContext();        // Context

  console.log(event.requestContext.requestId);
  console.log(context.getRemainingTimeInMillis());

  return new Response("ok");
});

| Getter | Returns | |---|---| | getAwsEvent() | APIGatewayProxyEvent \| APIGatewayProxyEventV2 — auto-detected | | getAwsV1Event() | APIGatewayProxyEvent — throws if event is v2 | | getAwsV2Event() | APIGatewayProxyEventV2 — throws if event is v1 | | getAwsContext() | Context |

All getters throw NotInHandlerContextError if called outside of a handler invocation.

Authorizer handlers

For routes protected by a Lambda authorizer, use the authorizer-aware handler variants. The incoming event type is correctly narrowed to include the authorizer payload:

HTTP API v2 — Lambda authorizer:

import { asLambdaAuthorizedHttpV2Handler } from "@beesolve/lambda-fetch-api";

export const handler = asLambdaAuthorizedHttpV2Handler(async (request) => {
  return Response.json({ ok: true });
});
// event: APIGatewayProxyEventV2WithLambdaAuthorizer<unknown>

REST API v1 — custom authorizer:

import { asCustomAuthorizedHttpV1Handler } from "@beesolve/lambda-fetch-api";

export const handler = asCustomAuthorizedHttpV1Handler(async (request) => {
  return Response.json({ ok: true });
});
// event: APIGatewayProxyWithLambdaAuthorizerEvent<unknown>

Reading the authorizer payload

Use getAwsLambdaAuthorizerContext (v2) or getAwsCustomAuthorizerContext (v1) to read the authorizer payload. Pass a Standard Schema-compatible schema to validate and type the result — works with valibot, Zod, ArkType, and any other Standard Schema library.

import * as v from "valibot";
import {
  asLambdaAuthorizedHttpV2Handler,
  getAwsLambdaAuthorizerContext,
} from "@beesolve/lambda-fetch-api";

const AuthSchema = v.object({
  userId: v.string(),
  role: v.picklist(["admin", "editor", "viewer"]),
});

export const handler = asLambdaAuthorizedHttpV2Handler(async (request) => {
  const auth = await getAwsLambdaAuthorizerContext(AuthSchema);
  // auth: { userId: string; role: "admin" | "editor" | "viewer" }

  if (auth.role !== "admin") {
    return new Response("Forbidden", { status: 403 });
  }

  return Response.json({ user: auth.userId });
});

Without a schema, the getter returns unknown:

const raw = getAwsLambdaAuthorizerContext();  // unknown

AuthorizerContextValidationError is thrown when the schema rejects the payload.

Type guards

Use these to narrow a union event type at runtime:

import { isAPIGatewayProxyEvent, isAPIGatewayProxyEventV2 } from "@beesolve/lambda-fetch-api";

const event = getAwsEvent();

if (isAPIGatewayProxyEventV2(event)) {
  console.log(event.rawPath);   // APIGatewayProxyEventV2
} else {
  console.log(event.path);      // APIGatewayProxyEvent
}

Testing

Use runWithAwsContext to run code inside a fake invocation context — the same mechanism the handlers use internally:

import { runWithAwsContext, getAwsV2Event, getAwsContext } from "@beesolve/lambda-fetch-api";

test("reads event inside context", async () => {
  const event = makeV2Event();
  const context = makeContext();

  await runWithAwsContext(event, context, async () => {
    expect(getAwsV2Event()).toBe(event);
    expect(getAwsContext().awsRequestId).toBe("req-123");
  });
});

Testing authorizer context works the same way — shape the event with the authorizer payload:

const event = {
  ...makeV2Event(),
  requestContext: {
    ...makeV2Event().requestContext,
    authorizer: { lambda: { userId: "u1", role: "admin" } },
  },
};

await runWithAwsContext(event, makeContext(), async () => {
  const auth = await getAwsLambdaAuthorizerContext(AuthSchema);
  expect(auth.userId).toBe("u1");
});

Error classes

| Class | Thrown when | |---|---| | NotInHandlerContextError | A getter is called outside of a handler invocation | | AuthorizerContextValidationError | The Standard Schema rejects the authorizer payload |

Response utilities

awsRequest, awsResponseHeaders, and awsResponseBody are exported for advanced use cases where you need to drive the request/response conversion manually.

License

MIT