@zimic/fetch 0.1.0-canary.9 → 0.1.1-canary.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/LICENSE.md +1 -1
- package/README.md +136 -175
- package/dist/index.d.ts +871 -44
- package/dist/index.js +77 -42
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +78 -43
- package/dist/index.mjs.map +1 -1
- package/package.json +12 -12
- package/src/client/FetchClient.ts +113 -66
- package/src/client/errors/FetchResponseError.ts +47 -6
- package/src/client/factory.ts +56 -5
- package/src/client/types/json.ts +7 -0
- package/src/client/types/public.ts +587 -51
- package/src/client/types/requests.ts +257 -49
- package/src/index.ts +5 -9
- package/src/types/requests.ts +1 -2
- package/src/utils/environment.ts +3 -0
- package/src/utils/files.ts +4 -19
- package/src/types/strings.d.ts +0 -9
- package/src/types/utils.ts +0 -11
- package/src/utils/imports.ts +0 -14
- package/src/utils/urls.ts +0 -43
package/LICENSE.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2023
|
|
3
|
+
Copyright (c) 2023-Present Zimic Team
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
6
6
|
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
package/README.md
CHANGED
|
@@ -3,15 +3,15 @@
|
|
|
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/
|
|
14
|
+
<a href="https://www.npmjs.com/package/@zimic/fetch">npm</a>
|
|
15
15
|
<span> • </span>
|
|
16
16
|
<a href="https://github.com/zimicjs/zimic/wiki">Docs</a>
|
|
17
17
|
<span> • </span>
|
|
@@ -26,196 +26,157 @@
|
|
|
26
26
|
|
|
27
27
|
[](https://github.com/zimicjs/zimic/actions/workflows/ci.yaml)
|
|
28
28
|
[](https://github.com/zimicjs/zimic/actions)
|
|
29
|
-
[](https://github.com/zimicjs/zimic/blob/canary/LICENSE.md)
|
|
30
|
-
[](https://github.com/zimicjs/zimic/blob/canary/LICENSE.md)
|
|
30
|
+
[](https://github.com/zimicjs/zimic)
|
|
31
|
+
|
|
32
|
+
[](https://www.npmjs.com/package/@zimic/fetch)
|
|
33
|
+
[](https://bundlephobia.com/package/@zimic/fetch)<br />
|
|
32
34
|
|
|
33
35
|
</div>
|
|
34
36
|
|
|
35
37
|
---
|
|
36
38
|
|
|
37
|
-
|
|
38
|
-
[
|
|
39
|
+
- [Features](#features)
|
|
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)
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
`@zimic/fetch` is a minimal (1 kB minified and gzipped), zero-dependency, and type-safe `fetch`-like API client.
|
|
51
|
+
|
|
52
|
+
> [!WARNING]
|
|
53
|
+
>
|
|
54
|
+
> :construction: This library is **experimental**.
|
|
39
55
|
|
|
40
56
|
## Features
|
|
41
57
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
[
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
[
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
58
|
+
- :sparkles: **Type-safe `fetch`**: Create a type-safe
|
|
59
|
+
[`fetch`-like](https://developer.mozilla.org/docs/Web/API/Fetch_API) API client. Use your
|
|
60
|
+
[`@zimic/http` schema](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http‐schemas) and have your requests and
|
|
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
|
+
};
|
|
90
121
|
};
|
|
91
122
|
};
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
};
|
|
106
|
-
response: {
|
|
107
|
-
204: {};
|
|
108
|
-
400: { body: RequestError };
|
|
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
|
+
};
|
|
109
136
|
};
|
|
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
|
-
afterAll(async () => {
|
|
143
|
-
// 4.4. Stop intercepting requests:
|
|
144
|
-
// https://bit.ly/zimic-interceptor-http#http-interceptorstop
|
|
145
|
-
await myInterceptor.stop();
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
// Enjoy mocking!
|
|
149
|
-
test('example', async () => {
|
|
150
|
-
const users: User[] = [{ username: 'my-user' }];
|
|
151
|
-
|
|
152
|
-
// 5. Declare your mocks:
|
|
153
|
-
// https://bit.ly/zimic-interceptor-http#http-interceptormethodpath
|
|
154
|
-
const myHandler = myInterceptor
|
|
155
|
-
.get('/users')
|
|
156
|
-
// 5.1. Use restrictions to make declarative assertions and narrow down your mocks:
|
|
157
|
-
// https://bit.ly/zimic-interceptor-http#http-handlerwithrestriction
|
|
158
|
-
.with({
|
|
159
|
-
headers: { authorization: 'Bearer my-token' },
|
|
160
|
-
searchParams: { username: 'my' },
|
|
161
|
-
})
|
|
162
|
-
// 5.2. Respond with your mock data:
|
|
163
|
-
// https://bit.ly/zimic-interceptor-http#http-handlerresponddeclaration
|
|
164
|
-
.respond({
|
|
165
|
-
status: 200,
|
|
166
|
-
body: users,
|
|
167
|
-
})
|
|
168
|
-
// 5.3. Define how many requests you expect your application to make:
|
|
169
|
-
// https://bit.ly/zimic-interceptor-http#http-handlertimes
|
|
170
|
-
.times(1);
|
|
171
|
-
|
|
172
|
-
// 6. Run your application and make requests:
|
|
173
|
-
// ...
|
|
174
|
-
|
|
175
|
-
// 7. Check the requests you expect:
|
|
176
|
-
// https://bit.ly/zimic-interceptor-http#http-handlerrequests
|
|
177
|
-
//
|
|
178
|
-
// NOTE: The code below checks the requests manually. This is optional in this
|
|
179
|
-
// example because the `with` and `times` calls act as a declarative validation,
|
|
180
|
-
// meaning that exactly one request is expected with specific data. If fewer or
|
|
181
|
-
// more requests are received, the test will fail when `myInterceptor.checkTimes()`
|
|
182
|
-
// is called in the `afterEach` hook.
|
|
183
|
-
const requests = myHandler.requests();
|
|
184
|
-
expect(requests).toHaveLength(1);
|
|
185
|
-
|
|
186
|
-
expect(requests[0].headers.get('authorization')).toBe('Bearer my-token');
|
|
187
|
-
|
|
188
|
-
expect(requests[0].searchParams.size).toBe(1);
|
|
189
|
-
expect(requests[0].searchParams.get('username')).toBe('my');
|
|
190
|
-
|
|
191
|
-
expect(requests[0].body).toBe(null);
|
|
192
|
-
});
|
|
193
|
-
```
|
|
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
|
+
```
|
|
194
169
|
|
|
195
170
|
## Documentation
|
|
196
171
|
|
|
197
172
|
- [Introduction](https://github.com/zimicjs/zimic/wiki)
|
|
198
|
-
- [Getting started](https://github.com/zimicjs/zimic/wiki/getting‐started)
|
|
199
|
-
- [API reference](https://github.com/zimicjs/zimic/wiki/api‐zimic)
|
|
200
|
-
- [CLI reference](https://github.com/zimicjs/zimic/wiki/cli‐zimic)
|
|
201
|
-
- Guides
|
|
202
|
-
- [Testing](https://github.com/zimicjs/zimic/wiki/guides‐testing)
|
|
203
|
-
|
|
204
|
-
> [!NOTE]
|
|
205
|
-
>
|
|
206
|
-
> Zimic has gone a long way in v0, but we're not yet v1!
|
|
207
|
-
>
|
|
208
|
-
> Reviews and improvements to the public API are possible, so breaking changes may **_exceptionally_** land without a
|
|
209
|
-
> major release during v0. Despite of that, we do not expect big mental model shifts. Usually, migrating to a new Zimic
|
|
210
|
-
> release requires minimal to no refactoring. During v0, we will follow these guidelines:
|
|
211
|
-
>
|
|
212
|
-
> - Breaking changes, if any, will be delivered in the next **_minor_** version.
|
|
213
|
-
> - Breaking changes, if any, will be documented in the [version release](https://github.com/zimicjs/zimic/releases),
|
|
214
|
-
> along with a migration guide detailing the introduced changes and suggesting steps to migrate.
|
|
173
|
+
- [Getting started](https://github.com/zimicjs/zimic/wiki/getting‐started‐fetch)
|
|
174
|
+
- [API reference](https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch)
|
|
215
175
|
|
|
216
176
|
## Examples
|
|
217
177
|
|
|
218
|
-
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.
|
|
219
180
|
|
|
220
181
|
## Changelog
|
|
221
182
|
|