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
Copy file name to clipboardexpand all lines: docs/openapi-fetch/api.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -268,7 +268,7 @@ And the `onError` callback receives an additional `error` property:
268
268
269
269
Each middleware callback can return:
270
270
271
-
-**onRequest**: Either a `Request` to modify the request, or `undefined` to leave it untouched (skip)
271
+
-**onRequest**: A `Request` to modify the request, a `Response` to short-circuit the middleware chain, or `undefined` to leave request untouched (skip)
272
272
-**onResponse**: Either a `Response` to modify the response, or `undefined` to leave it untouched (skip)
273
273
-**onError**: Either an `Error` to modify the error that is thrown, a `Response` which means that the `fetch` call will proceed as successful, or `undefined` to leave the error untouched (skip)
Copy file name to clipboardexpand all lines: docs/openapi-fetch/middleware-auth.md
+32
Original file line number
Diff line number
Diff line change
@@ -64,6 +64,38 @@ onRequest({ schemaPath }) {
64
64
65
65
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.
66
66
67
+
### Early Response
68
+
69
+
You can return a `Response` directly from `onRequest`, which will skip the actual request and remaining middleware chain. This is useful for cases such as deduplicating or caching responses to avoid unnecessary network requests.
// Return cached response, skipping actual request and remaining middleware chain
81
+
returncached.clone();
82
+
}
83
+
},
84
+
onResponse({ request, response }) {
85
+
if (response.ok) {
86
+
const key =getCacheKey(request);
87
+
cache.set(key, response.clone());
88
+
}
89
+
}
90
+
};
91
+
```
92
+
93
+
When a middleware returns a `Response`:
94
+
95
+
* The request is not sent to the server
96
+
* Subsequent `onRequest` handlers are skipped
97
+
*`onResponse` handlers are skipped
98
+
67
99
### Throwing
68
100
69
101
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):
0 commit comments