@zimic/fetch 0.1.0-canary.2 → 0.1.0-canary.21
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 +110 -180
- 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 +14 -13
- 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
|
@@ -7,12 +7,10 @@
|
|
|
7
7
|
</h1>
|
|
8
8
|
|
|
9
9
|
<p align="center">
|
|
10
|
-
TypeScript-first HTTP
|
|
10
|
+
TypeScript-first HTTP integrations
|
|
11
11
|
</p>
|
|
12
12
|
|
|
13
13
|
<p align="center">
|
|
14
|
-
<a href="https://www.npmjs.com/package/zimic">npm</a>
|
|
15
|
-
<span> • </span>
|
|
16
14
|
<a href="https://github.com/zimicjs/zimic/wiki">Docs</a>
|
|
17
15
|
<span> • </span>
|
|
18
16
|
<a href="#examples">Examples</a>
|
|
@@ -26,200 +24,132 @@
|
|
|
26
24
|
|
|
27
25
|
[](https://github.com/zimicjs/zimic/actions/workflows/ci.yaml)
|
|
28
26
|
[](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)
|
|
28
|
+
[](https://github.com/zimicjs/zimic)
|
|
29
|
+
|
|
30
|
+
[](https://www.npmjs.com/package/@zimic/http)
|
|
31
|
+
[](https://bundlephobia.com/package/@zimic/http)<br />
|
|
32
|
+
[](https://www.npmjs.com/package/@zimic/fetch)
|
|
33
|
+
[](https://bundlephobia.com/package/@zimic/fetch)<br />
|
|
34
|
+
[](https://www.npmjs.com/package/@zimic/interceptor)
|
|
35
|
+
[](https://bundlephobia.com/package/@zimic/interceptor)
|
|
32
36
|
|
|
33
37
|
</div>
|
|
34
38
|
|
|
35
39
|
---
|
|
36
40
|
|
|
37
|
-
|
|
38
|
-
[Zod](https://github.com/colinhacks/zod)'s type inference and using [MSW](https://github.com/mswjs/msw) under the hood.
|
|
41
|
+
## Contents <!-- omit from toc -->
|
|
39
42
|
|
|
40
|
-
|
|
43
|
+
- [Libraries](#libraries)
|
|
44
|
+
- [`@zimic/http`](#zimichttp)
|
|
45
|
+
- [`@zimic/fetch`](#zimicfetch)
|
|
46
|
+
- [`@zimic/interceptor`](#zimicinterceptor)
|
|
47
|
+
- [Examples](#examples)
|
|
48
|
+
- [Changelog](#changelog)
|
|
49
|
+
- [Contributing](#contributing)
|
|
41
50
|
|
|
42
|
-
|
|
51
|
+
---
|
|
43
52
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
- :link: **Network-level intercepts**: Internally, Zimic combines [MSW](https://github.com/mswjs/msw) and
|
|
50
|
-
[interceptor servers](https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server) to act on real HTTP requests. From you
|
|
51
|
-
application's point of view, the mocked responses are indistinguishable from the real ones.
|
|
52
|
-
- :wrench: **Flexibility**: Mock external services and reliably test how your application behaves. Simulate success,
|
|
53
|
-
loading, and error states with ease using [standard web APIs](https://developer.mozilla.org/docs/Web/API).
|
|
54
|
-
- :bulb: **Simplicity**: Zimic was designed to encourage clarity, simplicity, and robustness in your mocks. Check our
|
|
55
|
-
[getting started guide](https://github.com/zimicjs/zimic/wiki/getting‐started) and starting mocking!
|
|
56
|
-
|
|
57
|
-
```ts
|
|
58
|
-
import { type HttpSchema } from '@zimic/http';
|
|
59
|
-
import { httpInterceptor } from 'zimic/interceptor/http';
|
|
60
|
-
|
|
61
|
-
// 1. Declare your types:
|
|
62
|
-
interface User {
|
|
63
|
-
username: string;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
interface RequestError {
|
|
67
|
-
code: string;
|
|
68
|
-
message: string;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// 2. Declare your HTTP schema:
|
|
72
|
-
// https://bit.ly/zimic-interceptor-http-schemas
|
|
73
|
-
type MySchema = HttpSchema<{
|
|
74
|
-
'/users': {
|
|
75
|
-
POST: {
|
|
76
|
-
request: { body: User };
|
|
77
|
-
response: {
|
|
78
|
-
201: { body: User };
|
|
79
|
-
400: { body: RequestError };
|
|
80
|
-
409: { body: RequestError };
|
|
81
|
-
};
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
GET: {
|
|
85
|
-
request: {
|
|
86
|
-
headers: { authorization: string };
|
|
87
|
-
searchParams: {
|
|
88
|
-
username?: string;
|
|
89
|
-
limit?: `${number}`;
|
|
90
|
-
};
|
|
91
|
-
};
|
|
92
|
-
response: {
|
|
93
|
-
200: { body: User[] };
|
|
94
|
-
400: { body: RequestError };
|
|
95
|
-
401: { body: RequestError };
|
|
96
|
-
};
|
|
97
|
-
};
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
'/users/:userId': {
|
|
101
|
-
PATCH: {
|
|
102
|
-
request: {
|
|
103
|
-
headers: { authorization: string };
|
|
104
|
-
body: Partial<User>;
|
|
105
|
-
};
|
|
106
|
-
response: {
|
|
107
|
-
204: {};
|
|
108
|
-
400: { body: RequestError };
|
|
109
|
-
};
|
|
110
|
-
};
|
|
111
|
-
};
|
|
112
|
-
}>;
|
|
113
|
-
|
|
114
|
-
// 3. Create your interceptor:
|
|
115
|
-
// https://bit.ly/zimic-interceptor-http#httpinterceptorcreateoptions
|
|
116
|
-
const myInterceptor = httpInterceptor.create<MySchema>({
|
|
117
|
-
type: 'local',
|
|
118
|
-
baseURL: 'http://localhost:3000',
|
|
119
|
-
saveRequests: true, // Allow access to `handler.requests()`
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
// 4. Manage your interceptor lifecycle:
|
|
123
|
-
// https://bit.ly/zimic-guides-testing
|
|
124
|
-
beforeAll(async () => {
|
|
125
|
-
// 4.1. Start intercepting requests:
|
|
126
|
-
// https://bit.ly/zimic-interceptor-http#http-interceptorstart
|
|
127
|
-
await myInterceptor.start();
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
beforeEach(() => {
|
|
131
|
-
// 4.2. Clear interceptors so that no tests affect each other:
|
|
132
|
-
// https://bit.ly/zimic-interceptor-http#http-interceptorclear
|
|
133
|
-
myInterceptor.clear();
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
afterEach(() => {
|
|
137
|
-
// 4.3. Check that all expected requests were made:
|
|
138
|
-
// https://bit.ly/zimic-interceptor-http#http-interceptorchecktimes
|
|
139
|
-
myInterceptor.checkTimes();
|
|
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
|
-
```
|
|
194
|
-
|
|
195
|
-
## Documentation
|
|
196
|
-
|
|
197
|
-
- [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)
|
|
53
|
+
Zimic is a set of lightweight, thoroughly tested, TypeScript-first HTTP integration libraries.
|
|
54
|
+
|
|
55
|
+
## Libraries
|
|
56
|
+
|
|
57
|
+
### `@zimic/http`
|
|
203
58
|
|
|
204
59
|
> [!NOTE]
|
|
205
60
|
>
|
|
206
|
-
>
|
|
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`
|
|
85
|
+
|
|
86
|
+
> [!WARNING]
|
|
207
87
|
>
|
|
208
|
-
>
|
|
209
|
-
|
|
210
|
-
|
|
88
|
+
> :construction: This library is **experimental**.
|
|
89
|
+
|
|
90
|
+
[`@zimic/fetch`](../../packages/zimic-fetch) is a minimal (1 kB minified and gzipped), zero-dependency, and type-safe
|
|
91
|
+
`fetch`-like API client.
|
|
92
|
+
|
|
93
|
+
- :sparkles: **Type-safe `fetch`**: Create a type-safe
|
|
94
|
+
[`fetch`-like](https://developer.mozilla.org/docs/Web/API/Fetch_API) API client. Use your
|
|
95
|
+
[`@zimic/http` schema](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http‐schemas) and have your requests and
|
|
96
|
+
responses fully typed by default.
|
|
97
|
+
- :muscle: **Developer experience**: `@zimic/fetch` seeks to be as compatible with the
|
|
98
|
+
[native Fetch API](https://developer.mozilla.org/docs/Web/API/Fetch_API) as possible, while providing an ergonomic
|
|
99
|
+
interface to improve type safety. Define default options to apply to your requests, such as a base URL, headers,
|
|
100
|
+
search parameters, and more. Inspect and modify requests and responses using
|
|
101
|
+
[`onRequest`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchonrequest) and
|
|
102
|
+
[`onResponse`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchonresponse) listeners.
|
|
103
|
+
|
|
104
|
+
**Learn more**:
|
|
105
|
+
|
|
106
|
+
- [`@zimic/fetch` - Getting started](https://github.com/zimicjs/zimic/wiki/getting‐started‐fetch)
|
|
107
|
+
- [`@zimic/fetch` - API reference](https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch)
|
|
108
|
+
|
|
109
|
+
### `@zimic/interceptor`
|
|
110
|
+
|
|
111
|
+
> [!NOTE]
|
|
211
112
|
>
|
|
212
|
-
>
|
|
213
|
-
|
|
214
|
-
|
|
113
|
+
> :seedling: This library is in **beta**.
|
|
114
|
+
|
|
115
|
+
[`@zimic/interceptor`](../../packages/zimic-interceptor) provides a flexible and type-safe way to intercept and mock HTTP
|
|
116
|
+
requests.
|
|
117
|
+
|
|
118
|
+
- :globe_with_meridians: **HTTP interceptors**: Intercept HTTP requests and return mock responses. Use
|
|
119
|
+
[local](https://github.com/zimicjs/zimic/wiki/getting‐started#local-http-interceptors) or
|
|
120
|
+
[remote](https://github.com/zimicjs/zimic/wiki/getting‐started#remote-http-interceptors) interceptors to adapt your
|
|
121
|
+
mocks to your development and testing workflow.
|
|
122
|
+
- :zap: **Fully typed mocks**: Use your
|
|
123
|
+
[`@zimic/http` schema](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http‐schemas) and create type-safe mocks for
|
|
124
|
+
your HTTP requests.
|
|
125
|
+
- :link: **Network-level interceptor**: `@zimic/interceptor` combines [MSW](https://github.com/mswjs/msw) and
|
|
126
|
+
[interceptor servers](https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server) to handle real HTTP requests. From you
|
|
127
|
+
application's point of view, the mocked responses are indistinguishable from the real ones.
|
|
128
|
+
- :wrench: **Flexibility**: Mock external services and reliably test how your application behaves. Simulate success,
|
|
129
|
+
loading, and error states with ease using [standard web APIs](https://developer.mozilla.org/docs/Web/API).
|
|
130
|
+
- :bulb: **Simplicity**: `@zimic/interceptor` was designed to encourage clarity, simplicity, and robustness in your
|
|
131
|
+
mocks.
|
|
132
|
+
|
|
133
|
+
> [!TIP]
|
|
215
134
|
>
|
|
216
|
-
>
|
|
217
|
-
>
|
|
218
|
-
>
|
|
135
|
+
> `@zimic/fetch` and `@zimic/interceptor` are not required to be used together. `@zimic/interceptor` is compatible with
|
|
136
|
+
> any HTTP client implementation, as `@zimic/fetch` works with any HTTP interceptor library. With that in mind,
|
|
137
|
+
> `@zimic/fetch` and `@zimic/interceptor` work best together, providing a seamless and type-safe experience for
|
|
138
|
+
> performing HTTP requests in your application and mocking them during development and testing.
|
|
139
|
+
|
|
140
|
+
**Learn more**:
|
|
141
|
+
|
|
142
|
+
- [`@zimic/interceptor` - Getting started](https://github.com/zimicjs/zimic/wiki/getting‐started‐interceptor)
|
|
143
|
+
- [`@zimic/interceptor` - API reference](https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http)
|
|
144
|
+
- `@zimic/interceptor` - CLI reference
|
|
145
|
+
- [Browser](https://github.com/zimicjs/zimic/wiki/cli‐zimic‐browser)
|
|
146
|
+
- [Server](https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server)
|
|
147
|
+
- `@zimic/interceptor` - Guides
|
|
148
|
+
- [Testing](https://github.com/zimicjs/zimic/wiki/guides‐testing‐interceptor)
|
|
219
149
|
|
|
220
150
|
## Examples
|
|
221
151
|
|
|
222
|
-
Visit our [examples](../../examples/README.md) to see how to use Zimic with popular frameworks, libraries, and use cases
|
|
152
|
+
Visit our [examples](../../examples/README.md) to see how to use Zimic with popular frameworks, libraries, and use cases.
|
|
223
153
|
|
|
224
154
|
## Changelog
|
|
225
155
|
|