From a88ee19436e70d866806be7ff90171c21909e189 Mon Sep 17 00:00:00 2001 From: Tobias Schlatter Date: Mon, 12 Aug 2024 09:14:07 +0200 Subject: [PATCH] fix: allow use of `PathBasedClient` with generated `paths` To achieve this, we remove unnecessary type bounds. This fixes #1840. --- .changeset/little-knives-design.md | 5 +++++ packages/openapi-fetch/src/index.d.ts | 9 +++------ packages/openapi-fetch/test/index.test.ts | 12 ++++++++++++ 3 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 .changeset/little-knives-design.md 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, Location extends keyof Params> = RequiredKeysOf< +export type MaybeOptionalInit = RequiredKeysOf< FetchOptions> > extends never ? FetchOptions> | undefined @@ -174,7 +174,7 @@ export type ClientMethod< ...init: InitParam ) => Promise>; -export type ClientForPath, Media extends MediaType> = { +export type ClientForPath = { [Method in keyof PathInfo as Uppercase]: >( ...init: InitParam ) => Promise>; @@ -221,10 +221,7 @@ export default function createClient; -export type PathBasedClient< - Paths extends Record>, - Media extends MediaType = MediaType, -> = { +export type PathBasedClient = { [Path in keyof Paths]: ClientForPath; }; 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({ baseUrl }); + expectTypeOf(client).toEqualTypeOf>(); + }); + 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({ baseUrl }); + expectTypeOf(client).toEqualTypeOf>(); + }); + it("performs a call without params", async () => { const client = createPathBasedClient({ baseUrl });