You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The 7.x docs are for a beta release that’s not production-ready yet. See the [6.x docs](/6.x/introduction) for the stable version.
11
-
12
-
:::
13
-
14
8
openapi-typescript turns [OpenAPI 3.0 & 3.1](https://spec.openapis.org/oas/latest.html) schemas into TypeScript quickly using Node.js. No Java/node-gyp/running OpenAPI servers necessary.
15
9
16
10
The code is [MIT-licensed](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-typescript/LICENSE") and free for use.
@@ -39,7 +33,7 @@ _Note: OpenAPI 2.x is supported with versions `5.x` and previous_
39
33
This library requires the latest version of [Node.js](https://nodejs.org) installed (20.x or higher recommended). With that present, run the following in your project:
40
34
41
35
```bash
42
-
npm i -D openapi-typescript@next typescript
36
+
npm i -D openapi-typescript typescript
43
37
```
44
38
45
39
And in your `tsconfig.json`, to load the types properly:
|`req`|`MiddlewareRequest`| A standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) with `schemaPath` (OpenAPI pathname) and `params` ([params](/openapi-fetch/api#fetch-options) object) |
199
-
|`options`|`MergedOptions`| Combination of [createClient](/openapi-fetch/api#create-client) options + [fetch overrides](/openapi-fetch/api#fetch-options)|
188
+
#### Parameters
200
189
201
-
And it expects either:
190
+
Each middleware callback receives the following `options` object with the following:
202
191
203
-
-**If modifying the request:** A [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request)
|`req`|`Response`| A standard [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response). |
218
-
|`options`|`MergedOptions`| Combination of [createClient](/openapi-fetch/api#create-client) options + [fetch overrides](/openapi-fetch/api#fetch-options)|
219
-
|`req`|`MiddlewareRequest`| A standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) with `schemaPath` (OpenAPI pathname) and `params` ([params](/openapi-fetch/api#fetch-options) object) |
220
-
221
-
And it expects either:
222
-
223
-
-**If modifying the response:** A [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response)
224
-
-**If not modifying:**`undefined` (void)
225
-
226
-
### Skipping
227
-
228
-
If you want to skip the middleware under certain conditions, just `return` as early as possible:
229
-
230
-
```ts
231
-
onRequest(req) {
232
-
if (req.schemaPath!=="/projects/{project_id}") {
233
-
returnundefined;
234
-
}
235
-
// …
236
-
}
237
-
```
203
+
Each middleware callback can return:
238
204
239
-
This will leave the request/response unmodified, and pass things off to the next middleware handler (if any). There’s no internal callback or observer library needed.
205
+
-**onRequest**: Either a `Request` to modify the request, or `undefined` to leave it untouched (skip)
206
+
-**onResponse** Either a `Response` to modify the response, or `undefined` to leave it untouched (skip)
@@ -48,8 +48,8 @@ The order in which middleware are registered matters. For requests, `onRequest()
48
48
If you want to skip the middleware under certain conditions, just `return` as early as possible:
49
49
50
50
```ts
51
-
onRequest(req) {
52
-
if (req.schemaPath!=="/projects/{project_id}") {
51
+
onRequest({ schemaPath }) {
52
+
if (schemaPath!=="/projects/{project_id}") {
53
53
returnundefined;
54
54
}
55
55
// …
@@ -63,9 +63,9 @@ This will leave the request/response unmodified, and pass things off to the next
63
63
Middleware can also be used to throw an error that `fetch()` wouldn’t normally, useful in libraries like [TanStack Query](https://tanstack.com/query/latest):
64
64
65
65
```ts
66
-
onResponse(res) {
67
-
if (res.error) {
68
-
thrownewError(res.error.message);
66
+
onResponse({ response }) {
67
+
if (response.error) {
68
+
thrownewError(response.error.message);
69
69
}
70
70
}
71
71
```
@@ -98,12 +98,10 @@ By default, `openapi-fetch` will **NOT** arbitrarily clone requests/responses fo
98
98
<!-- prettier-ignore -->
99
99
```ts
100
100
const myMiddleware:Middleware= {
101
-
onResponse(res) {
102
-
if (res) {
103
-
const data =awaitres.json(); // [!code --]
104
-
const data =awaitres.clone().json(); // [!code ++]
105
-
returnundefined;
106
-
}
101
+
onResponse({ response }) {
102
+
const data =awaitresponse.json(); // [!code --]
103
+
const data =awaitresponse.clone().json(); // [!code ++]
104
+
returnundefined;
107
105
},
108
106
};
109
107
```
@@ -125,7 +123,7 @@ import type { paths } from "./my-openapi-3-schema";
Copy file name to clipboardexpand all lines: packages/openapi-fetch/CHANGELOG.md
+11
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,16 @@
1
1
# openapi-fetch
2
2
3
+
## 0.10.0
4
+
5
+
### Minor Changes
6
+
7
+
- ⚠️ **Breaking Change**: `openapi-typescript@7` is needed to work. You’ll get type errors with `openapi-typescript@6` and below.
8
+
- ⚠️ **Breaking Change**: The Middleware API has changed to be an object rather than `(request, options)` or `(response, options)`. [See Middleware docs](https://openapi-ts.dev/openapi-fetch/middleware-auth) for updated API.
9
+
- ⚠️ **Breaking Change**: The `Content-Type` header is no longer sent by default if a body payload is attached.
10
+
- ⚠️ **Breaking Change**: The `customFetch` type now calls `fetch(input: string, init: RequestInit)` and the types have been updated, rather than `fetch(input: Request)` introduced in `0.9.0`.
11
+
12
+
- Added `id` to middleware handlers that create a unique ID per-fetch
0 commit comments