@zimic/http 0.1.0-canary.7 → 0.1.1-canary.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 +173 -179
  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 +13 -13
  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
@@ -3,15 +3,15 @@
3
3
  </p>
4
4
 
5
5
  <h1 align="center">
6
- Zimic
6
+ @zimic/http
7
7
  </h1>
8
8
 
9
9
  <p align="center">
10
- TypeScript-first HTTP request mocking
10
+ TypeScript-first HTTP intercepting and mocking
11
11
  </p>
12
12
 
13
13
  <p align="center">
14
- <a href="https://www.npmjs.com/package/zimic">npm</a>
14
+ <a href="https://www.npmjs.com/package/@zimic/http">npm</a>
15
15
  <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
16
16
  <a href="https://github.com/zimicjs/zimic/wiki">Docs</a>
17
17
  <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
@@ -26,196 +26,190 @@
26
26
 
27
27
  [![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
28
  [![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;
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)&nbsp;
30
+ [![Stars](https://img.shields.io/github/stars/zimicjs/zimic)](https://github.com/zimicjs/zimic)
31
+
32
+ [![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;
33
+ [![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
34
 
33
35
  </div>
34
36
 
35
37
  ---
36
38
 
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
+ - [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/http` is a collection of type-safe utilities to handle HTTP requests and responses, including headers, search
51
+ params, and form data.
52
+
53
+ > [!NOTE]
54
+ >
55
+ > :seedling: This library is in **beta**.
39
56
 
40
57
  ## Features
41
58
 
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-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
- ```
59
+ - :star: **HTTP schemas and typegen**: Declare the structure of your HTTP endpoints as a TypeScript
60
+ [schema](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http‐schemas) and use it to type your HTTP requests and
61
+ responses. If you have an [OpenAPI v3](https://swagger.io/specification) declaration,
62
+ [`zimic-http typegen`](https://github.com/zimicjs/zimic/wiki/cli‐zimic‐typegen) can automatically generate the types
63
+ of your schema.
64
+ - :pushpin: **Type-safe native APIs**: Declare type-safe
65
+ [`Headers`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpheaders),
66
+ [`URLSearchParams`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpsearchparams), and
67
+ [`FormData`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpformdata) objects, fully compatible with their
68
+ native counterparts.
69
+
70
+ ## Getting started
71
+
72
+ Check our [getting started guide](https://github.com/zimicjs/zimic/wiki/getting‐started‐http).
73
+
74
+ ### Installation
75
+
76
+ | Manager | Command |
77
+ | :-----: | -------------------------------- |
78
+ | npm | `npm install @zimic/http --save` |
79
+ | yarn | `yarn add @zimic/http` |
80
+ | pnpm | `pnpm add @zimic/http` |
81
+
82
+ ## Basic usage
83
+
84
+ 1. Declare your [schema](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http‐schemas):
85
+
86
+ ```ts
87
+ import { type HttpSchema } from '@zimic/http';
88
+
89
+ interface User {
90
+ username: string;
91
+ }
92
+
93
+ interface RequestError {
94
+ code: string;
95
+ message: string;
96
+ }
97
+
98
+ type Schema = HttpSchema<{
99
+ '/users': {
100
+ POST: {
101
+ request: { body: User };
102
+ response: {
103
+ 201: { body: User };
104
+ 400: { body: RequestError };
105
+ 409: { body: RequestError };
106
+ };
107
+ };
108
+
109
+ GET: {
110
+ request: {
111
+ headers: { authorization: string };
112
+ searchParams: { query?: string; limit?: `${number}` };
113
+ };
114
+ response: {
115
+ 200: { body: User[] };
116
+ 400: { body: RequestError };
117
+ 401: { body: RequestError };
118
+ };
119
+ };
120
+ };
121
+
122
+ '/users/:userId': {
123
+ PATCH: {
124
+ request: {
125
+ headers: { authorization: string };
126
+ body: Partial<User>;
127
+ };
128
+ response: {
129
+ 204: {};
130
+ 400: { body: RequestError };
131
+ };
132
+ };
133
+ };
134
+ }>;
135
+ ```
136
+
137
+ 2. Use the types in your code!
138
+
139
+ - **Example 1**: Reference the types in your code:
140
+
141
+ ```ts
142
+ import { HttpHeaders, HttpSearchParams, HttpFormData } from '@zimic/http';
143
+
144
+ type UserListHeaders = Schema['/users']['GET']['request']['headers'];
145
+
146
+ const headers = new HttpHeaders<UserListHeaders>({
147
+ authorization: 'Bearer token',
148
+ });
149
+
150
+ type UserListSearchParams = Schema['/users']['GET']['request']['searchParams'];
151
+
152
+ const searchParams = new HttpSearchParams<UserListSearchParams>({
153
+ query: 'u',
154
+ limit: '10',
155
+ });
156
+
157
+ type UserCreateBody = Schema['/users']['POST']['request']['body'];
158
+
159
+ const formData = new HttpFormData<UserCreateBody>();
160
+ formData.append('username', 'user');
161
+ ```
162
+
163
+ - **Example 2**: Using [`@zimic/fetch`](../zimic-fetch):
164
+
165
+ ```ts
166
+ import { createFetch } from '@zimic/fetch';
194
167
 
195
- ## Documentation
168
+ const fetch = createFetch<Schema>({
169
+ baseURL: 'http://localhost:3000',
170
+ });
196
171
 
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)
172
+ const response = await fetch('/users', {
173
+ method: 'POST',
174
+ body: { username: 'user' },
175
+ });
176
+
177
+ if (!response.ok) {
178
+ throw response.error;
179
+ }
180
+
181
+ console.log(await response.json()); // { username: 'user' }
182
+ ```
183
+
184
+ - **Example 3**: Using [`@zimic/interceptor`](../zimic-interceptor):
185
+
186
+ ```ts
187
+ import { httpInterceptor } from '@zimic/interceptor/http';
188
+
189
+ const interceptor = httpInterceptor.create<Schema>({
190
+ type: 'local',
191
+ baseURL: 'http://localhost:3000',
192
+ });
193
+
194
+ await interceptor.start();
195
+
196
+ interceptor.post('/users').respond({
197
+ status: 201,
198
+ body: { username: body.username },
199
+ });
200
+ ```
203
201
 
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.
202
+ ## Documentation
203
+
204
+ - [Getting started](https://github.com/zimicjs/zimic/wiki/getting‐started‐http)
205
+ - [API reference](https://github.com/zimicjs/zimic/wiki/api‐zimic‐http)
206
+ - CLI reference
207
+ - [`zimic-http typegen`](https://github.com/zimicjs/zimic/wiki/cli‐zimic‐typegen)
215
208
 
216
209
  ## Examples
217
210
 
218
- Visit our [examples](../../examples/README.md) to see how to use Zimic with popular frameworks, libraries, and use cases!
211
+ Visit our [examples](../../examples/README.md) to see how to use Zimic with popular frameworks, libraries, and use
212
+ cases.
219
213
 
220
214
  ## Changelog
221
215