@zap-studio/fetch 0.5.6 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +63 -67
- package/LICENSE +1 -1
- package/README.md +49 -224
- package/dist/index.d.ts +126 -7
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +315 -6
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +29 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +11 -16
- package/dist/constants.d.ts +0 -17
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js +0 -21
- package/dist/constants.js.map +0 -1
- package/dist/fetch-D0IXjsUD.js +0 -222
- package/dist/fetch-D0IXjsUD.js.map +0 -1
- package/dist/fetch.d.ts +0 -114
- package/dist/fetch.d.ts.map +0 -1
- package/dist/fetch.js +0 -3
- package/dist/headers.d.ts +0 -26
- package/dist/headers.d.ts.map +0 -1
- package/dist/headers.js +0 -34
- package/dist/headers.js.map +0 -1
- package/dist/request.d.ts +0 -30
- package/dist/request.d.ts.map +0 -1
- package/dist/request.js +0 -43
- package/dist/request.js.map +0 -1
- package/dist/url.d.ts +0 -26
- package/dist/url.d.ts.map +0 -1
- package/dist/url.js +0 -60
- package/dist/url.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,44 +1,61 @@
|
|
|
1
|
-
|
|
1
|
+
# Changelog
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [1.0.0]
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
- Clarified tree-shakeable design in the package description and README (no code change).
|
|
12
|
+
|
|
13
|
+
### Removed
|
|
14
|
+
|
|
15
|
+
Collapsed the internal request pipeline (`_internal.ts`, `_methods.ts`, `constants.ts`, `headers.ts`, `request.ts`, `url.ts`) into implementation-only files. `mergeHeaders`, `normalizeRequest`, and `resolveRequestUrl` are no longer public API — they were pipeline internals, not meant for standalone use.
|
|
16
|
+
|
|
17
|
+
- Removed the `./constants`, `./headers`, `./request`, `./url`, and `./fetch` subpath exports. Use the root `@zap-studio/fetch` entry instead.
|
|
18
|
+
- `GLOBAL_DEFAULTS`, `FetchError`, `$fetch`, `api`, `createFetch`, and all public types are unaffected and still exported from `.`; `./errors` and `./types` subpaths are unaffected.
|
|
19
|
+
|
|
20
|
+
## [0.5.6]
|
|
21
|
+
|
|
22
|
+
### Added
|
|
4
23
|
|
|
5
24
|
The package root now re-exports the full public API, so everything can be imported from `@zap-studio/fetch` directly (`$fetch`, `api`, `createFetch`, `FetchError`, `mergeHeaders`, `GLOBAL_DEFAULTS`, `normalizeRequest`, `resolveRequestUrl`, and all public types). All exports are side-effect free and tree-shakeable; granular subpath imports keep working.
|
|
6
25
|
|
|
7
26
|
- The `$fetch`/`api`/`createFetch` implementation moved from the entrypoint into its own module, available as the new `./fetch` subpath.
|
|
27
|
+
|
|
28
|
+
### Removed
|
|
29
|
+
|
|
8
30
|
- Removed the `./internal` and `./methods` subpath exports. Both were implementation details (`fetchInternal`, `createMethod`) and are no longer part of the public API.
|
|
9
31
|
|
|
10
|
-
##
|
|
32
|
+
## [0.5.5]
|
|
11
33
|
|
|
12
|
-
###
|
|
34
|
+
### Changed
|
|
13
35
|
|
|
14
36
|
Internal formatting and lint cleanup only. No public API or behavior change.
|
|
15
37
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
## 0.5.4
|
|
38
|
+
## [0.5.4]
|
|
19
39
|
|
|
20
40
|
### Changed
|
|
21
41
|
|
|
22
42
|
- Added fetch ecosystem benchmarks and grouped benchmark output for easier cross-library comparisons.
|
|
23
43
|
- Applied small internal performance optimizations in request preparation, URL resolution, and header merging without changing public API behavior.
|
|
24
44
|
|
|
25
|
-
## 0.5.3
|
|
45
|
+
## [0.5.3]
|
|
26
46
|
|
|
27
47
|
### Changed
|
|
28
48
|
|
|
29
49
|
- Refactor `createFetch(...)` to derive fallback defaults from `GLOBAL_DEFAULTS` instead of re-defining primitive defaults inline.
|
|
30
50
|
|
|
31
|
-
## 0.5.2
|
|
51
|
+
## [0.5.2]
|
|
32
52
|
|
|
33
53
|
### Changed
|
|
34
54
|
|
|
35
55
|
- Expand TSDoc coverage across fetch modules and exported contracts for stronger JSR documentation completeness.
|
|
36
|
-
|
|
37
|
-
### Dependencies
|
|
38
|
-
|
|
39
56
|
- Updated dependency `@zap-studio/validation` to `0.3.4`.
|
|
40
57
|
|
|
41
|
-
## 0.5.1
|
|
58
|
+
## [0.5.1]
|
|
42
59
|
|
|
43
60
|
### Fixed
|
|
44
61
|
|
|
@@ -47,19 +64,18 @@ Internal formatting and lint cleanup only. No public API or behavior change.
|
|
|
47
64
|
### Changed
|
|
48
65
|
|
|
49
66
|
- 2ea1a70: Cleaned up public option typings by removing redundant `| undefined` unions from fetch configuration types and overloads.
|
|
50
|
-
|
|
51
|
-
### Dependencies
|
|
52
|
-
|
|
53
67
|
- Updated dependency `@zap-studio/validation` to `0.3.3`.
|
|
54
68
|
|
|
55
|
-
## 0.5.0
|
|
69
|
+
## [0.5.0]
|
|
56
70
|
|
|
57
|
-
###
|
|
71
|
+
### Added
|
|
58
72
|
|
|
59
|
-
-
|
|
73
|
+
- Documented the throwable error surface for `$fetch`, `createFetch`, and internal request execution with explicit `@throws` tags (for example `FetchError`, `ValidationError`, `TypeError`, `DOMException`, `SyntaxError`, and validator-thrown errors).
|
|
74
|
+
- Added full package test coverage across statements, branches, functions, and lines.
|
|
60
75
|
|
|
61
76
|
### Changed
|
|
62
77
|
|
|
78
|
+
- **Breaking:** Request bodies are no longer auto-serialized from plain objects; use the explicit `json` option (or set `body` yourself). `body` and `json` are mutually exclusive at the type level and enforced at runtime.
|
|
63
79
|
- Simplified the request API around web platform types.
|
|
64
80
|
- The first argument is named `input` and typed as `FetchInput` (`Parameters<typeof fetch>[0]` from `lib.dom`), exported from `@zap-studio/fetch/types`, so allowed inputs track global `fetch` when DOM typings change.
|
|
65
81
|
- Non-`Request` values (including `URL`) are normalized to a string URL before query merge.
|
|
@@ -71,46 +87,35 @@ Internal formatting and lint cleanup only. No public API or behavior change.
|
|
|
71
87
|
- `body` and `json` are mutually exclusive in TypeScript and guarded at runtime.
|
|
72
88
|
- Reworked URL handling to use the platform `URL` and `URLSearchParams` APIs while preserving relative URL output when no `baseURL` is configured.
|
|
73
89
|
- Simplified the internal module structure and removed the old `utils` module.
|
|
90
|
+
- Reworked tests to mirror the `src` module structure.
|
|
91
|
+
- JSR dependency mapping now pins `@zap-studio/validation` to `0.3.2`.
|
|
74
92
|
|
|
75
93
|
### Fixed
|
|
76
94
|
|
|
77
95
|
- Fixed absolute URL handling when no `baseURL` is configured.
|
|
78
96
|
- `resolveSearchParams` keeps a trailing `#` when the input URL had an empty fragment (for example `.../path#`), matching typical `URL` serialization instead of dropping the delimiter.
|
|
79
97
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
- Reworked tests to mirror the `src` module structure.
|
|
83
|
-
- Added full package coverage across statements, branches, functions, and lines.
|
|
84
|
-
|
|
85
|
-
### Documentation
|
|
86
|
-
|
|
87
|
-
- Documented the throwable error surface for `$fetch`, `createFetch`, and internal request execution with explicit `@throws` tags (for example `FetchError`, `ValidationError`, `TypeError`, `DOMException`, `SyntaxError`, and validator-thrown errors).
|
|
88
|
-
|
|
89
|
-
### Dependencies
|
|
90
|
-
|
|
91
|
-
- JSR dependency mapping now pins `@zap-studio/validation` to `0.3.2`.
|
|
92
|
-
|
|
93
|
-
## 0.4.7
|
|
98
|
+
## [0.4.7]
|
|
94
99
|
|
|
95
|
-
###
|
|
100
|
+
### Changed
|
|
96
101
|
|
|
97
102
|
- e26293e: Updated dependencies.
|
|
98
103
|
- @zap-studio/validation@0.3.2
|
|
99
104
|
|
|
100
|
-
## 0.4.6
|
|
105
|
+
## [0.4.6]
|
|
101
106
|
|
|
102
|
-
###
|
|
107
|
+
### Changed
|
|
103
108
|
|
|
104
109
|
- 5ea3d3b: Updated dependencies.
|
|
105
110
|
- @zap-studio/validation@0.3.1
|
|
106
111
|
|
|
107
|
-
## 0.4.5
|
|
112
|
+
## [0.4.5]
|
|
108
113
|
|
|
109
|
-
###
|
|
114
|
+
### Changed
|
|
110
115
|
|
|
111
116
|
- f75b984: Updated dependency `@zap-studio/validation` to `0.3.0`.
|
|
112
117
|
|
|
113
|
-
## 0.4.4
|
|
118
|
+
## [0.4.4]
|
|
114
119
|
|
|
115
120
|
### Fixed
|
|
116
121
|
|
|
@@ -119,39 +124,33 @@ Internal formatting and lint cleanup only. No public API or behavior change.
|
|
|
119
124
|
- Auto-stringify plain JSON body values even when no response schema is provided.
|
|
120
125
|
- Set `Content-Type: application/json` only when auto-stringifying and no explicit content type is set.
|
|
121
126
|
|
|
122
|
-
## 0.4.3
|
|
127
|
+
## [0.4.3]
|
|
123
128
|
|
|
124
129
|
### Changed
|
|
125
130
|
|
|
126
131
|
- e4542bb: Refined `standardValidate` typings so return types depend on `throwOnError`, and updated `@zap-studio/fetch` integration while preserving the existing boolean configuration API.
|
|
127
|
-
|
|
128
|
-
### Dependencies
|
|
129
|
-
|
|
130
132
|
- e4542bb: Updated dependency `@zap-studio/validation` to `0.2.1`.
|
|
131
133
|
|
|
132
|
-
## 0.4.2
|
|
134
|
+
## [0.4.2]
|
|
133
135
|
|
|
134
|
-
###
|
|
136
|
+
### Changed
|
|
135
137
|
|
|
136
138
|
- 2de8183: Updated dependency `@zap-studio/validation` to `0.2.0`.
|
|
137
139
|
|
|
138
|
-
## 0.4.1
|
|
140
|
+
## [0.4.1]
|
|
139
141
|
|
|
140
142
|
### Changed
|
|
141
143
|
|
|
142
144
|
- 447dbda: Switched shared Standard Schema validation utilities to `@zap-studio/validation`.
|
|
143
|
-
|
|
144
|
-
### Dependencies
|
|
145
|
-
|
|
146
145
|
- 447dbda: Updated dependency `@zap-studio/validation` to `0.1.0`.
|
|
147
146
|
|
|
148
|
-
## 0.4.0
|
|
147
|
+
## [0.4.0]
|
|
149
148
|
|
|
150
149
|
### Added
|
|
151
150
|
|
|
152
151
|
- 69057cd: Exposed fetch defaults constants and utility helpers as public exports.
|
|
153
152
|
|
|
154
|
-
## 0.3.1
|
|
153
|
+
## [0.3.1]
|
|
155
154
|
|
|
156
155
|
### Changed
|
|
157
156
|
|
|
@@ -160,31 +159,26 @@ Internal formatting and lint cleanup only. No public API or behavior change.
|
|
|
160
159
|
- `throwOnValidationError: false` returns `Promise<StandardSchemaV1.Result<TSchema>>`.
|
|
161
160
|
- This improves type safety and removes manual narrowing in default usage.
|
|
162
161
|
|
|
163
|
-
## 0.3.0
|
|
162
|
+
## [0.3.0]
|
|
164
163
|
|
|
165
164
|
### Added
|
|
166
165
|
|
|
167
166
|
- 659621c: Added `searchParams` support in `createFetch` for factory-level default query parameters.
|
|
168
167
|
- Per-request `searchParams` still override factory defaults.
|
|
169
168
|
|
|
170
|
-
## 0.2.2
|
|
169
|
+
## [0.2.2]
|
|
171
170
|
|
|
172
171
|
### Changed
|
|
173
172
|
|
|
174
173
|
- 5c3abbf: Prepared JSR publish and `isolatedDeclarations` support with explicit `$Fetch` and `ApiMethods` types.
|
|
175
174
|
|
|
176
|
-
## 0.2.1
|
|
175
|
+
## [0.2.1]
|
|
177
176
|
|
|
178
177
|
### Changed
|
|
179
178
|
|
|
180
179
|
- 82bac5c: Replaced regex-based slash trimming with more efficient string manipulation for URL normalization.
|
|
181
180
|
|
|
182
|
-
## 0.2.0
|
|
183
|
-
|
|
184
|
-
### Changed
|
|
185
|
-
|
|
186
|
-
- 78afb76: Migrated from Zod-only validation to Standard Schema v1 for broader validator compatibility.
|
|
187
|
-
- Supported libraries include Zod, Valibot, ArkType, and other Standard Schema-compliant validators.
|
|
181
|
+
## [0.2.0]
|
|
188
182
|
|
|
189
183
|
### Added
|
|
190
184
|
|
|
@@ -192,25 +186,27 @@ Internal formatting and lint cleanup only. No public API or behavior change.
|
|
|
192
186
|
- 78afb76: Added smart URL behavior so absolute URLs bypass `baseURL`.
|
|
193
187
|
- 78afb76: Added automatic JSON body serialization and `Content-Type` handling for schema-based requests.
|
|
194
188
|
|
|
195
|
-
###
|
|
189
|
+
### Changed
|
|
196
190
|
|
|
197
|
-
- 78afb76:
|
|
198
|
-
-
|
|
199
|
-
- 78afb76:
|
|
191
|
+
- 78afb76: Migrated from Zod-only validation to Standard Schema v1 for broader validator compatibility.
|
|
192
|
+
- Supported libraries include Zod, Valibot, ArkType, and other Standard Schema-compliant validators.
|
|
193
|
+
- 78afb76: **Breaking:** Standard Schema-compliant validator libraries are now required (for example Zod 3.23+, Valibot 1.0+, ArkType 2.0+).
|
|
194
|
+
- 78afb76: **Breaking:** Internal file structure was reorganized (affects deep imports).
|
|
195
|
+
- 78afb76: **Breaking:** `FetchError` constructor now requires `(message, response)`.
|
|
200
196
|
|
|
201
|
-
## 0.1.2
|
|
197
|
+
## [0.1.2]
|
|
202
198
|
|
|
203
199
|
### Changed
|
|
204
200
|
|
|
205
201
|
- 69c2b21: Renamed `safeFetch` to `$fetch` while preserving `safeFetch` compatibility for legacy usage.
|
|
206
202
|
|
|
207
|
-
## 0.1.1
|
|
203
|
+
## [0.1.1]
|
|
208
204
|
|
|
209
205
|
### Changed
|
|
210
206
|
|
|
211
207
|
- 5f1812b: Updated `files` in `package.json` to publish only required artifacts.
|
|
212
208
|
|
|
213
|
-
## 0.1.0
|
|
209
|
+
## [0.1.0]
|
|
214
210
|
|
|
215
211
|
### Added
|
|
216
212
|
|
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -2,29 +2,28 @@
|
|
|
2
2
|
|
|
3
3
|
A small fetch wrapper with [**Standard Schema**](https://standardschema.dev/schema) response validation.
|
|
4
4
|
|
|
5
|
+
Full documentation: [zapstudio.dev/fetch](https://www.zapstudio.dev/fetch)
|
|
6
|
+
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
7
9
|
```bash
|
|
8
|
-
nub add @zap-studio/fetch
|
|
9
|
-
# or
|
|
10
10
|
npm install @zap-studio/fetch
|
|
11
|
-
# or
|
|
12
|
-
pnpm add @zap-studio/fetch
|
|
13
11
|
```
|
|
14
12
|
|
|
15
|
-
|
|
13
|
+
You also need a schema library that implements [Standard Schema](https://standardschema.dev/schema), such as Zod, Valibot, or ArkType.
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
| ------------------ | ------------------------------------------------ |
|
|
19
|
-
| Node.js | 18.0.0 (ships native `fetch`) |
|
|
20
|
-
| Bun | 1.0.0 |
|
|
21
|
-
| Deno | 1.42 |
|
|
22
|
-
| Cloudflare Workers | Any current release |
|
|
23
|
-
| Browsers | Latest evergreen (Chrome, Edge, Firefox, Safari) |
|
|
15
|
+
## Features
|
|
24
16
|
|
|
25
|
-
|
|
17
|
+
- **Raw fetch mode** through `$fetch(input, options)` — behaves like native `fetch` and returns the `Response`.
|
|
18
|
+
- **Validated fetch mode** through `$fetch(input, schema, options)` — parses and validates the JSON response.
|
|
19
|
+
- **HTTP method helpers** through `api.get`, `api.post`, `api.put`, `api.patch`, and `api.delete`.
|
|
20
|
+
- **Configured clients** through `createFetch(...)` with shared `baseURL`, headers, query params, and error defaults.
|
|
21
|
+
- **JSON convenience** through the `json` option, which serializes the request body and sets `Content-Type`.
|
|
22
|
+
- **Structured errors** with `FetchError` for HTTP failures and `ValidationError` for schema failures.
|
|
23
|
+
- **Validator-agnostic** — works with any library that implements Standard Schema.
|
|
24
|
+
- **Tree-shakeable** — every export is a standalone function with no shared internal state; unused exports are dropped by any modern bundler.
|
|
26
25
|
|
|
27
|
-
## Quick
|
|
26
|
+
## Quick Start
|
|
28
27
|
|
|
29
28
|
```ts
|
|
30
29
|
import { api } from "@zap-studio/fetch";
|
|
@@ -33,45 +32,17 @@ import { z } from "zod";
|
|
|
33
32
|
const UserSchema = z.object({
|
|
34
33
|
id: z.number(),
|
|
35
34
|
name: z.string(),
|
|
36
|
-
email: z.
|
|
35
|
+
email: z.email(),
|
|
37
36
|
});
|
|
38
37
|
|
|
39
38
|
const user = await api.get("https://api.example.com/users/1", UserSchema);
|
|
40
39
|
|
|
41
|
-
console.log(user.name);
|
|
40
|
+
console.log(user.name); // typed as string, validated at runtime
|
|
42
41
|
```
|
|
43
42
|
|
|
44
|
-
##
|
|
45
|
-
|
|
46
|
-
Without response validation, fetch code often needs unchecked assertions:
|
|
47
|
-
|
|
48
|
-
```ts
|
|
49
|
-
type User = {
|
|
50
|
-
id: number;
|
|
51
|
-
name: string;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const response = await fetch("/api/users/1");
|
|
55
|
-
const data = await response.json();
|
|
56
|
-
|
|
57
|
-
const user = data as User;
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
## After
|
|
61
|
-
|
|
62
|
-
With `@zap-studio/fetch`, the response is validated before it is returned:
|
|
63
|
-
|
|
64
|
-
```ts
|
|
65
|
-
import { api } from "@zap-studio/fetch";
|
|
66
|
-
|
|
67
|
-
const user = await api.get("/api/users/1", UserSchema);
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
The returned value is inferred from the schema.
|
|
71
|
-
|
|
72
|
-
## Raw Fetch Usage
|
|
43
|
+
## Raw Fetch Mode
|
|
73
44
|
|
|
74
|
-
|
|
45
|
+
Behaves like native `fetch` and returns the `Response`.
|
|
75
46
|
|
|
76
47
|
```ts
|
|
77
48
|
import { $fetch } from "@zap-studio/fetch";
|
|
@@ -80,21 +51,9 @@ const response = await $fetch("/api/users/1");
|
|
|
80
51
|
const user = await response.json();
|
|
81
52
|
```
|
|
82
53
|
|
|
83
|
-
|
|
54
|
+
## Validated Fetch Mode
|
|
84
55
|
|
|
85
|
-
|
|
86
|
-
const response = await $fetch("/api/users", {
|
|
87
|
-
method: "POST",
|
|
88
|
-
body: JSON.stringify({ name: "Ada" }),
|
|
89
|
-
headers: {
|
|
90
|
-
"Content-Type": "application/json",
|
|
91
|
-
},
|
|
92
|
-
});
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
## Validated Fetch Usage
|
|
96
|
-
|
|
97
|
-
Pass a Standard Schema as the second argument to validate the JSON response.
|
|
56
|
+
Parses and validates the JSON response.
|
|
98
57
|
|
|
99
58
|
```ts
|
|
100
59
|
import { $fetch } from "@zap-studio/fetch";
|
|
@@ -102,213 +61,79 @@ import { $fetch } from "@zap-studio/fetch";
|
|
|
102
61
|
const user = await $fetch("/api/users/1", UserSchema);
|
|
103
62
|
```
|
|
104
63
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
When validation fails, a `ValidationError` is thrown by default.
|
|
108
|
-
|
|
109
|
-
## API Method Helpers
|
|
64
|
+
## HTTP Method Helpers
|
|
110
65
|
|
|
111
|
-
|
|
66
|
+
`api.get`, `api.post`, `api.put`, `api.patch`, and `api.delete`.
|
|
112
67
|
|
|
113
68
|
```ts
|
|
114
69
|
import { api } from "@zap-studio/fetch";
|
|
115
70
|
|
|
116
|
-
const user = await api.get("/api/users/1", UserSchema);
|
|
117
|
-
|
|
118
71
|
const created = await api.post("/api/users", UserSchema, {
|
|
119
|
-
json: {
|
|
120
|
-
name: "Ada",
|
|
121
|
-
email: "ada@example.com",
|
|
122
|
-
},
|
|
72
|
+
json: { name: "Ada", email: "ada@example.com" },
|
|
123
73
|
});
|
|
124
74
|
```
|
|
125
75
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
These helpers set the HTTP method for you. For raw responses without validation, use `$fetch`.
|
|
129
|
-
|
|
130
|
-
## JSON Request Bodies
|
|
76
|
+
## Configured Clients
|
|
131
77
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
```ts
|
|
135
|
-
await api.post("/api/users", UserSchema, {
|
|
136
|
-
json: {
|
|
137
|
-
name: "Ada",
|
|
138
|
-
},
|
|
139
|
-
});
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
The schema argument still validates the response. Validate request bodies separately before passing them to `json` if your application needs outgoing payload validation.
|
|
143
|
-
|
|
144
|
-
This sets:
|
|
145
|
-
|
|
146
|
-
- `body` to `JSON.stringify(json)`
|
|
147
|
-
- `Content-Type` to `application/json` when no content type was already provided
|
|
148
|
-
|
|
149
|
-
Use native `body` for standard fetch behavior.
|
|
150
|
-
|
|
151
|
-
```ts
|
|
152
|
-
await $fetch("/api/upload", {
|
|
153
|
-
method: "POST",
|
|
154
|
-
body: formData,
|
|
155
|
-
});
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
`body` and `json` are mutually exclusive.
|
|
159
|
-
|
|
160
|
-
## Query Params
|
|
161
|
-
|
|
162
|
-
Use `searchParams` for per-request query params.
|
|
163
|
-
|
|
164
|
-
```ts
|
|
165
|
-
await api.get("/api/users", UserListSchema, {
|
|
166
|
-
searchParams: {
|
|
167
|
-
page: "1",
|
|
168
|
-
limit: "20",
|
|
169
|
-
},
|
|
170
|
-
});
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
`searchParams` accepts the same input shapes as `new URLSearchParams(...)`.
|
|
174
|
-
|
|
175
|
-
```ts
|
|
176
|
-
await api.get("/api/users", UserListSchema, {
|
|
177
|
-
searchParams: new URLSearchParams({ q: "ada" }),
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
await api.get("/api/users", UserListSchema, {
|
|
181
|
-
searchParams: "q=ada&page=1",
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
await api.get("/api/users", UserListSchema, {
|
|
185
|
-
searchParams: [["q", "ada"]],
|
|
186
|
-
});
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
When defaults, URL query params, and request params overlap, later values win:
|
|
190
|
-
|
|
191
|
-
1. `createFetch({ searchParams })`
|
|
192
|
-
2. query params already present in the URL
|
|
193
|
-
3. per-request `searchParams`
|
|
194
|
-
|
|
195
|
-
## Creating a Client
|
|
196
|
-
|
|
197
|
-
Use `createFetch` to configure shared defaults.
|
|
78
|
+
Shared `baseURL`, headers, query params, and error defaults.
|
|
198
79
|
|
|
199
80
|
```ts
|
|
200
81
|
import { createFetch } from "@zap-studio/fetch";
|
|
201
82
|
|
|
202
|
-
const {
|
|
83
|
+
const { api } = createFetch({
|
|
203
84
|
baseURL: "https://api.example.com",
|
|
204
|
-
headers: {
|
|
205
|
-
Authorization: `Bearer ${token}`,
|
|
206
|
-
},
|
|
207
|
-
searchParams: {
|
|
208
|
-
locale: "en",
|
|
209
|
-
},
|
|
85
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
210
86
|
});
|
|
211
87
|
|
|
212
88
|
const user = await api.get("/users/1", UserSchema);
|
|
213
|
-
const response = await $fetch("/health");
|
|
214
89
|
```
|
|
215
90
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
| Option | Description |
|
|
219
|
-
| ------------------------ | ----------------------------------------------- |
|
|
220
|
-
| `baseURL` | Base URL for relative request URLs |
|
|
221
|
-
| `headers` | Headers applied to every request |
|
|
222
|
-
| `searchParams` | Query params applied to every request |
|
|
223
|
-
| `throwOnFetchError` | Throw `FetchError` for non-ok responses |
|
|
224
|
-
| `throwOnValidationError` | Throw `ValidationError` for validation failures |
|
|
91
|
+
## JSON Convenience
|
|
225
92
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
Set `throwOnValidationError: false` to receive the raw Standard Schema result.
|
|
93
|
+
Serializes the request body and sets `Content-Type`.
|
|
229
94
|
|
|
230
95
|
```ts
|
|
231
|
-
|
|
232
|
-
|
|
96
|
+
await api.post("/api/users", UserSchema, {
|
|
97
|
+
json: { name: "Ada" }, // mutually exclusive with `body`
|
|
233
98
|
});
|
|
234
|
-
|
|
235
|
-
if (result.issues) {
|
|
236
|
-
console.error("Validation failed:", result.issues);
|
|
237
|
-
} else {
|
|
238
|
-
console.log("Validation passed:", result.value);
|
|
239
|
-
}
|
|
240
99
|
```
|
|
241
100
|
|
|
242
|
-
##
|
|
101
|
+
## Structured Errors
|
|
243
102
|
|
|
244
|
-
|
|
103
|
+
`FetchError` for HTTP failures and `ValidationError` for schema failures.
|
|
245
104
|
|
|
246
105
|
```ts
|
|
247
106
|
import { FetchError } from "@zap-studio/fetch";
|
|
107
|
+
import { ValidationError } from "@zap-studio/validation";
|
|
248
108
|
|
|
249
109
|
try {
|
|
250
|
-
await api.get("/api/users/
|
|
110
|
+
await api.get("/api/users/1", UserSchema);
|
|
251
111
|
} catch (error) {
|
|
252
|
-
if (error instanceof FetchError)
|
|
253
|
-
|
|
254
|
-
console.error(error.response);
|
|
255
|
-
}
|
|
112
|
+
if (error instanceof FetchError) console.error(error.status);
|
|
113
|
+
if (error instanceof ValidationError) console.error(error.issues);
|
|
256
114
|
}
|
|
257
115
|
```
|
|
258
116
|
|
|
259
|
-
|
|
117
|
+
## Validator-Agnostic
|
|
260
118
|
|
|
261
|
-
|
|
262
|
-
const response = await $fetch("/api/users/404", {
|
|
263
|
-
throwOnFetchError: false,
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
console.log(response.status);
|
|
267
|
-
```
|
|
268
|
-
|
|
269
|
-
## Request Objects
|
|
270
|
-
|
|
271
|
-
The first argument (`input`) uses the same type as global `fetch`.
|
|
119
|
+
Works with any library that implements Standard Schema.
|
|
272
120
|
|
|
273
121
|
```ts
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
Authorization: `Bearer ${token}`,
|
|
277
|
-
},
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
const user = await $fetch(request, UserSchema);
|
|
122
|
+
// UserSchema can come from Zod, Valibot, ArkType, or any Standard Schema-compatible library
|
|
123
|
+
import type { StandardSchemaV1 } from "@zap-studio/validation";
|
|
281
124
|
```
|
|
282
125
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
## Standard Schema
|
|
286
|
-
|
|
287
|
-
`@zap-studio/fetch` validates responses through `@zap-studio/validation`, so it works with any validation library that implements Standard Schema.
|
|
288
|
-
|
|
289
|
-
Examples include:
|
|
290
|
-
|
|
291
|
-
- [Zod](https://zod.dev/)
|
|
292
|
-
- [Valibot](https://valibot.dev/)
|
|
293
|
-
- [ArkType](https://arktype.io/)
|
|
294
|
-
|
|
295
|
-
You can also import Standard Schema types from `@zap-studio/validation` when needed.
|
|
126
|
+
## Runtime Support
|
|
296
127
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
128
|
+
| Runtime | Minimum version |
|
|
129
|
+
| ------------------ | ------------------------------------------------ |
|
|
130
|
+
| Node.js | 18.0.0 (ships native `fetch`) |
|
|
131
|
+
| Bun | 1.0.0 |
|
|
132
|
+
| Deno | 1.42 |
|
|
133
|
+
| Cloudflare Workers | Any current release |
|
|
134
|
+
| Browsers | Latest evergreen (Chrome, Edge, Firefox, Safari) |
|
|
300
135
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
| API | Use when |
|
|
304
|
-
| ------------- | ------------------------------------------------- |
|
|
305
|
-
| `$fetch` | You want raw fetch behavior or a custom method |
|
|
306
|
-
| `api.get` | You want a validated GET request |
|
|
307
|
-
| `api.post` | You want a validated POST request |
|
|
308
|
-
| `api.put` | You want a validated PUT request |
|
|
309
|
-
| `api.patch` | You want a validated PATCH request |
|
|
310
|
-
| `api.delete` | You want a validated DELETE request |
|
|
311
|
-
| `createFetch` | You want shared defaults like base URL or headers |
|
|
136
|
+
The package relies on the global `fetch` API and ships standard ESM only. Deno 1.42 is the first release that can install packages from JSR (`deno add jsr:@zap-studio/fetch`).
|
|
312
137
|
|
|
313
138
|
## License
|
|
314
139
|
|