Skip to content

Expose original request on Middleware.onResponse #1697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/openapi-fetch/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,17 @@ And it expects either:
### onResponse

```ts
onResponse(res, options) {
onResponse(res, options, req) {
// …
}
```

`onResponse()` also takes 2 params:
`onResponse()` also takes 3 params:
| Name | Type | Description |
| :-------- | :-----------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `req` | `MiddlewareRequest` | A standard [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response). |
| `req` | `Response` | A standard [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response). |
| `options` | `MergedOptions` | Combination of [createClient](/openapi-fetch/api#create-client) options + [fetch overrides](/openapi-fetch/api#fetch-options) |
| `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) |
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| `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) |
| `req` | `MiddlewareRequest` | The original [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request), see `onRequest` |


And it expects either:

Expand Down
6 changes: 5 additions & 1 deletion packages/openapi-fetch/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ export function onRequest(
req: MiddlewareRequest,
options: MergedOptions,
): Request | undefined | Promise<Request | undefined>;
export function onResponse(res: Response, options: MergedOptions): Response | undefined | Promise<Response | undefined>;
export function onResponse(
res: Response,
options: MergedOptions,
req: MiddlewareRequest,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This is a good non-breaking change. Will probably opt for a more “logical” ordering in a future breaking change

): Response | undefined | Promise<Response | undefined>;

export interface Middleware {
onRequest?: typeof onRequest;
Expand Down
4 changes: 3 additions & 1 deletion packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ export default function createClient(clientOptions) {
for (let i = middlewares.length - 1; i >= 0; i--) {
const m = middlewares[i];
if (m && typeof m === "object" && typeof m.onResponse === "function") {
const result = await m.onResponse(response, mergedOptions);
request.schemaPath = url; // (re)attach original URL
request.params = params; // (re)attach params
const result = await m.onResponse(response, mergedOptions, request);
if (result) {
if (!(result instanceof Response)) {
throw new Error("Middleware must return new Response() when modifying the response");
Expand Down
23 changes: 23 additions & 0 deletions packages/openapi-fetch/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,29 @@ describe("client", () => {
expect(requestBaseUrl).toBe("https://api.foo.bar/v1");
});

it("receives the original request", async () => {
useMockRequestHandler({
baseUrl: "https://api.foo.bar/v1/",
method: "get",
path: "/self",
status: 200,
body: {},
});

const client = createClient<paths>({
baseUrl: "https://api.foo.bar/v1/",
});
client.use({
onResponse(res, options, req) {
expect(req).toBeInstanceOf(Request);

return undefined;
},
});

await client.GET("/self");
});

it("receives OpenAPI options passed in from parent", async () => {
useMockRequestHandler({
method: "put",
Expand Down