@zimic/fetch 0.1.0 → 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.
Files changed (2) hide show
  1. package/README.md +119 -92
  2. package/package.json +5 -6
package/README.md CHANGED
@@ -3,14 +3,16 @@
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 integrations
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/fetch">npm</a>
15
+ <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
14
16
  <a href="https://github.com/zimicjs/zimic/wiki">Docs</a>
15
17
  <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
16
18
  <a href="#examples">Examples</a>
@@ -27,68 +29,31 @@
27
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;
28
30
  [![Stars](https://img.shields.io/github/stars/zimicjs/zimic)](https://github.com/zimicjs/zimic)
29
31
 
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
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
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;
36
34
 
37
35
  </div>
38
36
 
39
37
  ---
40
38
 
41
- ## Contents <!-- omit from toc -->
42
-
43
- - [Libraries](#libraries)
44
- - [`@zimic/http`](#zimichttp)
45
- - [`@zimic/fetch`](#zimicfetch)
46
- - [`@zimic/interceptor`](#zimicinterceptor)
39
+ - [Features](#features)
40
+ - [Getting started](#getting-started)
41
+ - [Installation](#installation)
42
+ - [Basic usage](#basic-usage)
43
+ - [Documentation](#documentation)
47
44
  - [Examples](#examples)
48
45
  - [Changelog](#changelog)
49
46
  - [Contributing](#contributing)
50
47
 
51
48
  ---
52
49
 
53
- Zimic is a set of lightweight, thoroughly tested, TypeScript-first HTTP integration libraries.
54
-
55
- ## Libraries
56
-
57
- ### `@zimic/http`
58
-
59
- > [!NOTE]
60
- >
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`
50
+ `@zimic/fetch` is a minimal (1 kB minified and gzipped), zero-dependency, and type-safe `fetch`-like API client.
85
51
 
86
52
  > [!WARNING]
87
53
  >
88
54
  > :construction: This library is **experimental**.
89
55
 
90
- [`@zimic/fetch`](../../packages/zimic-fetch) is a minimal (1 kB minified and gzipped), zero-dependency, and type-safe
91
- `fetch`-like API client.
56
+ ## Features
92
57
 
93
58
  - :sparkles: **Type-safe `fetch`**: Create a type-safe
94
59
  [`fetch`-like](https://developer.mozilla.org/docs/Web/API/Fetch_API) API client. Use your
@@ -101,55 +66,117 @@ including headers, search params, and form data.
101
66
  [`onRequest`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchonrequest) and
102
67
  [`onResponse`](https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchonresponse) listeners.
103
68
 
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]
112
- >
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]
134
- >
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)
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
+ };
121
+ };
122
+ };
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
+ };
136
+ };
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
+ ```
169
+
170
+ ## Documentation
171
+
172
+ - [Introduction](https://github.com/zimicjs/zimic/wiki)
173
+ - [Getting started](https://github.com/zimicjs/zimic/wiki/getting‐started‐fetch)
174
+ - [API reference](https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch)
149
175
 
150
176
  ## Examples
151
177
 
152
- 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.
153
180
 
154
181
  ## Changelog
155
182
 
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "api",
14
14
  "static"
15
15
  ],
16
- "version": "0.1.0",
16
+ "version": "0.1.1-canary.1",
17
17
  "repository": {
18
18
  "type": "git",
19
19
  "url": "https://github.com/zimicjs/zimic.git",
@@ -66,10 +66,10 @@
66
66
  "typescript": "^5.8.2",
67
67
  "vitest": "^3.0.7",
68
68
  "@zimic/eslint-config-node": "0.0.0",
69
- "@zimic/interceptor": "0.14.0-canary.26",
69
+ "@zimic/tsconfig": "0.0.0",
70
+ "@zimic/interceptor": "0.14.0",
70
71
  "@zimic/lint-staged-config": "0.0.0",
71
- "@zimic/utils": "0.0.0",
72
- "@zimic/tsconfig": "0.0.0"
72
+ "@zimic/utils": "0.0.0"
73
73
  },
74
74
  "peerDependencies": {
75
75
  "@zimic/http": "^0.1.0 || ^0.1.0-canary.0",
@@ -87,7 +87,6 @@
87
87
  "test": "dotenv -v NODE_ENV=test -- vitest",
88
88
  "test:turbo": "dotenv -v CI=true -- pnpm run test run --coverage",
89
89
  "types:check": "tsc --noEmit",
90
- "deps:init": "playwright install chromium",
91
- "prepublish:patch-relative-paths": "sed -E -i 's/\\]\\(\\.\\/([^\\)]+)\\)/](..\\/..\\/\\1)/g;s/\"\\.\\/([^\"]+)\"/\"..\\/..\\/\\1\"/g'"
90
+ "deps:init": "playwright install chromium"
92
91
  }
93
92
  }