百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Z

zod-fetch

> 编程语言
开源

使用 Zod 构建类型安全的获取器的简单函数

695 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

使用 Zod 构建类型安全的获取器的简单函数

Zod Fetch

zod-fetch is a simple API for building a type and runtime-safe fetcher function using Zod schemas.

Usage

Using the default fetcher

You can create a fetcher using createZodFetcher:

import { z } from "zod";
import { createZodFetcher } from "zod-fetch";

const fetchWithZod = createZodFetcher();

fetchWithZod(
  // The schema you want to validate with
  z.object({
    hello: z.literal("world"),
  }),
  // Any parameters you would usually pass to fetch
  "/my-api",
).then((res) => {
  console.log(res);
  //          ^? { hello: string }
});

If you don't pass a fetcher to createZodFetcher, it uses a sensible default fetcher (using the fetch API) to grab the data needed.

Using a custom fetcher

You can pass custom fetchers to createZodFetcher:

import { z } from "zod";
import { createZodFetcher } from "zod-fetch";
import axios from "axios";

const fetchWithZod = createZodFetcher(axios.get);

fetchWithZod(
  z.object({
    data: z.object({
      name: z.string(),
    }),
  }),
  "/user",
  {
    params: {
      id: 12345,
    },
  },
).then((res) => {
  console.log(res);
  //          ^? { data: { name: string } }
});

Why does this exist?

For teams that want to combine runtime-safety with a fetcher (an extremely common use case), you often have a choice:

  1. Use a big, all-encompassing solution like tRPC
  2. Hack something together on your own

I hope that this small API offers a nice compromise for teams looking to bridge that gap. Or, at least, offers some pretty example code you can copy-and-paste into your projects.

Want to learn TypeScript?

zod-fetch only really exists because there's some TypeScript magic required to help zod and fetch knit together nicely.

I cover this TS magic in my TypeScript course, Total TypeScript.

There's also a free Beginners TypeScript Tutorial to get you started, and even one on Zod!

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言