Skip to content

Commit 248195d

Browse files
authored
add MethodResponse for openapi-react-query (#1945)
* add MethodResponse for openapi-react-query * improve Path param construction
1 parent ccbc2e3 commit 248195d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

packages/openapi-react-query/src/index.ts

+11
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ export interface OpenapiQueryClient<Paths extends {}, Media extends MediaType =
175175
useMutation: UseMutationMethod<Paths, Media>;
176176
}
177177

178+
export type MethodResponse<
179+
CreatedClient extends OpenapiQueryClient<any, any>,
180+
Method extends HttpMethod,
181+
Path extends CreatedClient extends OpenapiQueryClient<infer Paths, infer _Media>
182+
? PathsWithMethod<Paths, Method>
183+
: never,
184+
Options = object,
185+
> = CreatedClient extends OpenapiQueryClient<infer Paths extends { [key: string]: any }, infer Media extends MediaType>
186+
? NonNullable<FetchResponse<Paths[Path][Method], Options, Media>["data"]>
187+
: never;
188+
178189
// TODO: Add the ability to bring queryClient as argument
179190
export default function createClient<Paths extends {}, Media extends MediaType = MediaType>(
180191
client: FetchClient<Paths, Media>,

packages/openapi-react-query/test/index.test.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { afterAll, beforeAll, describe, expect, it } from "vitest";
22
import { server, baseUrl, useMockRequestHandler } from "./fixtures/mock-server.js";
33
import type { paths } from "./fixtures/api.js";
4-
import createClient from "../src/index.js";
4+
import createClient, { type MethodResponse } from "../src/index.js";
55
import createFetchClient from "openapi-fetch";
66
import { act, fireEvent, render, renderHook, screen, waitFor } from "@testing-library/react";
77
import {
@@ -337,6 +337,7 @@ describe("client", () => {
337337

338338
const { data, error } = result.current;
339339

340+
expectTypeOf(data).toEqualTypeOf<MethodResponse<typeof client, "get", "/string-array"> | undefined>();
340341
expectTypeOf(data).toEqualTypeOf<string[] | undefined>();
341342
expectTypeOf(error).toEqualTypeOf<{ code: number; message: string } | null>();
342343
});

0 commit comments

Comments
 (0)