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

@pingpong-js/plugin-zod

v1.0.1

Published

Zod schema validation plugin for @pingpong-js/fetch with type-safe API responses

Readme

@pingpong-js/plugin-zod

Zod schema validation plugin for @pingpong-js/fetch with type-safe API responses.

Installation

npm install @pingpong-js/plugin-zod
# peer dependencies
npm install @pingpong-js/fetch zod

Features

  • Response Validation: Validate API responses against Zod schemas
  • 🔄 Data Transformation: Parse and transform response data
  • 📝 Request Validation: Validate request bodies before sending
  • 🎯 URL Pattern Matching: Different schemas for different endpoints
  • 🏭 Typed Client Factory: Create fully typed API clients
  • ⚠️ Custom Error Handling: Flexible error handling options

Usage

Basic Validation

import pingpong from '@pingpong-js/fetch';
import { withZod } from '@pingpong-js/plugin-zod';
import { z } from 'zod';

const UserSchema = z.object({
  id: z.number(),
  name: z.string(),
  email: z.string().email(),
});

pingpong.onResponse(withZod(UserSchema));

const user = await pingpong.get('/api/users/1');
// Throws ZodValidationError if response doesn't match schema!

Data Transformation

import pingpong from '@pingpong-js/fetch';
import { withZodParse } from '@pingpong-js/plugin-zod';
import { z } from 'zod';

const UserSchema = z.object({
  id: z.number(),
  created_at: z.string().transform(s => new Date(s)), // Transform to Date
  name: z.string().transform(s => s.toUpperCase()),   // Transform to uppercase
});

pingpong.onResponse(withZodParse(UserSchema));

const user = await pingpong.get('/api/users/1');
console.log(user.data.created_at instanceof Date); // true!
console.log(user.data.name); // "JOHN DOE"

Schema Registry

Different schemas for different endpoints:

import pingpong from '@pingpong-js/fetch';
import { withZodRegistry } from '@pingpong-js/plugin-zod';
import { z } from 'zod';

let lastUrl = '';
pingpong.onRequest(req => { lastUrl = req.url; return req; });

const UserSchema = z.object({ id: z.number(), name: z.string() });
const PostSchema = z.object({ id: z.number(), title: z.string() });

pingpong.onResponse(withZodRegistry(
  [
    [/\/users\/\d+$/, UserSchema],
    [/\/posts\/\d+$/, PostSchema],
  ],
  () => lastUrl
));

Typed Client Factory

Create fully typed API clients:

import pingpong from '@pingpong-js/fetch';
import { createTypedClient } from '@pingpong-js/plugin-zod';
import { z } from 'zod';

const UserSchema = z.object({
  id: z.number(),
  name: z.string(),
  email: z.string().email(),
});

const userApi = createTypedClient(pingpong, UserSchema);

// Returns validated, typed data directly!
const user = await userApi.get('/api/users/1');
console.log(user.name); // TypeScript knows this is string!

Request Body Validation

import pingpong from '@pingpong-js/fetch';
import { withZodRequest } from '@pingpong-js/plugin-zod';
import { z } from 'zod';

const CreateUserSchema = z.object({
  name: z.string().min(1),
  email: z.string().email(),
});

pingpong.onRequest(withZodRequest(CreateUserSchema, {
  methods: ['POST', 'PUT'],
  urlPattern: /\/users/,
}));

// Throws ZodValidationError if body is invalid!
await pingpong.post('/api/users', {
  name: '',  // Error: name must be at least 1 character
  email: 'invalid', // Error: invalid email
});

Error Response Validation

import pingpong from '@pingpong-js/fetch';
import { withZodError } from '@pingpong-js/plugin-zod';
import { z } from 'zod';

const ErrorSchema = z.object({
  error: z.object({
    code: z.string(),
    message: z.string(),
  }),
});

pingpong.onResponse(withZodError(ErrorSchema));

API Reference

withZod(schema, options?)

Validate response data against a Zod schema.

Options:

  • strict: Throw on validation failure (default: true)
  • onError: Custom error handler
  • formatError: Custom error message formatter
  • statusCodes: Only validate specific status codes

withZodParse(schema, options?)

Parse and transform response data. Replaces response.data with parsed value.

withZodError(schema, options?)

Validate error responses (non-2xx status codes).

withZodRegistry(schemas, urlExtractor, options?)

Validate responses based on URL patterns.

createTypedClient(client, schema, options?)

Create a typed API client that returns validated data directly.

withZodRequest(schema, options?)

Validate request bodies before sending.

ZodValidationError

Custom error class with access to the underlying Zod error.

import { ZodValidationError } from '@pingpong-js/plugin-zod';

try {
  await pingpong.get('/api/users/1');
} catch (error) {
  if (error instanceof ZodValidationError) {
    console.log(error.zodError.issues);
  }
}

License

MIT