@kwaimind/zodfetch
v0.0.2
Published
Type-safe fetch wrapper with Zod validation.
Downloads
28
Readme
@kwaimind/zodfetch
Type-safe fetch wrapper with Zod validation.
Install
npm install @kwaimind/zodfetch zodUsage
Uses the same fetch API but with some zod sugar.
import { z } from "zod";
import { zodFetch } from "@kwaimind/zodfetch";
const userSchema = z.object({
id: z.number(),
name: z.string(),
email: z.email(),
});
// GET request
const user = await zodFetch("https://api.example.com/users/123", userSchema);
// POST request with body
const newUser = await zodFetch("https://api.example.com/users", userSchema, {
method: "POST",
body: JSON.stringify({ name: "John", email: "[email protected]" }),
headers: { "Content-Type": "application/json" }
});
// With custom headers
const user = await zodFetch("https://api.example.com/users/123", userSchema, {
headers: { Authorization: "Bearer token" }
});Error handling
import { ValidationError } from "zod-fetch";
try {
await zodFetch("https://api.example.com/users/123", userSchema);
} catch (error) {
if (error instanceof ValidationError) {
console.log(error.issues); // Zod validation issues
}
}