@zimic/fetch 0.1.0 → 0.1.1
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 +119 -92
- package/package.json +5 -6
package/README.md
CHANGED
|
@@ -3,14 +3,16 @@
|
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
<h1 align="center">
|
|
6
|
-
|
|
6
|
+
@zimic/fetch
|
|
7
7
|
</h1>
|
|
8
8
|
|
|
9
9
|
<p align="center">
|
|
10
|
-
TypeScript-first
|
|
10
|
+
TypeScript-first fetch-like API client
|
|
11
11
|
</p>
|
|
12
12
|
|
|
13
13
|
<p align="center">
|
|
14
|
+
<a href="https://www.npmjs.com/package/@zimic/fetch">npm</a>
|
|
15
|
+
<span> • </span>
|
|
14
16
|
<a href="https://github.com/zimicjs/zimic/wiki">Docs</a>
|
|
15
17
|
<span> • </span>
|
|
16
18
|
<a href="#examples">Examples</a>
|
|
@@ -27,68 +29,31 @@
|
|
|
27
29
|
[](https://github.com/zimicjs/zimic/blob/canary/LICENSE.md)
|
|
28
30
|
[](https://github.com/zimicjs/zimic)
|
|
29
31
|
|
|
30
|
-
[](https://www.npmjs.com/package/@zimic/http)
|
|
31
|
-
[](https://bundlephobia.com/package/@zimic/http)<br />
|
|
32
32
|
[](https://www.npmjs.com/package/@zimic/fetch)
|
|
33
33
|
[](https://bundlephobia.com/package/@zimic/fetch)<br />
|
|
34
|
-
[](https://www.npmjs.com/package/@zimic/interceptor)
|
|
35
|
-
[](https://bundlephobia.com/package/@zimic/interceptor)
|
|
36
34
|
|
|
37
35
|
</div>
|
|
38
36
|
|
|
39
37
|
---
|
|
40
38
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
- [
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
- [`@zimic/interceptor`](#zimicinterceptor)
|
|
39
|
+
- [Features](#features)
|
|
40
|
+
- [Getting started](#getting-started)
|
|
41
|
+
- [Installation](#installation)
|
|
42
|
+
- [Basic usage](#basic-usage)
|
|
43
|
+
- [Documentation](#documentation)
|
|
47
44
|
- [Examples](#examples)
|
|
48
45
|
- [Changelog](#changelog)
|
|
49
46
|
- [Contributing](#contributing)
|
|
50
47
|
|
|
51
48
|
---
|
|
52
49
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
## Libraries
|
|
56
|
-
|
|
57
|
-
### `@zimic/http`
|
|
58
|
-
|
|
59
|
-
> [!NOTE]
|
|
60
|
-
>
|
|
61
|
-
> :seedling: This library is in **beta**.
|
|
62
|
-
|
|
63
|
-
[`@zimic/http`](../../packages/zimic-http) is a collection of type-safe utilities to handle HTTP requests and responses,
|
|
64
|
-
including headers, search params, and form data.
|
|
65
|
-
|
|
66
|
-
- :star: **HTTP schemas and typegen**: Declare the structure of your HTTP endpoints as a TypeScript
|
|
67
|
-
[schema](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http‐schemas) and use it to type your HTTP requests and
|
|
68
|
-
responses. If you have an [OpenAPI v3](https://swagger.io/specification) declaration,
|
|
69
|
-
[`zimic-http typegen`](https://github.com/zimicjs/zimic/wiki/cli‐zimic‐typegen) can automatically generate the types
|
|
70
|
-
of your schema.
|
|
71
|
-
- :pushpin: **Type-safe native APIs**: Declare type-safe
|
|
72
|
-
[`Headers`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpheaders),
|
|
73
|
-
[`URLSearchParams`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpsearchparams), and
|
|
74
|
-
[`FormData`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpformdata) objects, fully compatible with their
|
|
75
|
-
native counterparts.
|
|
76
|
-
|
|
77
|
-
**Learn more**:
|
|
78
|
-
|
|
79
|
-
- [`@zimic/http` - Getting started](https://github.com/zimicjs/zimic/wiki/getting‐started‐http)
|
|
80
|
-
- [`@zimic/http` - API reference](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http)
|
|
81
|
-
- `@zimic/http` - CLI reference
|
|
82
|
-
- [`zimic-http typegen`](https://github.com/zimicjs/zimic/wiki/cli‐zimic‐typegen)
|
|
83
|
-
|
|
84
|
-
### `@zimic/fetch`
|
|
50
|
+
`@zimic/fetch` is a minimal (1 kB minified and gzipped), zero-dependency, and type-safe `fetch`-like API client.
|
|
85
51
|
|
|
86
52
|
> [!WARNING]
|
|
87
53
|
>
|
|
88
54
|
> :construction: This library is **experimental**.
|
|
89
55
|
|
|
90
|
-
|
|
91
|
-
`fetch`-like API client.
|
|
56
|
+
## Features
|
|
92
57
|
|
|
93
58
|
- :sparkles: **Type-safe `fetch`**: Create a type-safe
|
|
94
59
|
[`fetch`-like](https://developer.mozilla.org/docs/Web/API/Fetch_API) API client. Use your
|
|
@@ -101,55 +66,117 @@ including headers, search params, and form data.
|
|
|
101
66
|
[`onRequest`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchonrequest) and
|
|
102
67
|
[`onResponse`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchonresponse) listeners.
|
|
103
68
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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)
|
|
149
175
|
|
|
150
176
|
## Examples
|
|
151
177
|
|
|
152
|
-
Visit our [examples](../../examples/README.md) to see how to use Zimic with popular frameworks, libraries, and use
|
|
178
|
+
Visit our [examples](../../examples/README.md) to see how to use Zimic with popular frameworks, libraries, and use
|
|
179
|
+
cases.
|
|
153
180
|
|
|
154
181
|
## Changelog
|
|
155
182
|
|
package/package.json
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"api",
|
|
14
14
|
"static"
|
|
15
15
|
],
|
|
16
|
-
"version": "0.1.
|
|
16
|
+
"version": "0.1.1",
|
|
17
17
|
"repository": {
|
|
18
18
|
"type": "git",
|
|
19
19
|
"url": "https://github.com/zimicjs/zimic.git",
|
|
@@ -65,11 +65,11 @@
|
|
|
65
65
|
"tsup": "^8.4.0",
|
|
66
66
|
"typescript": "^5.8.2",
|
|
67
67
|
"vitest": "^3.0.7",
|
|
68
|
+
"@zimic/interceptor": "0.14.0",
|
|
68
69
|
"@zimic/eslint-config-node": "0.0.0",
|
|
69
|
-
"@zimic/interceptor": "0.14.0-canary.26",
|
|
70
70
|
"@zimic/lint-staged-config": "0.0.0",
|
|
71
|
-
"@zimic/
|
|
72
|
-
"@zimic/
|
|
71
|
+
"@zimic/tsconfig": "0.0.0",
|
|
72
|
+
"@zimic/utils": "0.0.0"
|
|
73
73
|
},
|
|
74
74
|
"peerDependencies": {
|
|
75
75
|
"@zimic/http": "^0.1.0 || ^0.1.0-canary.0",
|
|
@@ -87,7 +87,6 @@
|
|
|
87
87
|
"test": "dotenv -v NODE_ENV=test -- vitest",
|
|
88
88
|
"test:turbo": "dotenv -v CI=true -- pnpm run test run --coverage",
|
|
89
89
|
"types:check": "tsc --noEmit",
|
|
90
|
-
"deps:init": "playwright install chromium"
|
|
91
|
-
"prepublish:patch-relative-paths": "sed -E -i 's/\\]\\(\\.\\/([^\\)]+)\\)/](..\\/..\\/\\1)/g;s/\"\\.\\/([^\"]+)\"/\"..\\/..\\/\\1\"/g'"
|
|
90
|
+
"deps:init": "playwright install chromium"
|
|
92
91
|
}
|
|
93
92
|
}
|