Skip to content

Commit dc92b42

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 f66be91 commit dc92b42

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

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

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

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

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

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

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 Client 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)