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

@luckystack/api

v0.2.5

Published

Type-safe API request handlers for LuckyStack. WebSocket-first via socket.io, with HTTP fallback. File-based routing, generated route map, integrated rate limiting, validation, hooks, and error-tracking tracing.

Readme

@luckystack/api

Type-safe API request handlers for LuckyStack. WebSocket-first via socket.io with HTTP fallback. File-based routing, generated route map, integrated rate limiting, validation, hooks, and error-tracking tracing.

Install

npm install @luckystack/api @luckystack/core @luckystack/login @luckystack/error-tracking socket.io

Quickstart

API endpoints live in src/{page}/_api/{name}_v{N}.ts and are picked up by the file-based router. Each file exports main, plus optional metadata (auth, method, rateLimit).

// src/settings/_api/updateUser_v1.ts
import type { AuthProps } from '@luckystack/core';
import type { ApiResponse } from '@luckystack/core';

export const rateLimit: number | false = 20;
export const method = 'POST' as const;
export const auth: AuthProps = { login: true };

export interface ApiParams {
  data: { name: string };
  user: SessionLayout;
  functions: Functions;
}

export const main = async ({ data, user }: ApiParams): Promise<ApiResponse> => {
  await prisma.user.update({ where: { id: user.id }, data: { name: data.name } });
  return { status: 'success', result: { ok: true } };
};

The package exposes the two transport adapters that @luckystack/server wires into Socket.io and HTTP:

import handleApiRequest from '@luckystack/api';
import { handleHttpApiRequest } from '@luckystack/api';

io.on('connection', socket => {
  socket.on('apiRequest', msg => handleApiRequest({ msg, socket, token }));
});

httpServer.on('request', async (req, res) => {
  if (req.url?.startsWith('/api/')) {
    const result = await handleHttpApiRequest({ name: body.name, data: body.data, token });
    res.end(JSON.stringify(result));
  }
});

You typically don't call these yourself — createLuckyStackServer does. Use this package directly only when building a custom transport.

How it integrates

  1. Validates the inbound payload against the Zod schema generated from your ApiParams['data'] interface (via @luckystack/devkit).
  2. Rate-limits by IP + token using checkRateLimit from @luckystack/core.
  3. Authenticates via getSession from @luckystack/login when auth.login === true.
  4. Dispatches the preApiExecute hook (may abort with a stop signal).
  5. Calls your main(...) and captures errors via tryCatch (auto-forwarded to Sentry).
  6. Dispatches the postApiExecute hook with the result + duration.
  7. Returns the response via socket ack or HTTP body / SSE stream.

Generated types

apiRequest (in @luckystack/core/client) is fully typed against the route map emitted by @luckystack/devkit from your _api/* files (default location: src/_sockets/apiTypes.generated.ts). Use route-name + version literals so inference works:

const result = await apiRequest({
  name: 'settings/updateUser',
  version: 'v1',
  data: { name: 'Alice' },
});

if (result.status === 'success') {
  // result is typed from the matching `main` return type.
}

Do not wrap apiRequest in unknown / any shims (see .claude/CLAUDE.md rule 16). If inference fails, fix the typing source or regenerate maps instead.

Public API

| Export | Purpose | | --- | --- | | handleApiRequest({ msg, socket, token }) | Socket.io request handler (default export). | | handleHttpApiRequest({ name, data, token, ... }) | HTTP fallback for /api/* routes; returns Promise<ApiNetworkResponse> and supports SSE streaming via a stream callback. | | Type: ApiHttpStreamEvent | SSE event shape emitted by streaming endpoints. |

Related architecture docs

Dependencies

  • Runtime: @luckystack/core, @luckystack/login, @luckystack/error-tracking
  • Peer (canonical ranges, standardized 2026-05-07):
    • @prisma/client@^6.19.0 (transitively required via @luckystack/core)
    • socket.io@^4.8.0

License

MIT — see LICENSE.