@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.
- package/LICENSE.md +1 -1
- package/README.md +173 -179
- package/dist/{chunk-6UNIPPBC.mjs → chunk-C5GWXTO5.mjs} +117 -140
- package/dist/chunk-C5GWXTO5.mjs.map +1 -0
- package/dist/{chunk-343XY3VF.js → chunk-KDDZRZK6.js} +117 -140
- package/dist/chunk-KDDZRZK6.js.map +1 -0
- package/dist/cli.js +9 -9
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +3 -3
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.ts +22 -37
- package/dist/index.js +17 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +17 -19
- package/dist/index.mjs.map +1 -1
- package/dist/typegen.js +2 -2
- package/dist/typegen.mjs +1 -1
- package/package.json +13 -13
- package/src/formData/HttpFormData.ts +2 -2
- package/src/formData/types.ts +1 -1
- package/src/headers/HttpHeaders.ts +12 -10
- package/src/headers/types.ts +1 -1
- package/src/pathParams/types.ts +1 -1
- package/src/searchParams/HttpSearchParams.ts +1 -1
- package/src/searchParams/types.ts +1 -1
- package/src/typegen/openapi/generate.ts +1 -2
- package/src/typegen/openapi/transform/components.ts +2 -3
- package/src/typegen/openapi/transform/filters.ts +3 -3
- package/src/typegen/openapi/transform/io.ts +3 -5
- package/src/typegen/openapi/transform/methods.ts +4 -5
- package/src/typegen/openapi/transform/operations.ts +2 -3
- package/src/typegen/openapi/transform/paths.ts +2 -3
- package/src/types/requests.ts +2 -1
- package/src/types/schema.ts +14 -32
- package/src/utils/console.ts +1 -1
- package/src/utils/files.ts +4 -24
- package/src/utils/prettier.ts +1 -1
- package/src/utils/time.ts +1 -1
- package/dist/chunk-343XY3VF.js.map +0 -1
- package/dist/chunk-6UNIPPBC.mjs.map +0 -1
- package/src/types/arrays.d.ts +0 -4
- package/src/types/objects.d.ts +0 -14
- package/src/types/strings.d.ts +0 -9
- package/src/types/utils.ts +0 -40
- package/src/utils/data.ts +0 -13
- package/src/utils/imports.ts +0 -12
- 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
|
|
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
|
-
|
|
6
|
+
@zimic/http
|
|
7
7
|
</h1>
|
|
8
8
|
|
|
9
9
|
<p align="center">
|
|
10
|
-
TypeScript-first HTTP
|
|
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/
|
|
14
|
+
<a href="https://www.npmjs.com/package/@zimic/http">npm</a>
|
|
15
15
|
<span> • </span>
|
|
16
16
|
<a href="https://github.com/zimicjs/zimic/wiki">Docs</a>
|
|
17
17
|
<span> • </span>
|
|
@@ -26,196 +26,190 @@
|
|
|
26
26
|
|
|
27
27
|
[](https://github.com/zimicjs/zimic/actions/workflows/ci.yaml)
|
|
28
28
|
[](https://github.com/zimicjs/zimic/actions)
|
|
29
|
-
[](https://github.com/zimicjs/zimic/blob/canary/LICENSE.md)
|
|
30
|
-
[](https://github.com/zimicjs/zimic/blob/canary/LICENSE.md)
|
|
30
|
+
[](https://github.com/zimicjs/zimic)
|
|
31
|
+
|
|
32
|
+
[](https://www.npmjs.com/package/@zimic/http)
|
|
33
|
+
[](https://bundlephobia.com/package/@zimic/http)<br />
|
|
32
34
|
|
|
33
35
|
</div>
|
|
34
36
|
|
|
35
37
|
---
|
|
36
38
|
|
|
37
|
-
|
|
38
|
-
[
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
[
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
[
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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
|
-
|
|
168
|
+
const fetch = createFetch<Schema>({
|
|
169
|
+
baseURL: 'http://localhost:3000',
|
|
170
|
+
});
|
|
196
171
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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
|
|
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
|
|