Skip to content

Commit

Permalink
Improve middleware type definition (#1918)
Browse files Browse the repository at this point in the history
* Improve middleware type definition

* Add type test for middleware

* Add changeset
  • Loading branch information
yicrotkd authored Sep 20, 2024
1 parent d2cb020 commit e39d11e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-lizards-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Improve `Middleware` type definition to require either onRequest or onResponse
22 changes: 16 additions & 6 deletions packages/openapi-fetch/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,22 @@ export interface MiddlewareCallbackParams {
readonly options: MergedOptions;
}

export interface Middleware {
onRequest?: (options: MiddlewareCallbackParams) => void | Request | undefined | Promise<Request | undefined | void>;
onResponse?: (
options: MiddlewareCallbackParams & { response: Response },
) => void | Response | undefined | Promise<Response | undefined | void>;
}
type MiddlewareOnRequest = (
options: MiddlewareCallbackParams,
) => void | Request | undefined | Promise<Request | undefined | void>;
type MiddlewareOnResponse = (
options: MiddlewareCallbackParams & { response: Response },
) => void | Response | undefined | Promise<Response | undefined | void>;

export type Middleware =
| {
onRequest: MiddlewareOnRequest;
onResponse?: MiddlewareOnResponse;
}
| {
onRequest?: MiddlewareOnRequest;
onResponse: MiddlewareOnResponse;
};

/** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */
export type MaybeOptionalInit<Params, Location extends keyof Params> = RequiredKeysOf<
Expand Down
11 changes: 10 additions & 1 deletion packages/openapi-fetch/test/middleware/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test } from "vitest";
import { expect, test, expectTypeOf, assertType } from "vitest";
import { createObservedClient } from "../helpers.js";
import type { Middleware, MiddlewareCallbackParams } from "../../src/index.js";
import type { paths } from "./schemas/middleware.js";
Expand Down Expand Up @@ -354,3 +354,12 @@ test("auth header", async () => {
});
expect(headers.get("authorization")).toBe(`Bearer ${accessToken}`);
});

test("type error occurs only when neither onRequest nor onResponse is specified", async () => {
expectTypeOf<Middleware>().not.toEqualTypeOf({});
const onRequest = async ({ request }: MiddlewareCallbackParams) => request;
const onResponse = async ({ response }: MiddlewareCallbackParams & { response: Response }) => response;
assertType<Middleware>({ onRequest });
assertType<Middleware>({ onResponse });
assertType<Middleware>({ onRequest, onResponse });
});

0 comments on commit e39d11e

Please sign in to comment.