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

@shkumbinhsn/fetcher

v0.1.0-alpha.3

Published

Type-safe fetch wrapper with Standard Schema validation and error handling

Readme

Dog fetching a cube

@shkumbinhsn/fetcher

npm version npm downloads

Type-safe fetch wrapper with Standard Schema validation and error handling.

Features

  • 🔒 Type-safe API calls with full TypeScript support
  • ✅ Response validation using any Standard Schema compatible library (Zod, Valibot, etc.)
  • 🎯 Structured error handling with typed error responses
  • 🚀 Extended RequestInit interface - works exactly like fetch with extra features
  • 📦 Tiny bundle size with minimal dependencies

Installation

npm install @shkumbinhsn/fetcher

Quick Start

import { fetcher } from '@shkumbinhsn/fetcher';
import { z } from 'zod';

// Define your schema
const UserSchema = z.object({
  id: z.string(),
  name: z.string(),
  email: z.string().email()
});

// Basic usage (works exactly like fetch)
const data = await fetcher('/api/users');

// With schema validation
const user = await fetcher('/api/users/123', {
  schema: UserSchema
});

// user is fully typed as { id: string, name: string, email: string }
console.log(user.name);

HTTP Methods

// GET request
const user = await fetcher('/api/users/123', {
  schema: UserSchema
});

// POST request
const newUser = await fetcher('/api/users', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ name: 'John', email: '[email protected]' }),
  schema: UserSchema
});

// With authentication
const user = await fetcher('/api/users/123', {
  headers: { 'Authorization': 'Bearer your-token' },
  schema: UserSchema
});

Error Handling

import { defineError, fetcher } from '@shkumbinhsn/fetcher';
import { z } from 'zod';

// Define custom errors
const NotFoundError = defineError(
  404,
  z.object({
    message: z.string(),
    resource: z.string()
  }),
  'NotFoundError'
);

const ValidationError = defineError(
  400,
  z.object({
    errors: z.array(z.object({
      field: z.string(),
      message: z.string()
    }))
  }),
  'ValidationError'
);

// Use in API calls
try {
  const user = await fetcher('/api/users/123', {
    schema: UserSchema,
    errors: [NotFoundError, ValidationError]
  });
} catch (error) {
  if (error instanceof NotFoundError) {
    // error.data is fully typed
    console.log(`Not found: ${error.data.resource}`);
  } else if (error instanceof ValidationError) {
    // error.data.errors is fully typed
    error.data.errors.forEach(err => 
      console.log(`${err.field}: ${err.message}`)
    );
  }
}

With React Query

import { useQuery, useMutation } from '@tanstack/react-query';
import { fetcher } from '@shkumbinhsn/fetcher';

// Query
const { data, error } = useQuery({
  queryKey: ['user', userId],
  queryFn: () => fetcher(`/api/users/${userId}`, {
    schema: UserSchema,
    errors: [NotFoundError]
  })
});

// Mutation
const mutation = useMutation({
  mutationFn: (userData: CreateUserInput) => 
    fetcher('/api/users', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(userData),
      schema: UserSchema,
      errors: [ValidationError]
    })
});

TypeScript Support

The library exports a FetcherRequestInit type that extends the standard RequestInit:

import type { FetcherRequestInit } from '@shkumbinhsn/fetcher';

interface MyRequestInit extends FetcherRequestInit<UserSchema> {
  // Your custom properties
}

function myFetch(url: string, init: MyRequestInit) {
  return fetcher(url, init);
}

API Reference

fetcher<T>(url, init?)

function fetcher<TResponse extends StandardSchemaV1 | undefined = undefined>(
  input: RequestInfo | URL,
  init?: FetcherRequestInit<TResponse>
): Promise<InferResponse<TResponse>>

The main fetch wrapper function that extends the standard fetch API with:

  • schema?: TResponse - Optional response validation schema
  • errors?: ApiErrorStatic<any>[] - Optional custom error types

defineError(statusCode, schema, name?)

function defineError<TSchema extends StandardSchemaV1>(
  statusCode: number,
  schema: TSchema,
  name?: string
): ApiErrorStatic<TSchema>

Create typed error classes for API responses.

FetcherRequestInit<T>

Extended RequestInit interface that includes schema and errors properties.

Supported Schema Libraries

Any library that implements the Standard Schema specification:

License

MIT