Skip to content

Commit 78fe9b8

Browse files
committed
Expose original request on Middleware.onResponse
1 parent 19ab734 commit 78fe9b8

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

docs/openapi-fetch/api.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,17 @@ And it expects either:
206206
### onResponse
207207

208208
```ts
209-
onResponse(res, options) {
209+
onResponse(res, options, req) {
210210
//
211211
}
212212
```
213213

214-
`onResponse()` also takes 2 params:
214+
`onResponse()` also takes 3 params:
215215
| Name | Type | Description |
216216
| :-------- | :-----------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
217-
| `req` | `MiddlewareRequest` | A standard [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response). |
217+
| `req` | `Response` | A standard [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response). |
218218
| `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) |
219220

220221
And it expects either:
221222

packages/openapi-fetch/src/index.d.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ export function onRequest(
144144
req: MiddlewareRequest,
145145
options: MergedOptions,
146146
): Request | undefined | Promise<Request | undefined>;
147-
export function onResponse(res: Response, options: MergedOptions): Response | undefined | Promise<Response | undefined>;
147+
export function onResponse(
148+
res: Response,
149+
options: MergedOptions,
150+
req: MiddlewareRequest,
151+
): Response | undefined | Promise<Response | undefined>;
148152

149153
export interface Middleware {
150154
onRequest?: typeof onRequest;

packages/openapi-fetch/src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ export default function createClient(clientOptions) {
114114
for (let i = middlewares.length - 1; i >= 0; i--) {
115115
const m = middlewares[i];
116116
if (m && typeof m === "object" && typeof m.onResponse === "function") {
117-
const result = await m.onResponse(response, mergedOptions);
117+
request.schemaPath = url; // (re)attach original URL
118+
request.params = params; // (re)attach params
119+
const result = await m.onResponse(response, mergedOptions, request);
118120
if (result) {
119121
if (!(result instanceof Response)) {
120122
throw new Error("Middleware must return new Response() when modifying the response");

packages/openapi-fetch/test/index.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,29 @@ describe("client", () => {
10131013
expect(requestBaseUrl).toBe("https://api.foo.bar/v1");
10141014
});
10151015

1016+
it("receives the original request", async () => {
1017+
useMockRequestHandler({
1018+
baseUrl: "https://api.foo.bar/v1/",
1019+
method: "get",
1020+
path: "/self",
1021+
status: 200,
1022+
body: {},
1023+
});
1024+
1025+
const client = createClient<paths>({
1026+
baseUrl: "https://api.foo.bar/v1/",
1027+
});
1028+
client.use({
1029+
onResponse(res, options, req) {
1030+
expect(req).toBeInstanceOf(Request);
1031+
1032+
return undefined;
1033+
},
1034+
});
1035+
1036+
await client.GET("/self");
1037+
});
1038+
10161039
it("receives OpenAPI options passed in from parent", async () => {
10171040
useMockRequestHandler({
10181041
method: "put",

0 commit comments

Comments
 (0)