shahradelahi / zod-request

🛡️ Type-safe and Validated HTTP requests using Zod

Home Page:https://www.npmjs.com/package/zod-request

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

zod-request

Valid and Type-safe HTTP requests using Zod

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

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

Relevant Links

License

This project is licensed under the GPL-3.0 License - see the LICENSE file for details

About

🛡️ Type-safe and Validated HTTP requests using Zod

https://www.npmjs.com/package/zod-request

License:MIT License


Languages

Language:TypeScript 100.0%