@zimic/fetch 0.4.0-canary.2 → 0.4.1-canary.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/README.md +24 -146
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -13,9 +13,9 @@
|
|
|
13
13
|
<p align="center">
|
|
14
14
|
<a href="https://www.npmjs.com/package/@zimic/fetch">npm</a>
|
|
15
15
|
<span> • </span>
|
|
16
|
-
<a href="https://
|
|
16
|
+
<a href="https://zimic.dev">Docs</a>
|
|
17
17
|
<span> • </span>
|
|
18
|
-
<a href="
|
|
18
|
+
<a href="https://zimic.dev/docs/examples">Examples</a>
|
|
19
19
|
<span> • </span>
|
|
20
20
|
<a href="https://github.com/zimicjs/zimic/issues">Issues</a>
|
|
21
21
|
<span> • </span>
|
|
@@ -36,152 +36,30 @@
|
|
|
36
36
|
|
|
37
37
|
---
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
- [Getting started](#getting-started)
|
|
41
|
-
- [Installation](#installation)
|
|
42
|
-
- [Basic usage](#basic-usage)
|
|
43
|
-
- [Documentation](#documentation)
|
|
44
|
-
- [Examples](#examples)
|
|
45
|
-
- [Changelog](#changelog)
|
|
46
|
-
- [Contributing](#contributing)
|
|
39
|
+
`@zimic/fetch` is a minimal (~2 kB minified and gzipped) and type-safe `fetch`-like API client.
|
|
47
40
|
|
|
48
|
-
|
|
41
|
+
Status: :seedling: **Beta**
|
|
42
|
+
|
|
43
|
+
## Features
|
|
49
44
|
|
|
50
|
-
|
|
45
|
+
- :zap: **Type-safe `fetch`**
|
|
51
46
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
47
|
+
Use your [`@zimic/http` schema](https://zimic.dev/docs/http/guides/schemas) to create a type-safe
|
|
48
|
+
[`fetch`-like](https://developer.mozilla.org/docs/Web/API/Fetch_API) API client and have your requests and responses
|
|
49
|
+
fully typed by default.
|
|
55
50
|
|
|
56
|
-
|
|
51
|
+
- :sparkles: **Zero dependencies**
|
|
52
|
+
|
|
53
|
+
`@zimic/fetch` has no external dependencies, making it a lightweight and fast alternative to other HTTP clients.
|
|
54
|
+
|
|
55
|
+
- :muscle: **Developer experience**
|
|
56
|
+
|
|
57
|
+
Define default options to apply to your requests, such as a base URL, headers, search parameters, and more. Inspect
|
|
58
|
+
and modify requests and responses using [`onRequest`](https://zimic.dev/docs/fetch/api/fetch#onrequest) and
|
|
59
|
+
[`onResponse`](https://zimic.dev/docs/fetch/api/fetch#onresponse) listeners.
|
|
60
|
+
|
|
61
|
+
**Learn more**:
|
|
57
62
|
|
|
58
|
-
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
responses fully typed by default.
|
|
62
|
-
- :muscle: **Developer experience**: `@zimic/fetch` seeks to be as compatible with the
|
|
63
|
-
[native Fetch API](https://developer.mozilla.org/docs/Web/API/Fetch_API) as possible, while providing an ergonomic
|
|
64
|
-
interface to improve type safety. Define default options to apply to your requests, such as a base URL, headers,
|
|
65
|
-
search parameters, and more. Inspect and modify requests and responses using
|
|
66
|
-
[`onRequest`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchonrequest) and
|
|
67
|
-
[`onResponse`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchonresponse) listeners.
|
|
68
|
-
|
|
69
|
-
## Getting started
|
|
70
|
-
|
|
71
|
-
Check our [getting started guide](https://github.com/zimicjs/zimic/wiki/getting‐started‐fetch).
|
|
72
|
-
|
|
73
|
-
### Installation
|
|
74
|
-
|
|
75
|
-
| Manager | Command |
|
|
76
|
-
| :-----: | --------------------------------------------- |
|
|
77
|
-
| npm | `npm install @zimic/http @zimic/fetch --save` |
|
|
78
|
-
| yarn | `yarn add @zimic/http @zimic/fetch` |
|
|
79
|
-
| pnpm | `pnpm add @zimic/http @zimic/fetch` |
|
|
80
|
-
|
|
81
|
-
## Basic usage
|
|
82
|
-
|
|
83
|
-
1. Declare your HTTP schema using [`@zimic/http`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http):
|
|
84
|
-
|
|
85
|
-
```ts
|
|
86
|
-
import { type HttpSchema } from '@zimic/http';
|
|
87
|
-
|
|
88
|
-
interface User {
|
|
89
|
-
username: string;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
interface RequestError {
|
|
93
|
-
code: string;
|
|
94
|
-
message: string;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
type Schema = HttpSchema<{
|
|
98
|
-
'/users': {
|
|
99
|
-
POST: {
|
|
100
|
-
request: { body: User };
|
|
101
|
-
response: {
|
|
102
|
-
201: { body: User };
|
|
103
|
-
400: { body: RequestError };
|
|
104
|
-
409: { body: RequestError };
|
|
105
|
-
500: { body: RequestError };
|
|
106
|
-
};
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
GET: {
|
|
110
|
-
request: {
|
|
111
|
-
searchParams: {
|
|
112
|
-
query?: string;
|
|
113
|
-
limit?: number;
|
|
114
|
-
};
|
|
115
|
-
};
|
|
116
|
-
response: {
|
|
117
|
-
200: { body: User[] };
|
|
118
|
-
404: { body: RequestError };
|
|
119
|
-
500: { body: RequestError };
|
|
120
|
-
};
|
|
121
|
-
};
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
'/users/:userId': {
|
|
125
|
-
PATCH: {
|
|
126
|
-
request: {
|
|
127
|
-
headers: { authorization: string };
|
|
128
|
-
body: Partial<User>;
|
|
129
|
-
};
|
|
130
|
-
response: {
|
|
131
|
-
204: {};
|
|
132
|
-
400: { body: RequestError };
|
|
133
|
-
500: { body: RequestError };
|
|
134
|
-
};
|
|
135
|
-
};
|
|
136
|
-
};
|
|
137
|
-
}>;
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
2. Create your [fetch client](https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#createfetch):
|
|
141
|
-
|
|
142
|
-
```ts
|
|
143
|
-
import { createFetch } from '@zimic/fetch';
|
|
144
|
-
|
|
145
|
-
const fetch = createFetch<Schema>({
|
|
146
|
-
baseURL: 'http://localhost:3000',
|
|
147
|
-
});
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
3. Enjoy requests and responses typed by default!
|
|
151
|
-
|
|
152
|
-
```ts
|
|
153
|
-
const response = await fetch('/users', {
|
|
154
|
-
method: 'GET',
|
|
155
|
-
searchParams: { query: 'u', limit: 10 },
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
if (response.status === 404) {
|
|
159
|
-
return null; // Not found
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (!response.ok) {
|
|
163
|
-
throw response.error;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const users = await response.json();
|
|
167
|
-
return users; // User[]
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
## Documentation
|
|
171
|
-
|
|
172
|
-
- [Introduction](https://github.com/zimicjs/zimic/wiki)
|
|
173
|
-
- [Getting started](https://github.com/zimicjs/zimic/wiki/getting‐started‐fetch)
|
|
174
|
-
- [API reference](https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch)
|
|
175
|
-
|
|
176
|
-
## Examples
|
|
177
|
-
|
|
178
|
-
Visit our [examples](../../examples/README.md) to see how to use Zimic with popular frameworks, libraries, and use
|
|
179
|
-
cases.
|
|
180
|
-
|
|
181
|
-
## Changelog
|
|
182
|
-
|
|
183
|
-
The changelog is available on our [GitHub Releases](https://github.com/zimicjs/zimic/releases) page.
|
|
184
|
-
|
|
185
|
-
## Contributing
|
|
186
|
-
|
|
187
|
-
Interested in contributing to Zimic? Check out our [contributing guide](../../CONTRIBUTING.md) to get started!
|
|
63
|
+
- [`@zimic/fetch` - Getting started](https://zimic.dev/docs/fetch/getting-started)
|
|
64
|
+
- [`@zimic/fetch` - Guides](https://zimic.dev/docs/fetch/guides)
|
|
65
|
+
- [`@zimic/fetch` - API](https://zimic.dev/docs/fetch/api)
|
package/package.json
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"api",
|
|
14
14
|
"static"
|
|
15
15
|
],
|
|
16
|
-
"version": "0.4.
|
|
16
|
+
"version": "0.4.1-canary.0",
|
|
17
17
|
"repository": {
|
|
18
18
|
"type": "git",
|
|
19
19
|
"url": "https://github.com/zimicjs/zimic.git",
|
|
@@ -67,10 +67,10 @@
|
|
|
67
67
|
"typescript": "^5.8.3",
|
|
68
68
|
"vitest": "^3.1.2",
|
|
69
69
|
"@zimic/eslint-config-node": "0.0.0",
|
|
70
|
-
"@zimic/
|
|
71
|
-
"@zimic/
|
|
70
|
+
"@zimic/lint-staged-config": "0.0.0",
|
|
71
|
+
"@zimic/interceptor": "0.19.1-canary.0",
|
|
72
72
|
"@zimic/tsconfig": "0.0.0",
|
|
73
|
-
"@zimic/
|
|
73
|
+
"@zimic/utils": "0.0.0"
|
|
74
74
|
},
|
|
75
75
|
"peerDependencies": {
|
|
76
76
|
"typescript": ">=5.0.0",
|
|
@@ -92,6 +92,6 @@
|
|
|
92
92
|
"test": "dotenv -v NODE_ENV=test -- vitest",
|
|
93
93
|
"test:turbo": "dotenv -v CI=true -- pnpm run test run --coverage",
|
|
94
94
|
"types:check": "tsc --noEmit",
|
|
95
|
-
"deps:
|
|
95
|
+
"deps:prepare": "playwright install chromium"
|
|
96
96
|
}
|
|
97
97
|
}
|