Registry / http-networking / zod-request

zod-request

JSON →
library0.2.2jsnpmunverified

zod-request (version 0.2.2) is a JavaScript/TypeScript library designed to provide validated and type-safe HTTP requests by integrating with the Zod schema validation library. It offers an API that closely mirrors the native `fetch` function, enhancing it with automatic request and response payload validation against Zod schemas. This ensures that both outgoing request bodies and incoming response data conform to predefined types, significantly reducing runtime errors related to data shape. The library is environment-agnostic, supporting Node.js (v18+), browsers, and other JavaScript runtimes like Bun. Its key differentiators include its `fetch`-like interface for familiarity, universal compatibility, and its deep integration with Zod, allowing developers to leverage Zod's powerful schema definition capabilities for end-to-end type safety in network communication. As of its current version, 0.2.2, it appears to be in an active development phase, with a focus on core features and stability rather than a fixed release cadence.

npm install zod-request
INSTALL
IMPORT
SIG · ZOD-REQUEST
Z
zod-request
http-networkingjavascriptv0.2.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fetch
import { fetch } from 'zod-request'
const { fetch } = require('zod-request')
The primary enhanced fetch function, providing Zod validation. zod-request is primarily an ESM library, though bundlers can transpile for CJS.
Response
import type { Response } from 'zod-request'
Type import for the enhanced Response object returned by `zod-request`'s fetch, useful for explicit type annotations.
RequestInit
import type { RequestInit } from 'zod-request'
Type import for the extended RequestInit options, which includes the `schema` property for defining Zod validation schemas.

Demonstrates how to fetch and validate an array of to-do items from a public API using Zod schemas for response validation.

import { fetch } from 'zod-request'; import { z } from 'zod'; const todoSchema = z.object({ userId: z.number(), id: z.number(), title: z.string(), completed: z.boolean() }); async function fetchTodos() { try { const response = await fetch('https://jsonplaceholder.typicode.com/todos', { method: 'GET', headers: { 'Content-Type': 'application/json' }, schema: { response: z.array(todoSchema) } }); const data = await response.json(); console.log('Fetched data length:', data.length); console.log('First item:', data[0]); // Example of accessing data safely, TypeScript will infer the type based on todoSchema if (data.length > 0) { console.log('First todo title:', data[0].title); } } catch (error) { console.error('Error fetching todos:', error); } } fetchTodos();
Debug
Known issues
gotchazod-request requires `zod` as a peer dependency for schema definition. Forgetting to install it will lead to runtime errors.
fix
npm install zod
affects: >=0.1.0
gotchaAs a relatively new library (v0.2.2), `zod-request`'s API surface might evolve rapidly. Monitor release notes for potential breaking changes in minor versions.
fix
Always pin to exact versions (e.g., '0.2.2') or use tilde (e.g., '~0.2.2') for minor updates, and review changelogs before upgrading.
affects: >=0.1.0
gotchaResponse body validation only occurs when methods like `response.json()` or `response.formData()` are called. Using `response.unsafeJson()` bypasses Zod validation entirely.
fix
Always use `response.json()`, `response.text()`, etc., when validation is desired. Only use `response.unsafeJson()` if you explicitly want to skip validation for that response.
affects: >=0.1.0
gotchaIncorrectly defining `schema.body` for request payloads or `schema.response` for response payloads will lead to validation failures or unexpected type inference.
fix
Ensure `schema.body` matches the expected request payload type and `schema.response` accurately describes the expected response body structure, using appropriate Zod methods like `z.object`, `z.array`, `z.string`, etc.
affects: >=0.1.0
Errors
Common errors & fixes
ReferenceError: z is not defined
The Zod library, `zod`, is not installed or imported.
fix
Ensure `npm install zod` is run and `import { z } from 'zod';` is present in your file.
TypeError: (0 , zod_request__WEBPACK_IMPORTED_MODULE_0__.fetch) is not a function
Attempting to use `zod-request` with CommonJS `require()` syntax in an ESM-only environment, or a misconfigured bundler.
fix
Ensure your project uses ESM imports (`import { fetch } from 'zod-request';`) and your `package.json` specifies `"type": "module"` if running directly in Node.js, or configure your bundler (Webpack/Rollup/Vite) correctly.
ZodError: Invalid input
The incoming HTTP response body or outgoing request body does not match the defined Zod schema.
fix
Inspect the actual payload using network tools or `console.error(error)` in the catch block to debug schema discrepancies. Adjust your Zod schema to accurately reflect the data structure.
TypeError: fetch is not a function
The native `fetch` API is not available in the current environment (e.g., older Node.js versions) or the `zod-request` `fetch` was not correctly imported.
fix
Ensure your Node.js version is >=18.0.0. Verify `import { fetch } from 'zod-request';` is at the top of your file.
Upgrade
Version history
0.2.2latest on npm
Audit
Dependencies
zodrequiredRequired for defining schemas to validate request and response payloads.
Agent activity
45 hits · last 30 days
node
38
OpenAI (training)
1
Resources
zod-request — npm install zod-request · libregistry