Socket
Book a DemoInstallSign in
Socket

zod-request

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zod-request

Validated and Type-safe HTTP requests using Zod

0.2.2
latest
Source
npmnpm
Version published
Weekly downloads
2.8K
-6.24%
Maintainers
1
Weekly downloads
 
Created
Source

zod-request

Validated and Type-safe HTTP requests using Zod

npm install size

Notable Features

  • Exact API as Native fetch with extra features
  • Supports every environment (Node.js, Browser, Bun, etc.)

📦 Installation

npm install zod-request zod

📖 Usage

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()
});

const response = await fetch('https://jsonplaceholder.typicode.com/todos', {
  method: 'GET',
  headers: {
    'Content-Type': 'application/json'
  },
  schema: {
    response: z.array(todoSchema)
  }
});

// type of data is [{ userId: number, id: number, title: string, completed: boolean }, ...]
const data = await response.json();

console.log(Array.isArray(data)); // true
console.log(data.length); // 200

📚 Documentation

For all configuration options, please see the API docs.

🤝 Contributing

You can contribute to this project by opening an issue or a pull request on GitHub. Feel free to contribute, we care about your ideas and suggestions.

Examples

Send a Post request with a FormData body
import { fetch } from 'zod-request';
import { z } from 'zod';

const schema = {
  body: z.object({
    name: z.string(),
    age: z.number()
  }),
  response: z.object({
    form: z.record(z.any()),
    headers: z.record(z.string())
  })
};

const response = await fetch('https://httpbin.org/post', {
  method: 'POST',
  form: {
    name: 'John',
    age: 20
  },
  schema: schema
});

const { form, headers } = await response.json();
console.log(form); // { name: 'John', age: '20' }
console.log(headers); // { 'Content-Type': 'multipart/form-data; boundary=---- ...
Skip body validation for a request
const response = await fetch('https://jsonplaceholder.typicode.com/todos', {
  method: 'GET',
  headers: {
    'Content-Type': 'application/json'
  }
});

const data = await response.unsafeJson(); // Throws an error if the response is not a valid JSON

console.log(Array.isArray(data)); // true
console.log(data.length); // 200

License

MIT © Shahrad Elahi

Keywords

zod

FAQs

Package last updated on 27 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.