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

zetch

v1.0.7

Published

HTTP Client with static type inference from your Zod Schemas

Readme

Table of contents

Introduction

Zetch is a HTTP Client that allows you to get static type inference from your existing Zod schemas.

Zetch is designed to be as simple, light-weight and un-opinionated as possible. The goal is that this will eliminate the need for you to manually pass in types to your API calls. Instead, you can just pass in your Zod schema associated with the API call and get immediate static type inference.

Some key features of Zetch are:

  • Static type inference - Zetch will infer the types of your API calls from your Zod schemas.
  • Retries Configuration - Zetch will retry your API calls to your specified amount if they fail and they meet your specified status codes.
  • Auth Configuration - Zetch allows you to configure your Authorization headers by providing your token and token scheme. If the request fails, your provided refeshToken function will run to get a new token prior to the retried request.
  • Error logging - Zetch will log API errors or Schema Validation errors to your configured onError functions.
  • Base Headers - Zetch allows you to configure your headers in a simple way for all of your calls for one client.
  • Request Configuration - Zetch allows you to configure your specific requests within a Zetch Client.

Installation

npm install zetch       # npm
yarn add zetch          # yarn
bun add zetch           # bun
pnpm add zetch         # pnpm

Usage

Making a GET request

// Create a schema for a Post
const postSchema = z.object({
    title: z.string(),
    body: z.string(),
    id: z.number(),
    userId: z.number(),
});

// Make a request to get Posts with your schema provided to get static type inference
await zetch.get('https://jsonplaceholder.typicode.com/posts', {
    validationSchema: z.array(postSchema),
});

Making a request with a body

// Make a POST request with your body
await zetch.post('/posts', {
    body: {
        title: 'foo',
        body: 'bar',
        userId: 1,
        id: 1,
    },
});

// Make a POST request with FormData
const formData = new FormData();
formData.append('title', 'foo');
formData.append('body', 'bar');
formData.append('userId', '1');
formData.append('id', '1');

await zetch.post('/posts', {
    body: formData,
});

Creating an API client and making a request

// Create a Zetch Client with a Base Url
const zetchClient = zetch.create({
  baseUrl: 'https://jsonplaceholder.typicode.com',
});

// Create a schema for a Post
const postSchema = z.object({
  title: z.string(),
  body: z.string(),
  id: z.number(),
  userId: z.number(),
});

// Make a request to get Posts with your schema provided to get static type inference
const result = await zetchClient.get('/posts', {
  validationSchema: z.array(postSchema),
});

Configs

AuthConfig

AuthConfig is used to configure the Authorization header for your requests and add in refreshing your token prior to a retried request.

interface AuthConfig {
  // The function you'd like called to refresh the token
  refreshToken?: () => Promise<string>;
  // The token scheme you'd like to use (Basic, Bearer, JWTBearer)
  tokenScheme: TokenScheme;
  // Function to return the token you'd like to use
  token: () => string;
}

RetriesConfig

Retries Config allows you to configure the number of retries and status codes for retries.

interface RetriesConfig {
  // Status codes you'd like to retry on
  retryStatuses: number[];
  // The max number of retries you'd like to allow
  numberOfRetries?: number;
}

BaseZetchConfig

Base Zetch Config that's utilized for both requests and creating an API client

interface BaseZetchConfig {
  headers?: Headers;

  // Configuration for authentication
  authConfig?: AuthConfig;

  // Configuration for retries
  retriesConfig?: RetriesConfig;

  // The function you'd like to use to log API errors to your error service of choice
  onApiError?: (error: ZetchError) => void;

  // The function you'd like to use to log API Validation errors to your error service of choice
  onApiValidationError?: (error: ZodError) => void;
}

ZetchRequestConfig

Configs for a Zetch Request

// Config used for all Zetch Requests except for GET requests
interface ZetchRequestConfig<ValidationSchema extends ZodFirstPartySchemaTypes>
  extends BaseZetchConfig {
  // The validation schema you'd like to use for the response
  validationSchema?: ValidationSchema;

  // The request body you'd like to send with the request
  body?: FormData | unknown[] | { [key: string]: unknown };

  // The abort controller you'd like to use for this request, in the event you would like to cancel the request
  abortController?: AbortController;
}

// Config used for GET requests
type ZetchGetRequestConfig<
  ValidationSchema extends ZodFirstPartySchemaTypes
> = Omit<ZetchRequestConfig<ValidationSchema>, 'body'>;

ZetchClientConfig

Config for your Zetch API Client from using zetch.create

interface ZetchClientConfig extends BaseZetchConfig {
  // The base url for your client
  baseUrl: string;
}