Skip to content

Commit 463b15d

Browse files
committed
Fix and test init param handling
1 parent ee62fd7 commit 463b15d

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,27 @@ export type MaybeOptionalInit<Params extends Record<HttpMethod, {}>, Location ex
156156
? FetchOptions<FilterKeys<Params, Location>> | undefined
157157
: FetchOptions<FilterKeys<Params, Location>>;
158158

159+
// The final init param to accept.
160+
// - Determines if the param is optional or not.
161+
// - Performs arbitrary [key: string] addition.
162+
// Note: the addition It MUST happen after all the inference happens (otherwise TS can’t infer if init is required or not).
163+
type InitParam<Init> = HasRequiredKeys<Init> extends never
164+
? [(Init & { [key: string]: unknown })?]
165+
: [Init & { [key: string]: unknown }];
166+
159167
export type ClientMethod<
160168
Paths extends Record<string, Record<HttpMethod, {}>>,
161169
Method extends HttpMethod,
162170
Media extends MediaType,
163171
> = <Path extends PathsWithMethod<Paths, Method>, Init extends MaybeOptionalInit<Paths[Path], Method>>(
164172
url: Path,
165-
...init: HasRequiredKeys<Init> extends never
166-
? [(Init & { [key: string]: unknown })?] // note: the arbitrary [key: string]: addition MUST happen here after all the inference happens (otherwise TS can’t infer if it’s required or not)
167-
: [Init & { [key: string]: unknown }]
173+
...init: InitParam<Init>
168174
) => Promise<FetchResponse<Paths[Path][Method], Init, Media>>;
169175

170176
export type ClientForPath<PathInfo extends Record<HttpMethod, {}>, Media extends MediaType> = {
171-
[Method in keyof PathInfo as Uppercase<string & Method>]: <
172-
Init extends MaybeOptionalInit<PathInfo, Method>,
173-
>() => Promise<FetchResponse<PathInfo[Method], Init, Media>>;
177+
[Method in keyof PathInfo as Uppercase<string & Method>]: <Init extends MaybeOptionalInit<PathInfo, Method>>(
178+
...init: InitParam<Init>
179+
) => Promise<FetchResponse<PathInfo[Method], Init, Media>>;
174180
};
175181

176182
export type Client<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType = MediaType> = {

packages/openapi-fetch/src/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,7 @@ export default function createClient(clientOptions) {
215215
}
216216

217217
// Assume the property is an URL.
218-
return Object.fromEntries(
219-
methods.map((method) => [method, (init) => coreFetch(property, { ...init, method })]),
220-
);
218+
return Object.fromEntries(methods.map((method) => [method, (init) => coreFetch(property, { ...init, method })]));
221219
},
222220
};
223221

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

+23-1
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,7 @@ describe("client", () => {
18321832
});
18331833

18341834
describe("path style call", () => {
1835-
it("sends the correct method", async () => {
1835+
it("performs a call without params", async () => {
18361836
const client = createClient<paths>({ baseUrl });
18371837
const { getRequest } = useMockRequestHandler({
18381838
baseUrl,
@@ -1842,6 +1842,28 @@ describe("client", () => {
18421842
await client["/anyMethod"].GET();
18431843
expect(getRequest().method).toBe("GET");
18441844
});
1845+
1846+
it("performs a call with params", async () => {
1847+
const client = createClient<paths>({ baseUrl });
1848+
const { getRequestUrl } = useMockRequestHandler({
1849+
baseUrl,
1850+
method: "get",
1851+
path: "/blogposts/:post_id",
1852+
status: 200,
1853+
body: { message: "OK" },
1854+
});
1855+
1856+
await client["/blogposts/{post_id}"].GET({
1857+
// expect error on number instead of string.
1858+
// @ts-expect-error
1859+
params: { path: { post_id: 1234 } },
1860+
});
1861+
1862+
await client["/blogposts/{post_id}"].GET({
1863+
params: { path: { post_id: "1234" } },
1864+
});
1865+
expect(getRequestUrl().pathname).toBe("/blogposts/1234");
1866+
});
18451867
});
18461868
});
18471869

0 commit comments

Comments
 (0)