https://github.com/shahradelahi/zod-request
🛡️ Type-safe and Validated HTTP requests using Zod
https://github.com/shahradelahi/zod-request
browser fetch node typescript zod
Last synced: 5 months ago
JSON representation
🛡️ Type-safe and Validated HTTP requests using Zod
- Host: GitHub
- URL: https://github.com/shahradelahi/zod-request
- Owner: shahradelahi
- License: mit
- Created: 2024-01-28T20:42:44.000Z (over 1 year ago)
- Default Branch: canary
- Last Pushed: 2025-05-09T04:19:35.000Z (5 months ago)
- Last Synced: 2025-05-14T00:41:53.961Z (5 months ago)
- Topics: browser, fetch, node, typescript, zod
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/zod-request
- Size: 138 KB
- Stars: 178
- Watchers: 3
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# zod-request
[](https://www.npmjs.com/package/zod-request)
[](https://packagephobia.com/result?p=zod-request)
[](https://choosealicense.com/licenses/mit/)
[](https://github.com/shahradelahi/zod-request/stargazers)> Validated and Type-safe HTTP requests using Zod
## Notable Features
- Exact API as Native `fetch` with extra features
- Supports every environment (**Node.js**, **Browser**, Bun, etc.)## 📦 Installation
```bash
npm install zod-request zod
```## 📖 Usage
```typescript
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: {
Accept: '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](https://paka.dev/npm/zod-request@canary/api).
## 🤝 Contributing
You can contribute to this project by opening an issue or a pull request
on [GitHub](https://github.com/shahradelahi/zod-request). Feel free to contribute, we care about your ideas and
suggestions.## Examples
Send a Post request with a FormData body
```typescript
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
```typescript
const response = await fetch('https://jsonplaceholder.typicode.com/todos', {
method: 'GET',
headers: {
Accept: '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
- [Zod Documentation](https://zod.dev/)
- [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
- [Transform JSON to Zod Schema](https://transform.tools/json-to-zod)## License
[MIT](LICENSE) © [Shahrad Elahi](https://github.com/shahradelahi)