@zimic/http 0.0.1-canary.2
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 +16 -0
- package/README.md +230 -0
- package/dist/chunk-VHQRAQPQ.mjs +1371 -0
- package/dist/chunk-VHQRAQPQ.mjs.map +1 -0
- package/dist/chunk-VUDGONB5.js +1382 -0
- package/dist/chunk-VUDGONB5.js.map +1 -0
- package/dist/cli.js +116 -0
- package/dist/cli.js.map +1 -0
- package/dist/cli.mjs +109 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/index.d.ts +1306 -0
- package/dist/index.js +544 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +537 -0
- package/dist/index.mjs.map +1 -0
- package/dist/typegen.d.ts +86 -0
- package/dist/typegen.js +12 -0
- package/dist/typegen.js.map +1 -0
- package/dist/typegen.mjs +3 -0
- package/dist/typegen.mjs.map +1 -0
- package/index.d.ts +1 -0
- package/package.json +110 -0
- package/src/cli/cli.ts +92 -0
- package/src/cli/index.ts +4 -0
- package/src/cli/typegen/openapi.ts +24 -0
- package/src/formData/HttpFormData.ts +300 -0
- package/src/formData/types.ts +110 -0
- package/src/headers/HttpHeaders.ts +217 -0
- package/src/headers/types.ts +65 -0
- package/src/index.ts +55 -0
- package/src/pathParams/types.ts +67 -0
- package/src/searchParams/HttpSearchParams.ts +258 -0
- package/src/searchParams/types.ts +133 -0
- package/src/typegen/index.ts +12 -0
- package/src/typegen/namespace/TypegenNamespace.ts +18 -0
- package/src/typegen/openapi/generate.ts +168 -0
- package/src/typegen/openapi/transform/components.ts +481 -0
- package/src/typegen/openapi/transform/context.ts +67 -0
- package/src/typegen/openapi/transform/filters.ts +71 -0
- package/src/typegen/openapi/transform/imports.ts +15 -0
- package/src/typegen/openapi/transform/io.ts +86 -0
- package/src/typegen/openapi/transform/methods.ts +803 -0
- package/src/typegen/openapi/transform/operations.ts +120 -0
- package/src/typegen/openapi/transform/paths.ts +119 -0
- package/src/typegen/openapi/utils/types.ts +45 -0
- package/src/types/arrays.d.ts +4 -0
- package/src/types/json.ts +89 -0
- package/src/types/objects.d.ts +14 -0
- package/src/types/requests.ts +96 -0
- package/src/types/schema.ts +834 -0
- package/src/types/strings.d.ts +9 -0
- package/src/types/utils.ts +64 -0
- package/src/utils/console.ts +7 -0
- package/src/utils/data.ts +13 -0
- package/src/utils/files.ts +28 -0
- package/src/utils/imports.ts +12 -0
- package/src/utils/prettier.ts +13 -0
- package/src/utils/strings.ts +3 -0
- package/src/utils/time.ts +25 -0
- package/src/utils/urls.ts +52 -0
- package/typegen.d.ts +1 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Diego Aquino
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
6
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
|
7
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
8
|
+
persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
|
11
|
+
Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
14
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
15
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
16
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="../../docs/zimic.png" align="center" width="100px" height="100px">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">
|
|
6
|
+
Zimic
|
|
7
|
+
</h1>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
TypeScript-first HTTP request mocking
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
<p align="center">
|
|
14
|
+
<a href="https://www.npmjs.com/package/zimic">npm</a>
|
|
15
|
+
<span> • </span>
|
|
16
|
+
<a href="https://github.com/zimicjs/zimic/wiki">Docs</a>
|
|
17
|
+
<span> • </span>
|
|
18
|
+
<a href="#examples">Examples</a>
|
|
19
|
+
<span> • </span>
|
|
20
|
+
<a href="https://github.com/zimicjs/zimic/issues">Issues</a>
|
|
21
|
+
<span> • </span>
|
|
22
|
+
<a href="https://github.com/orgs/zimicjs/projects/1/views/5">Roadmap</a>
|
|
23
|
+
</p>
|
|
24
|
+
|
|
25
|
+
<div align="center">
|
|
26
|
+
|
|
27
|
+
[](https://github.com/zimicjs/zimic/actions/workflows/ci.yaml)
|
|
28
|
+
[](https://github.com/zimicjs/zimic/actions)
|
|
29
|
+
[](https://github.com/zimicjs/zimic/blob/canary/LICENSE.md)
|
|
30
|
+
[](https://www.npmjs.com/package/zimic)
|
|
31
|
+
[](https://github.com/zimicjs/zimic)
|
|
32
|
+
|
|
33
|
+
</div>
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
Zimic is a lightweight, thoroughly tested, TypeScript-first HTTP request mocking library, inspired by
|
|
38
|
+
[Zod](https://github.com/colinhacks/zod)'s type inference and using [MSW](https://github.com/mswjs/msw) under the hood.
|
|
39
|
+
|
|
40
|
+
## Features
|
|
41
|
+
|
|
42
|
+
Zimic provides a flexible and type-safe way to mock HTTP requests.
|
|
43
|
+
|
|
44
|
+
- :zap: **Statically-typed mocks**: Declare the
|
|
45
|
+
[schema](https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http‐schemas) of your HTTP endpoints and create
|
|
46
|
+
fully typed mocks. If you have an [OpenAPI v3](https://swagger.io/specification) schema, use
|
|
47
|
+
[`zimic typegen`](https://github.com/zimicjs/zimic/wiki/cli‐zimic‐typegen) to automatically generate types and keep
|
|
48
|
+
your mocks in sync with your API.
|
|
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)
|
|
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.
|
|
215
|
+
>
|
|
216
|
+
> From v0.8 onwards, we expect Zimic's public API to become more stable. If you'd like to share any feedback, please
|
|
217
|
+
> feel free to [open an issue](https://github.com/zimicjs/zimic/issues) or
|
|
218
|
+
> [create a discussion](https://github.com/zimicjs/zimic/discussions/new/choose)!
|
|
219
|
+
|
|
220
|
+
## Examples
|
|
221
|
+
|
|
222
|
+
Visit our [examples](../../examples/README.md) to see how to use Zimic with popular frameworks, libraries, and use cases!
|
|
223
|
+
|
|
224
|
+
## Changelog
|
|
225
|
+
|
|
226
|
+
The changelog is available on our [GitHub Releases](https://github.com/zimicjs/zimic/releases) page.
|
|
227
|
+
|
|
228
|
+
## Contributing
|
|
229
|
+
|
|
230
|
+
Interested in contributing to Zimic? Check out our [contributing guide](../../CONTRIBUTING.md) to get started!
|