@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 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/fetch
7
7
  </h1>
8
8
 
9
9
  <p align="center">
10
- TypeScript-first HTTP request mocking
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/zimic">npm</a>
14
+ <a href="https://www.npmjs.com/package/@zimic/fetch">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,157 @@
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/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 />
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/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
- 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}`;
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
- 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 };
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
- // 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
- ```
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 cases!
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