Skip to content

Commit a88ee19

Browse files
committed
fix: allow use of PathBasedClient with generated paths
To achieve this, we remove unnecessary type bounds. This fixes openapi-ts#1840.
1 parent eb5307f commit a88ee19

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

.changeset/little-knives-design.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-fetch": patch
3+
---
4+
5+
fix: allow use of `PathBasedClient` with generated `paths`

packages/openapi-fetch/src/index.d.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export interface Middleware {
151151
}
152152

153153
/** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */
154-
export type MaybeOptionalInit<Params extends Record<HttpMethod, {}>, Location extends keyof Params> = RequiredKeysOf<
154+
export type MaybeOptionalInit<Params, Location extends keyof Params> = RequiredKeysOf<
155155
FetchOptions<FilterKeys<Params, Location>>
156156
> extends never
157157
? FetchOptions<FilterKeys<Params, Location>> | undefined
@@ -174,7 +174,7 @@ export type ClientMethod<
174174
...init: InitParam<Init>
175175
) => Promise<FetchResponse<Paths[Path][Method], Init, Media>>;
176176

177-
export type ClientForPath<PathInfo extends Record<HttpMethod, {}>, Media extends MediaType> = {
177+
export type ClientForPath<PathInfo, Media extends MediaType> = {
178178
[Method in keyof PathInfo as Uppercase<string & Method>]: <Init extends MaybeOptionalInit<PathInfo, Method>>(
179179
...init: InitParam<Init>
180180
) => Promise<FetchResponse<PathInfo[Method], Init, Media>>;
@@ -221,10 +221,7 @@ export default function createClient<Paths extends {}, Media extends MediaType =
221221
clientOptions?: ClientOptions,
222222
): Client<Paths, Media>;
223223

224-
export type PathBasedClient<
225-
Paths extends Record<string, Record<HttpMethod, {}>>,
226-
Media extends MediaType = MediaType,
227-
> = {
224+
export type PathBasedClient<Paths, Media extends MediaType = MediaType> = {
228225
[Path in keyof Paths]: ClientForPath<Paths[Path], Media>;
229226
};
230227

packages/openapi-fetch/test/index.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import createClient, {
55
type Middleware,
66
type MiddlewareCallbackParams,
77
type QuerySerializerOptions,
8+
type Client,
9+
type PathBasedClient,
810
createPathBasedClient,
911
} from "../src/index.js";
1012
import { server, baseUrl, useMockRequestHandler, toAbsoluteURL } from "./fixtures/mock-server.js";
@@ -142,6 +144,11 @@ describe("client", () => {
142144
}
143145
});
144146

147+
it("provides a Client type", () => {
148+
const client = createClient<paths>({ baseUrl });
149+
expectTypeOf(client).toEqualTypeOf<Client<paths>>();
150+
});
151+
145152
describe("params", () => {
146153
describe("path", () => {
147154
it("typechecks", async () => {
@@ -1875,6 +1882,11 @@ describe("client", () => {
18751882
});
18761883

18771884
describe("path based client", () => {
1885+
it("provides a PathBasedClient type", () => {
1886+
const client = createPathBasedClient<paths>({ baseUrl });
1887+
expectTypeOf(client).toEqualTypeOf<PathBasedClient<paths>>();
1888+
});
1889+
18781890
it("performs a call without params", async () => {
18791891
const client = createPathBasedClient<paths>({ baseUrl });
18801892

0 commit comments

Comments
 (0)