Skip to content

Commit ffc1207

Browse files
authored
feat: add support for header validation and aggregation in fetch schema (#65)
1 parent 825246b commit ffc1207

File tree

13 files changed

+529
-45
lines changed

13 files changed

+529
-45
lines changed

dev/index.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
1-
import { betterFetch, createFetch, createSchema } from "@better-fetch/fetch";
1+
import { createFetch, createSchema } from "@better-fetch/fetch";
22
import { z } from "zod";
3-
3+
44
const $fetch = createFetch({
5-
schema: createSchema({
6-
"@patch/user": {
7-
input: z.object({
8-
id: z.string(),
5+
baseURL: "https://jsonplaceholder.typicode.com",
6+
schema: createSchema({
7+
"/path": {
8+
input: z.object({
9+
userId: z.string(),
10+
id: z.number(),
11+
title: z.string(),
12+
completed: z.boolean(),
13+
}),
14+
headers: z.object({
15+
"x-custom-header": z.string(),
916
}),
10-
},
11-
}),
12-
baseURL: "http://localhost:3000",
13-
onRequest(context) {
14-
console.log("onRequest", JSON.parse(context.body));
17+
},
18+
}),
19+
auth: {
20+
type: "Bearer",
21+
token:"test-token",
22+
}
23+
})
24+
25+
const { data, error } = await $fetch("/path", {
26+
body: {
27+
userId: "1",
28+
id: 1,
29+
title: "title",
30+
completed: true,
31+
},
32+
headers: {
33+
"x-custom-header": "custom-value",
1534
},
16-
});
35+
})
1736

18-
const f = $fetch("https://jsonplaceholder.typicode.com/todos/:id", {
19-
method: "GET",
20-
params: {
21-
id: "1",
22-
},
23-
});
37+
console.log(data, error);

doc/content/docs/authorization.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ description: Authorization
66
Authorization is a way that allows you to add authentication headers to the request.
77
Currently, supports `Bearer` and `Basic` authorization.
88

9+
<Callout type="info">
10+
For more advanced header handling and validation, see the [Headers](/docs/headers) documentation.
11+
</Callout>
12+
913
### Bearer
1014

1115
The bearer authorization is used to add a bearer token to the request. The token is added to the `Authorization` header.

doc/content/docs/fetch-schema.mdx

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ const $fetch = createFetch({
4747
## Fetch Schema
4848

4949
The Fetch Schema is a map of path/url and schema. The path is the url path and the schema is an object with
50-
`input`, `output`, `query` and `params` keys.
50+
`input`, `output`, `query`, `params`, and `headers` keys.
5151

52-
The `input` key is the schema of the request data. The `output` key is the schema of the response data. The `query` key is the schema of the query params. The `params` key is dynamic path parameters.
52+
The `input` key is the schema of the request data. The `output` key is the schema of the response data. The `query` key is the schema of the query params. The `params` key is dynamic path parameters. The `headers` key is for header validation.
53+
54+
<Callout type="info">
55+
For detailed information about header validation and types, see the [Headers](/docs/headers) documentation.
56+
</Callout>
5357

5458
### Input
5559

@@ -143,6 +147,38 @@ const { data, error } = await $fetch("/path", {
143147
// @annotate: Hover over the data object to see the type
144148
```
145149

150+
### Headers
151+
152+
The headers schema is the schema of the request headers. You can define header validation using Zod or any StandardSchema library.
153+
154+
```ts title="fetch.ts"
155+
import { createFetch, createSchema } from "@better-fetch/fetch";
156+
import { z } from "zod";
157+
158+
const $fetch = createFetch({
159+
baseURL: "http://localhost:3000",
160+
schema: createSchema({
161+
"/api/users": {
162+
headers: z.object({
163+
"x-api-key": z.string(),
164+
"x-user-id": z.string().uuid(),
165+
}),
166+
},
167+
}),
168+
})
169+
170+
const { data } = await $fetch("/api/users", {
171+
headers: {
172+
"x-api-key": "my-key",
173+
"x-user-id": "123e4567-e89b-12d3-a456-426614174000",
174+
},
175+
})
176+
```
177+
178+
<Callout type="info">
179+
Header keys should be defined in lowercase for case-insensitive matching. See [Headers](/docs/headers) for more details.
180+
</Callout>
181+
146182
### Dynamic Path Parameters
147183

148184
The params schema is the schema of the path params. You can either use the `params` key to define the paramters or prepend `:` to the path to make the parameters dynamic.

doc/content/docs/getting-started.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,16 @@ Learn more about handling errors [Handling Errors](/docs/handling-errors) sectio
100100

101101
### Fetch Schema
102102

103-
Fetch schema enables you to pre-define the URL path and the shape of request and response data. This makes it easy to document your API.
103+
Fetch schema enables you to pre-define the URL path and the shape of request and response data, including headers, query parameters, and path parameters. This makes it easy to document your API.
104104

105105
<Callout type="info">
106106
Plugins can also define fetch schemas. See [Plugins](/docs/plugins) section for more details.
107107
</Callout>
108108

109109
The output of the schema will be validated using your schema and if the validation fails, it'll throw an error.
110110

111+
You can validate headers, request body, query params, and more. See [Fetch Schema](/docs/fetch-schema) and [Headers](/docs/headers) for more details.
112+
111113
```ts twoslash title="fetch.ts"
112114
import { createSchema, createFetch } from "@better-fetch/fetch";
113115

0 commit comments

Comments
 (0)