Skip to content

Commit c50361a

Browse files
docs: improve docs to make it clear we not just zod
1 parent 3c619f8 commit c50361a

File tree

15 files changed

+161
-102
lines changed

15 files changed

+161
-102
lines changed

dev/tsconfig.json

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
{
2-
"compilerOptions": {
3-
// Enable latest features
4-
"lib": ["ESNext", "DOM"],
5-
"target": "ESNext",
6-
"module": "ESNext",
7-
"moduleDetection": "force",
8-
"jsx": "react-jsx",
9-
"allowJs": true,
2+
"compilerOptions": {
3+
// Enable latest features
4+
"lib": ["ESNext", "DOM"],
5+
"target": "ESNext",
6+
"module": "ESNext",
7+
"moduleDetection": "force",
8+
"jsx": "react-jsx",
9+
"allowJs": true,
1010

11-
// Bundler mode
12-
"moduleResolution": "bundler",
13-
"allowImportingTsExtensions": true,
14-
"verbatimModuleSyntax": true,
15-
"noEmit": true,
11+
// Bundler mode
12+
"moduleResolution": "bundler",
13+
"allowImportingTsExtensions": true,
14+
"verbatimModuleSyntax": true,
15+
"noEmit": true,
1616

17-
// Best practices
18-
"strict": true,
19-
"skipLibCheck": true,
20-
"noFallthroughCasesInSwitch": true,
17+
// Best practices
18+
"strict": true,
19+
"skipLibCheck": true,
20+
"noFallthroughCasesInSwitch": true,
2121

22-
// Some stricter flags (disabled by default)
23-
"noUnusedLocals": false,
24-
"noUnusedParameters": false,
25-
"noPropertyAccessFromIndexSignature": false
26-
}
22+
// Some stricter flags (disabled by default)
23+
"noUnusedLocals": false,
24+
"noUnusedParameters": false,
25+
"noPropertyAccessFromIndexSignature": false
26+
}
2727
}

doc/app/page.tsx

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,95 @@ import { Banner } from "fumadocs-ui/components/banner";
77
import Link from "next/link";
88

99
export default function HomePage() {
10+
const installCode = `npm install @better-fetch/fetch
11+
# Then install your preferred validator
12+
npm install zod # or valibot, arktype, etc.`;
13+
14+
const usageExample = `import { betterFetch } from '@better-fetch/fetch';
15+
import { z } from 'zod'; // or your preferred validator
16+
17+
const { data, error } = await betterFetch("https://api.example.com/todos/1", {
18+
output: z.object({
19+
id: z.number(),
20+
title: z.string(),
21+
completed: z.boolean()
22+
})
23+
});`;
24+
1025
return (
11-
<div className="relative min-h-screen flex w-full items-center justify-center overflow-hidden rounded-lg border bg-background p-20 md:shadow-xl">
26+
<div className="relative min-h-screen flex w-full flex-col items-center justify-start overflow-hidden rounded-lg border bg-background p-8 md:p-20">
1227
<Ripple />
13-
<div className="z-10 flex flex-col items-center justify-center">
14-
<h1 className="mb-4 text-5xl font-bold text-center">Better Fetch</h1>
1528

16-
<p className="text-muted-foreground mx-auto max-w-2xl text-center">
17-
Advanced fetch wrapper for typescript with zod schema validations,
18-
pre-defined routes, callbacks, plugins and more.
29+
{/* Hero Section */}
30+
<div className="z-10 flex flex-col items-center justify-center max-w-4xl">
31+
<h1 className="mb-4 text-6xl font-bold text-center bg-clip-text text-transparent bg-gradient-to-r from-primary to-primary/60">
32+
Better Fetch
33+
</h1>
34+
<p className="mb-6 text-2xl font-semibold text-center">
35+
Type-Safe API Calls with Your Favorite Validator
36+
</p>
37+
<p className="text-muted-foreground mx-auto max-w-2xl text-center text-lg mb-8">
38+
A modern fetch wrapper that works with any Standard Schema validator.
39+
Use Zod, Valibot, ArkType, or any spec-compliant library for powerful
40+
runtime validations and perfect TypeScript inference.
1941
</p>
20-
<div className="flex py-4 items-center justify-center gap-4 w-full">
42+
43+
<div className="flex py-4 items-center justify-center gap-4 w-full mb-12">
2144
<Link href="/docs">
22-
<Button className="flex gap-2 cursor-pointer">
45+
<Button size="lg" className="flex gap-2 cursor-pointer">
2346
<Icons.docs />
24-
Docs
47+
Get Started
2548
</Button>
2649
</Link>
2750
<Link href="https://github.com/bekacru/better-fetch">
28-
<Button className="flex gap-2" variant="secondary">
51+
<Button size="lg" className="flex gap-2" variant="secondary">
2952
<Icons.github />
30-
Github
53+
GitHub
3154
</Button>
3255
</Link>
3356
</div>
57+
58+
{/* Feature Grid */}
59+
<div className="grid grid-cols-1 md:grid-cols-3 gap-6 mb-12 w-full">
60+
{[
61+
{
62+
title: "Schema Agnostic",
63+
description:
64+
"Works with Zod, Valibot, ArkType, and any Standard Schema validator",
65+
},
66+
{
67+
title: "Type Safety",
68+
description:
69+
"Full TypeScript inference with your preferred validation library",
70+
},
71+
{
72+
title: "Runtime Validation",
73+
description:
74+
"Automatic request/response validation with detailed error messages",
75+
},
76+
{
77+
title: "Pre-defined Routes",
78+
description: "Define API schemas once, use them everywhere",
79+
},
80+
{
81+
title: "Universal",
82+
description:
83+
"Works in browsers, Node.js, Deno, Bun, and edge runtimes",
84+
},
85+
{
86+
title: "Advanced Features",
87+
description: "Retries, timeouts, hooks, plugins, and more",
88+
},
89+
].map((feature) => (
90+
<div key={feature.title} className="p-6 rounded-lg border bg-card">
91+
<h3 className="font-semibold mb-2">{feature.title}</h3>
92+
<p className="text-sm text-muted-foreground">
93+
{feature.description}
94+
</p>
95+
</div>
96+
))}
97+
</div>
3498
</div>
35-
<div></div>
3699
</div>
37100
);
38101
}

doc/content/docs/default-types.mdx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ description: Default Types
66

77
## Default Output
88

9-
By default, the response data will always be type of `unknown`. If you want to customize the default type you can pass the `defaultOutput` option to the `createFetch` function.
9+
By default, the response data will always be of type `unknown`. If you want to customize the default type you can pass the `defaultOutput` option to the `createFetch` function.
10+
Note: When you supply a custom output schema using a Standard Schema validator (for example, zod or any alternative),
11+
the provided output schema will override the `defaultOutput` type. This approach offers a strongly typed solution without locking you to a single library.
1012

1113
<Callout type="info">
1214
This only serves as a type for the response data it's not used as a validation schema.
1315
</Callout>
1416

1517
```ts twoslash title="fetch.ts"
1618
import { createFetch } from "@better-fetch/fetch";
17-
import { z } from "zod";
19+
// Example using zod (or any Standard Schema compliant library)
20+
import { z } from "zod";
1821

1922
const $fetch = createFetch({
2023
baseURL: "https://jsonplaceholder.typicode.com",
@@ -52,10 +55,12 @@ const { data, error } = await $fetch("/todos/1", {
5255

5356
The default error type is:
5457
```ts
55-
{ status: number, statusText: string, message?: string }.
58+
{ status: number, statusText: string, message?: string }
5659
```
5760

58-
if you want custom default error type, you can pass a `defaultError` option to the `createFetch` function.
61+
If you want a custom error type, you can pass a `defaultError` option to the `createFetch` function.
62+
Remember: Your custom error type builds on top of the default properties, and if your API returns a JSON error,
63+
// it will be merged with your definition.
5964

6065
<Callout type="info">
6166
The `status` and `statusText` properties are always defined. Your custom error definitions are only
@@ -64,7 +69,7 @@ inferred if the API returns a JSON error object.
6469

6570
```ts twoslash title="fetch.ts"
6671
import { createFetch } from "@better-fetch/fetch";
67-
import { z } from "zod";
72+
import { z } from "zod"; // Example only
6873

6974
const $fetch = createFetch({
7075
baseURL: "https://jsonplaceholder.typicode.com",

doc/content/docs/fetch-schema.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ title: Fetch Schema
33
description: Fetch Schema
44
---
55

6-
Fetch schema allows you to pre-define the url path and the shape of request and response data. You can easily document your api using this schema.
6+
Fetch schema allows you to pre-define the URL path and the shape of request and response data. You can easily document your API using this schema.
77

8-
The output of the schema will be validated using zod and if the validation fails, it'll throw an error.
8+
Better Fetch now uses Standard Schema internally, allowing you to bring your own Standard Schema-compliant validator (e.g., Zod, Valibot, ArkType).
99

10-
Before we start, make sure you have installed the [zod](https://www.npmjs.com/package/zod) package.
10+
To create a fetch schema, you need to import the `createSchema` function from `@better-fetch/fetch`.
1111

1212
```package-install
1313
npm i zod

doc/content/docs/getting-started.mdx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,19 @@ description: Getting started with Better Fetch
99
npm i @better-fetch/fetch
1010
```
1111

12-
*You also need to have [zod](https://www.npmjs.com/package/zod) installed for schema validations. If you don't plan to use zod for schema validation, you can install it as a dev dependency.*
13-
1412
```package-install
15-
npm i zod
13+
npm i Schema-validation-of-your-choice
1614
```
1715

1816
### Quick Start
1917

20-
The fastest way to start using better fetch is to import the `betterFetch` function and start making requests.
21-
22-
You can define the response type using generics or **zod schema (recommended)**.
18+
The fastest way to start using Better Fetch is to import the `betterFetch` function.
19+
You can define the response type using generics or use a schema that supports Standard Schema (recommended).
2320

2421
```ts twoslash title="fetch.ts"
2522
import { betterFetch } from '@better-fetch/fetch';
26-
import { z } from 'zod';
27-
2823

29-
//Using generic type
24+
// Using generic type
3025
const { data, error } = await betterFetch<{
3126
userId: string;
3227
id: number;
@@ -35,21 +30,22 @@ const { data, error } = await betterFetch<{
3530
}>("https://jsonplaceholder.typicode.com/todos/1");
3631

3732

38-
//Using zod schema
39-
const { data: todos, error: todoError } = await betterFetch("https://jsonplaceholder.typicodei.com/todos/1", {
33+
// Using a Standard Schema validator (for example, zod)
34+
import { z } from 'zod'; // or your preferred Standard Schema compliant library
35+
36+
const { data: todos, error: todoError } = await betterFetch("https://jsonplaceholder.typicode.com/todos/1", {
4037
output: z.object({
4138
userId: z.string(),
4239
id: z.number(),
4340
title: z.string(),
4441
completed: z.boolean(),
4542
})
4643
});
47-
4844
// @annotate: Hover over the data object to see the type
4945
```
5046

5147
<Callout type="info">
52-
Make sure strict mode is enabled in your tsconfig when using zod schema validations.
48+
Make sure strict mode is enabled in your tsconfig when using schema validations.
5349
</Callout>
5450

5551
Better fetch by default returns a `Promise` that resolves to an object of `data` and `error` but if you pass the `throw` option, it will return the parsed response data only.
@@ -108,13 +104,15 @@ Fetch schema enables you to pre-define the URL path and the shape of request and
108104
Plugins can also define fetch schemas. See [Plugins](/docs/plugins) section for more details.
109105
</Callout>
110106

111-
The output of the scheam will be validated using zod and if the validation fails, it'll throw an error.
107+
The output of the scheam will be validated using your schema and if the validation fails, it'll throw an error.
112108

113109
```ts twoslash title="fetch.ts"
114110
import { createSchema, createFetch } from "@better-fetch/fetch";
111+
112+
// ZOD example
115113
import { z } from "zod";
116114

117-
export const schema = createSchema({ // [!code highlight]
115+
export const zodSchema = createSchema({ // [!code highlight]
118116
"/path": { // [!code highlight]
119117
input: z.object({ // [!code highlight]
120118
userId: z.string(), // [!code highlight]
@@ -133,7 +131,7 @@ export const schema = createSchema({ // [!code highlight]
133131

134132
const $fetch = createFetch({
135133
baseURL: "https://jsonplaceholder.typicode.com",
136-
schema: schema // [!code highlight]
134+
schema: zodSchema // [!code highlight]
137135
});
138136

139137
const { data, error } = await $fetch("/path", {

doc/content/docs/index.mdx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,27 @@ title: Introduction
33
description: Getting started with Better Fetch
44
---
55

6-
Better fetch is advanced fetch wrapper for typescript. It supports error as value, zod schema validations, advanced type inference, pre-defined routes, hooks, plugins and more. Works on the browser, node (version 18+), workers, deno and bun.
6+
# Better Fetch
7+
8+
Better Fetch is an advanced fetch wrapper for TypeScript that supports any Standard Schema-compliant validator (like Zod, Valibot, ArkType, etc) for runtime validation and type inference. It features error-as-value handling, pre-defined routes, hooks, plugins and more. Works on the browser, node (version 18+), workers, deno and bun.
79

810
## Features
911
<Cards>
12+
<Card
13+
title="Schema Agnostic"
14+
description="Use any Standard Schema compatible validator (Zod, Valibot, ArkType, etc) for runtime validation and type inference."
15+
/>
1016
<Card
1117
title="Type safety"
12-
description="Advanced type inference and runtime validations with zod"
18+
description="Harness advanced type inference and runtime validations powered by Standard Schema. Bring your validator of choice as long as it supports standard schema interfaces."
1319
/>
1420
<Card
1521
title="Smart Parser"
1622
description="Smart response parser for any content type"
1723
/>
1824
<Card
1925
title="Fetch Schema"
20-
description="Pre defined routes with zod schema validations"
26+
description="Pre defined routes with schema validations"
2127
href="/docs/fetch-schema"
2228
/>
2329
<Card

doc/content/docs/plugins.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,14 @@ const $fetch = createFetch({
7979

8080
### Schema
8181

82-
You can define a schema for a plugin. This allows you to easily document the api usage using a schema.
82+
You can define a schema for a plugin. This allows you to easily document the API usage using a schema.
83+
The schema is now based on the Standard Schema specification.
84+
85+
Note: Better Fetch now uses Standard Schema internally so you can bring your own Standard Schema–compliant validator. You are no longer limited to zod.
8386

8487
```ts twoslash title="fetch.ts"
8588
import { createFetch, createSchema, BetterFetchPlugin } from "@better-fetch/fetch";
89+
// Example using zod (or any Standard Schema compliant validator)
8690
import { z } from "zod";
8791
// @errors: 2353 2561
8892
const plugin = {

doc/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"typecheck": "tsc --noEmit"
1010
},
1111
"dependencies": {
12-
"@better-fetch/fetch": "^1.1.0",
13-
"@better-fetch/logger": "^1.1.0",
12+
"@better-fetch/fetch": "workspace:*",
13+
"@better-fetch/logger": "workspace:*",
1414
"@radix-ui/react-icons": "^1.3.0",
1515
"@radix-ui/react-slot": "^1.1.0",
1616
"class-variance-authority": "^0.7.0",
@@ -26,7 +26,7 @@
2626
"next": "^14.2.4",
2727
"react": "^18.3.1",
2828
"react-dom": "^18.3.1",
29-
"tailwind-merge": "^2.4.0",
29+
"tailwind-merge": "^2.5.2",
3030
"zod": "^3.24.0"
3131
},
3232
"devDependencies": {

packages/better-fetch/src/create-fetch/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export type FetchSchema = {
55
input?: StandardSchemaV1;
66
output?: StandardSchemaV1;
77
query?: StandardSchemaV1;
8-
params?: StandardSchemaV1<Record<string, unknown>> | undefined;
8+
params?: StandardSchemaV1<Record<string, unknown>> | undefined;
99
method?: Methods;
1010
};
1111

packages/better-fetch/src/test/create.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import {
1919
import type { BetterFetchResponse } from "../types";
2020

2121
import { BetterFetchPlugin } from "../plugins";
22+
import { ValidationError } from "../utils";
2223
import { router } from "./test-router";
23-
import { ValidationError } from '../utils';
2424

2525
const schema = {
2626
"/": {

0 commit comments

Comments
 (0)