diff --git a/.changeset/hip-years-invite.md b/.changeset/hip-years-invite.md new file mode 100644 index 000000000..4a751730b --- /dev/null +++ b/.changeset/hip-years-invite.md @@ -0,0 +1,5 @@ +--- +"openapi-fetch": patch +--- + +Add MethodResponse utility type to easily get the return type of an endpoint on a client diff --git a/packages/openapi-fetch/src/index.d.ts b/packages/openapi-fetch/src/index.d.ts index b2742e92d..3ba091c69 100644 --- a/packages/openapi-fetch/src/index.d.ts +++ b/packages/openapi-fetch/src/index.d.ts @@ -202,6 +202,20 @@ export interface Client { eject(...middleware: Middleware[]): void; } +export type ClientPathsWithMethod< + CreatedClient extends Client, + Method extends HttpMethod, +> = CreatedClient extends Client ? PathsWithMethod : never; + +export type MethodResponse< + CreatedClient extends Client, + Method extends HttpMethod, + Path extends ClientPathsWithMethod, + Options = {}, +> = CreatedClient extends Client + ? NonNullable["data"]> + : never; + export default function createClient( clientOptions?: ClientOptions, ): Client; diff --git a/packages/openapi-fetch/test/index.test.ts b/packages/openapi-fetch/test/index.test.ts index fe79c4e15..11b9a2a7b 100644 --- a/packages/openapi-fetch/test/index.test.ts +++ b/packages/openapi-fetch/test/index.test.ts @@ -1,6 +1,7 @@ import { HttpResponse, type StrictResponse } from "msw"; import { afterAll, beforeAll, describe, expect, expectTypeOf, it } from "vitest"; import createClient, { + type MethodResponse, type Middleware, type MiddlewareCallbackParams, type QuerySerializerOptions, @@ -133,6 +134,7 @@ describe("client", () => { created_at: number; updated_at: number; }>(); + expectTypeOf(result.data).toEqualTypeOf>(); } else { expectTypeOf(result.data).toBeUndefined(); expectTypeOf(result.error).extract<{ code: number }>().toEqualTypeOf<{ code: number; message: string }>();