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

@beta3000/serverless-library

v1.0.3

Published

Helpers de respuesta y utilidades para AWS Lambda (SAM)

Readme

serverless-library

TypeScript utility library for AWS Lambda + API Gateway (SAM) projects. Provides a unified response envelope, error handling, request parsing and Cognito identity helpers.

Installation

npm install @beta3000/serverless-library

Unified Response Envelope

Every response produced by buildResponse and buildErrorResponse follows the same JSON envelope:

{
  "success": true,
  "code": "OK",
  "httpCode": 200,
  "messages": [],
  "data": { "id": 1, "name": "Ada" }
}

Error responses use the same shape with success: false and data: null:

{
  "success": false,
  "code": "REQUEST_BODY",
  "httpCode": 400,
  "messages": ["Invalid JSON body"],
  "data": null
}

Configuration

CORS origin

Set the ORIGIN environment variable for CORS headers. When ORIGIN is an explicit origin, Access-Control-Allow-Credentials: true is included. When ORIGIN is not set, the fallback * is used without credentials (the combination is invalid per the CORS spec). In production, always set an explicit origin.

export ORIGIN=https://app.example.com

Usage

End-to-end Lambda handler

import {
  buildErrorResponse,
  buildResponse,
  getRequest,
  getPathParamsByKey,
  getUsername,
  LambdaEvent,
} from '@beta3000/serverless-library';

interface CreateItemBody {
  name: string;
  price: number;
}

export const handler = async (event: LambdaEvent) => {
  try {
    const body = getRequest<CreateItemBody>(event);
    const user = getUsername(event);

    // Business logic
    const item = { id: '123', ...body, createdBy: user };

    return buildResponse({
      data: item,
      code: 'CREATED',
      httpCode: 201,
      messages: ['Item created successfully'],
    });
  } catch (error) {
    return buildErrorResponse(error);
  }
};

Request body parsing — getRequest

import { getRequest } from '@beta3000/serverless-library';

const body = getRequest(event);
  • Returns {} when event.body is null.
  • Returns the parsed object when body is valid JSON.
  • Throws BusinessError with code REQUEST_BODY (HTTP 400) on invalid JSON.
  • Returns the event itself when body property is absent (fallback).
  • Base64-encoded bodies are not supported in v1.

Query and path parameters

import { getQueryParamsByKey, getPathParamsByKey } from '@beta3000/serverless-library';

const page = getQueryParamsByKey(event, 'page'); // string | undefined
const id = getPathParamsByKey(event, 'id'); // string | undefined

Both return undefined when the key is missing, the parameters object is null, or the value is an empty string.

Cognito identity — getUsername / getClaims

import { getUsername, getClaims } from '@beta3000/serverless-library';

const username = getUsername(event); // cognito:username or 'SYSTEM'
const claims = getClaims(event); // Record<string, string> | undefined

Safe to call on non-Cognito events — never throws. Falls back to 'SYSTEM' when claims are unavailable.

Building responses

import { buildResponse } from '@beta3000/serverless-library';

return buildResponse({
  data: { id: 1, name: 'Ada' },
  code: 'OK',
  httpCode: 200,
});

Error handling

import { buildErrorResponse, BusinessError, HTTP_CONSTANT } from '@beta3000/serverless-library';

// BusinessError → uses its code/httpCode/messages
// Generic Error → 500 INTERNAL_ERROR (internal message never exposed)
// Non-Error value → 500 INTERNAL_ERROR
return buildErrorResponse(error);

Security headers

import { buildSecurityHeaders } from '@beta3000/serverless-library';

const headers = buildSecurityHeaders();
// X-Content-Type-Options: nosniff
// X-XSS-Protection: 1; mode=block
// X-Frame-Options: SAMEORIGIN
// Referrer-Policy: strict-origin-when-cross-origin
// Strict-Transport-Security: max-age=31536000; includeSubDomains
// Access-Control-Allow-Origin: <ORIGIN>

Extending the error catalog

import { Errors, HTTP_CONSTANT, BusinessError } from '@beta3000/serverless-library';

const MyErrors = {
  ...Errors,
  DUPLICATE_EMAIL: { code: 'DUPLICATE_EMAIL', message: 'Email already in use' },
};

throw new BusinessError({
  code: MyErrors.DUPLICATE_EMAIL.code,
  httpCode: HTTP_CONSTANT.BAD_REQUEST.httpCode,
  messages: [MyErrors.DUPLICATE_EMAIL.message],
});

API Reference

Generate full API docs locally:

npm run docs

Output is written to docs-api/.

License

MIT