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

@zhyporium/rest

v1.1.0

Published

A type-safe REST client for TypeScript that provides full type checking for your API routes and responses.

Readme

@zhyporium/rest

A type-safe REST client for TypeScript that provides full type checking for your API routes and responses.

Features

  • 🔒 Type-safe: Full TypeScript support with compile-time type checking
  • 🛣️ Route definitions: Define your API routes with TypeScript interfaces
  • 📦 Multiple HTTP methods: Support for GET, POST, PATCH, PUT, and DELETE
  • 🔗 Path parameters: Support for both :param and [param] syntax
  • 🔍 Query parameters: Built-in query string handling
  • 📝 Headers: Base headers with per-request header overrides
  • Error handling: Structured error responses with status checking
  • 🎯 Response types: Automatic JSON parsing with type safety

Installation

npm install @zhyporium/rest
# or
pnpm add @zhyporium/rest
# or
yarn add @zhyporium/rest

Usage

Basic Example

import { REST } from "@zhyporium/rest";

// Define your API routes
type MyAPIRoutes = {
  GET: {
    "/users/:id": {
      params: { id: string };
      headers?: Record<string, string>;
      response: { id: string; name: string; email: string };
    };
    "/users": {
      query?: { limit?: string; offset?: string };
      headers?: Record<string, string>;
      response: { users: Array<{ id: string; name: string }> };
    };
  };
  POST: {
    "/users": {
      body: { name: string; email: string };
      headers?: Record<string, string>;
      response: { id: string; name: string; email: string };
    };
  };
  PATCH: {};
  PUT: {};
  DELETE: {};
};

// Create a REST client instance
const client = new REST<MyAPIRoutes>("https://api.example.com", {
  "Content-Type": "application/json",
  Authorization: "Bearer your-token",
});

// Make type-safe requests
const result = await client.get("/users/:id", {
  params: { id: "123" },
});

if (result.status === "success") {
  console.log(result.response.name); // TypeScript knows this is a string
  console.log(result.fetchResponse); // Access the raw Response object
} else {
  console.error(result.error); // Handle errors
}

Path Parameters

The client supports two path parameter syntaxes:

// Express-style with colon
await client.get("/users/:id", { params: { id: "123" } });

// Bracket syntax
await client.get("/users/[id]", { params: { id: "123" } });

Query Parameters

const result = await client.get("/users", {
  query: {
    limit: "10",
    offset: "20",
  },
});

POST Requests

const result = await client.post("/users", {
  body: {
    name: "John Doe",
    email: "[email protected]",
  },
  headers: {
    "X-Custom-Header": "value",
  },
});

Error Handling

All methods return a discriminated union that you can safely check:

const result = await client.get("/users/:id", {
  params: { id: "123" },
});

if (result.status === "success") {
  // TypeScript knows result.response is the response type
  console.log(result.response);
  console.log(result.fetchResponse.status); // Access raw response
} else {
  // TypeScript knows result.error is an Error
  console.error(result.error);
  if (result.fetchResponse) {
    console.error(result.fetchResponse.status); // May be null for network errors
  }
}

API Reference

REST<Routes>

The main REST client class.

Constructor

new REST<Routes>(baseUrl: string, baseHeaders: Record<string, string>)
  • baseUrl: The base URL for all requests
  • baseHeaders: Headers to include in all requests

Methods

  • get<T>(path: T, request): Promise<RESTResponse<...>>
  • post<T>(path: T, request): Promise<RESTResponse<...>>
  • patch<T>(path: T, request): Promise<RESTResponse<...>>
  • put<T>(path: T, request): Promise<RESTResponse<...>>
  • delete<T>(path: T, request): Promise<RESTResponse<...>>

Response Type

type RESTResponse<ResponseType> =
  | {
      status: "success";
      response: ResponseType;
      fetchResponse: Response;
    }
  | {
      status: "error";
      error: Error;
      fetchResponse: Response | null;
    };

Development

Install dependencies

pnpm install

Run tests

pnpm test

Build the library

pnpm build

Type checking

pnpm typecheck

Watch mode

pnpm dev

License

MIT