@zimic/http 0.1.0-canary.7 → 0.1.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.
Files changed (47) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +111 -177
  3. package/dist/{chunk-6UNIPPBC.mjs → chunk-C5GWXTO5.mjs} +117 -140
  4. package/dist/chunk-C5GWXTO5.mjs.map +1 -0
  5. package/dist/{chunk-343XY3VF.js → chunk-KDDZRZK6.js} +117 -140
  6. package/dist/chunk-KDDZRZK6.js.map +1 -0
  7. package/dist/cli.js +9 -9
  8. package/dist/cli.js.map +1 -1
  9. package/dist/cli.mjs +3 -3
  10. package/dist/cli.mjs.map +1 -1
  11. package/dist/index.d.ts +22 -37
  12. package/dist/index.js +17 -19
  13. package/dist/index.js.map +1 -1
  14. package/dist/index.mjs +17 -19
  15. package/dist/index.mjs.map +1 -1
  16. package/dist/typegen.js +2 -2
  17. package/dist/typegen.mjs +1 -1
  18. package/package.json +12 -11
  19. package/src/formData/HttpFormData.ts +2 -2
  20. package/src/formData/types.ts +1 -1
  21. package/src/headers/HttpHeaders.ts +12 -10
  22. package/src/headers/types.ts +1 -1
  23. package/src/pathParams/types.ts +1 -1
  24. package/src/searchParams/HttpSearchParams.ts +1 -1
  25. package/src/searchParams/types.ts +1 -1
  26. package/src/typegen/openapi/generate.ts +1 -2
  27. package/src/typegen/openapi/transform/components.ts +2 -3
  28. package/src/typegen/openapi/transform/filters.ts +3 -3
  29. package/src/typegen/openapi/transform/io.ts +3 -5
  30. package/src/typegen/openapi/transform/methods.ts +4 -5
  31. package/src/typegen/openapi/transform/operations.ts +2 -3
  32. package/src/typegen/openapi/transform/paths.ts +2 -3
  33. package/src/types/requests.ts +2 -1
  34. package/src/types/schema.ts +14 -32
  35. package/src/utils/console.ts +1 -1
  36. package/src/utils/files.ts +4 -24
  37. package/src/utils/prettier.ts +1 -1
  38. package/src/utils/time.ts +1 -1
  39. package/dist/chunk-343XY3VF.js.map +0 -1
  40. package/dist/chunk-6UNIPPBC.mjs.map +0 -1
  41. package/src/types/arrays.d.ts +0 -4
  42. package/src/types/objects.d.ts +0 -14
  43. package/src/types/strings.d.ts +0 -9
  44. package/src/types/utils.ts +0 -40
  45. package/src/utils/data.ts +0 -13
  46. package/src/utils/imports.ts +0 -12
  47. package/src/utils/urls.ts +0 -52
package/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 Diego Aquino
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 request mocking
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>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
16
14
  <a href="https://github.com/zimicjs/zimic/wiki">Docs</a>
17
15
  <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
18
16
  <a href="#examples">Examples</a>
@@ -26,196 +24,132 @@
26
24
 
27
25
  [![CI](https://github.com/zimicjs/zimic/actions/workflows/ci.yaml/badge.svg?branch=canary)](https://github.com/zimicjs/zimic/actions/workflows/ci.yaml)&nbsp;
28
26
  [![Coverage](https://img.shields.io/badge/Coverage-100%25-31C654?labelColor=353C43)](https://github.com/zimicjs/zimic/actions)&nbsp;
29
- [![License](https://img.shields.io/github/license/zimicjs/zimic?color=0E69BE&label=License&labelColor=353C43)](https://github.com/zimicjs/zimic/blob/canary/LICENSE.md)
30
- [![NPM Downloads](https://img.shields.io/npm/dm/zimic?style=flat&logo=npm&color=0E69BE&label=Downloads&labelColor=353C43)](https://www.npmjs.com/package/zimic)&nbsp;
31
- [![Stars](https://img.shields.io/github/stars/zimicjs/zimic)](https://github.com/zimicjs/zimic)&nbsp;
27
+ [![License](https://img.shields.io/github/license/zimicjs/zimic?color=0E69BE&label=License&labelColor=353C43)](https://github.com/zimicjs/zimic/blob/canary/LICENSE.md)&nbsp;
28
+ [![Stars](https://img.shields.io/github/stars/zimicjs/zimic)](https://github.com/zimicjs/zimic)
29
+
30
+ [![NPM Downloads - @zimic/http](https://img.shields.io/npm/dm/@zimic/http?style=flat&logo=npm&color=0E69BE&label=%20%40zimic%2Fhttp&labelColor=353C43)](https://www.npmjs.com/package/@zimic/http)&nbsp;
31
+ [![Bundle size - @zimic/http](https://badgen.net/bundlephobia/minzip/@zimic/http?color=0E69BE&labelColor=353C43&label=@zimic/http%20min%20gzip)](https://bundlephobia.com/package/@zimic/http)<br />
32
+ [![NPM Downloads - @zimic/fetch](https://img.shields.io/npm/dm/@zimic/fetch?style=flat&logo=npm&color=0E69BE&label=%20%40zimic%2Ffetch&labelColor=353C43)](https://www.npmjs.com/package/@zimic/fetch)&nbsp;
33
+ [![Bundle size - @zimic/fetch](https://badgen.net/bundlephobia/minzip/@zimic/fetch?color=0E69BE&labelColor=353C43&label=@zimic/fetch%20min%20gzip)](https://bundlephobia.com/package/@zimic/fetch)<br />
34
+ [![NPM Downloads - @zimic/interceptor](https://img.shields.io/npm/dm/@zimic/interceptor?style=flat&logo=npm&color=0E69BE&label=%20%40zimic%2Finterceptor&labelColor=353C43)](https://www.npmjs.com/package/@zimic/interceptor)&nbsp;
35
+ [![Bundle size - @zimic/interceptor](https://badgen.net/bundlephobia/minzip/@zimic/interceptor?color=0E69BE&labelColor=353C43&label=@zimic/interceptor%20min%20gzip)](https://bundlephobia.com/package/@zimic/interceptor)&nbsp;
32
36
 
33
37
  </div>
34
38
 
35
39
  ---
36
40
 
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.
41
+ ## Contents <!-- omit from toc -->
39
42
 
40
- ## Features
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
- Zimic provides a flexible and type-safe way to mock HTTP requests.
51
+ ---
43
52
 
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-http typegen`](https://github.com/zimicjs/zimic/wiki/cli‐zimic‐typegen) to automatically generate types and
48
- keep 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)
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
- > Zimic has gone a long way in v0, but we're not yet v1!
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]
87
+ >
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]
207
112
  >
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:
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]
211
134
  >
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.
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)
215
149
 
216
150
  ## Examples
217
151
 
218
- 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.
219
153
 
220
154
  ## Changelog
221
155