Skip to content
Open
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
5 changes: 4 additions & 1 deletion packages/openapi-fetch/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ export interface MiddlewareCallbackParams {
}

type MiddlewareOnRequest = (
options: MiddlewareCallbackParams,
options: MiddlewareCallbackParams & {
/** body as provided to the original openapi-fetch function, pre-serialization */
body?: unknown;
},
) => void | Request | Response | undefined | Promise<Request | Response | undefined | void>;
type MiddlewareOnResponse = (
options: MiddlewareCallbackParams & { response: Response },
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export default function createClient(clientOptions) {
request,
schemaPath,
params,
body,
options,
id,
});
Expand Down
7 changes: 5 additions & 2 deletions packages/openapi-fetch/test/middleware/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertType, expect, expectTypeOf, test } from "vitest";
import type { Middleware, MiddlewareCallbackParams } from "../../src/index.js";
import type { Middleware, MiddlewareCallbackParams, MiddlewareOnRequest } from "../../src/index.js";
import { createObservedClient } from "../helpers.js";
import type { paths } from "./schemas/middleware.js";

Expand Down Expand Up @@ -297,19 +297,22 @@ test("receives OpenAPI options passed in from parent", async () => {

let receivedPath = "";
let receivedParams: MiddlewareCallbackParams["params"] = {};
let receivedBody: unknown = {};

const client = createObservedClient<paths>();
client.use({
onRequest({ schemaPath, params }) {
onRequest({ schemaPath, params, body }) {
receivedPath = schemaPath;
receivedParams = params;
receivedBody = body;
return undefined;
},
});
await client.PUT(pathname, tagData);

expect(receivedPath).toBe(pathname);
expect(receivedParams).toEqual(tagData.params);
expect(receivedBody).toEqual(tagData.body);
});

test("can be skipped without interrupting request", async () => {
Expand Down