diff --git a/.changeset/little-knives-design.md b/.changeset/little-knives-design.md new file mode 100644 index 000000000..a53e70395 --- /dev/null +++ b/.changeset/little-knives-design.md @@ -0,0 +1,5 @@ +--- +"openapi-fetch": patch +--- + +fix: allow use of `PathBasedClient` with generated `paths` diff --git a/packages/openapi-fetch/src/index.d.ts b/packages/openapi-fetch/src/index.d.ts index ca173c1a2..a7151c706 100644 --- a/packages/openapi-fetch/src/index.d.ts +++ b/packages/openapi-fetch/src/index.d.ts @@ -151,7 +151,7 @@ export interface Middleware { } /** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */ -export type MaybeOptionalInit<Params extends Record<HttpMethod, {}>, Location extends keyof Params> = RequiredKeysOf< +export type MaybeOptionalInit<Params, Location extends keyof Params> = RequiredKeysOf< FetchOptions<FilterKeys<Params, Location>> > extends never ? FetchOptions<FilterKeys<Params, Location>> | undefined @@ -174,7 +174,7 @@ export type ClientMethod< ...init: InitParam<Init> ) => Promise<FetchResponse<Paths[Path][Method], Init, Media>>; -export type ClientForPath<PathInfo extends Record<HttpMethod, {}>, Media extends MediaType> = { +export type ClientForPath<PathInfo, Media extends MediaType> = { [Method in keyof PathInfo as Uppercase<string & Method>]: <Init extends MaybeOptionalInit<PathInfo, Method>>( ...init: InitParam<Init> ) => Promise<FetchResponse<PathInfo[Method], Init, Media>>; @@ -221,10 +221,7 @@ export default function createClient<Paths extends {}, Media extends MediaType = clientOptions?: ClientOptions, ): Client<Paths, Media>; -export type PathBasedClient< - Paths extends Record<string, Record<HttpMethod, {}>>, - Media extends MediaType = MediaType, -> = { +export type PathBasedClient<Paths, Media extends MediaType = MediaType> = { [Path in keyof Paths]: ClientForPath<Paths[Path], Media>; }; diff --git a/packages/openapi-fetch/test/index.test.ts b/packages/openapi-fetch/test/index.test.ts index 11b9a2a7b..2647fb388 100644 --- a/packages/openapi-fetch/test/index.test.ts +++ b/packages/openapi-fetch/test/index.test.ts @@ -5,6 +5,8 @@ import createClient, { type Middleware, type MiddlewareCallbackParams, type QuerySerializerOptions, + type Client, + type PathBasedClient, createPathBasedClient, } from "../src/index.js"; import { server, baseUrl, useMockRequestHandler, toAbsoluteURL } from "./fixtures/mock-server.js"; @@ -142,6 +144,11 @@ describe("client", () => { } }); + it("provides a Client type", () => { + const client = createClient<paths>({ baseUrl }); + expectTypeOf(client).toEqualTypeOf<Client<paths>>(); + }); + describe("params", () => { describe("path", () => { it("typechecks", async () => { @@ -1875,6 +1882,11 @@ describe("client", () => { }); describe("path based client", () => { + it("provides a PathBasedClient type", () => { + const client = createPathBasedClient<paths>({ baseUrl }); + expectTypeOf(client).toEqualTypeOf<PathBasedClient<paths>>(); + }); + it("performs a call without params", async () => { const client = createPathBasedClient<paths>({ baseUrl });