Skip to content

Commit 2b69f32

Browse files
committed
Add useSuspenseQuery impl
1 parent eb39931 commit 2b69f32

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

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

+19-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type UseSuspenseQueryResult,
88
useMutation,
99
useQuery,
10+
useSuspenseQuery,
1011
} from "@tanstack/react-query";
1112
import type { ClientMethod, FetchResponse, MaybeOptionalInit, Client as FetchClient } from "openapi-fetch";
1213
import type { HasRequiredKeys, HttpMethod, MediaType, PathsWithMethod } from "openapi-typescript-helpers";
@@ -53,10 +54,12 @@ export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}
5354

5455
export interface OpenapiQueryClient<Paths extends {}, Media extends MediaType = MediaType> {
5556
useQuery: UseQueryMethod<Paths, Media>;
56-
useSuspenseQery: any;
57+
useSuspenseQuery: UseSuspenseQueryMethod<Paths, Media>;
5758
useMutation: UseMutationMethod<Paths, Media>;
5859
}
5960

61+
// TODO: Move the client[method]() fn outside for reusability
62+
// TODO: Add the ability to bring queryClient as argument
6063
export default function createClient<Paths extends {}, Media extends MediaType = MediaType>(
6164
client: FetchClient<Paths, Media>,
6265
): OpenapiQueryClient<Paths, Media> {
@@ -76,11 +79,25 @@ export default function createClient<Paths extends {}, Media extends MediaType =
7679
...options,
7780
});
7881
},
79-
useSuspenseQery: () => {},
82+
useSuspenseQuery: (method, path, ...[init, options]) => {
83+
return useSuspenseQuery({
84+
queryKey: [method, path, init],
85+
queryFn: async () => {
86+
const mth = method.toUpperCase() as keyof typeof client;
87+
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
88+
const { data, error } = await fn(path, init as any); // TODO: find a way to avoid as any
89+
if (error || !data) {
90+
throw error;
91+
}
92+
return data;
93+
},
94+
});
95+
},
8096
useMutation: (method, path, options) => {
8197
return useMutation({
8298
mutationKey: [method, path],
8399
mutationFn: async (init) => {
100+
// TODO: Put in external fn for reusability
84101
const mth = method.toUpperCase() as keyof typeof client;
85102
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
86103
const { data, error } = await fn(path, init as any); // TODO: find a way to avoid as any

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

+24
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe("client", () => {
3030
const fetchClient = createFetchClient<paths>({ baseUrl });
3131
const client = createClient<paths>(fetchClient);
3232
expect(client).toHaveProperty("useQuery");
33+
expect(client).toHaveProperty("useSuspenseQuery");
3334
expect(client).toHaveProperty("useMutation");
3435
});
3536

@@ -56,6 +57,29 @@ describe("client", () => {
5657
});
5758
});
5859

60+
describe("useSuspenseQuery", () => {
61+
it("should work", async () => {
62+
const fetchClient = createFetchClient<paths>({ baseUrl });
63+
const client = createClient(fetchClient);
64+
65+
useMockRequestHandler({
66+
baseUrl,
67+
method: "get",
68+
path: "/self",
69+
status: 200,
70+
body: { message: "OK" },
71+
});
72+
73+
const { result } = renderHook(() => client.useSuspenseQuery("get", "/self"), {
74+
wrapper,
75+
});
76+
77+
await waitFor(() => expect(result.current.isSuccess).toBe(true));
78+
79+
expect(result.current.data).toEqual({ message: "OK" });
80+
});
81+
});
82+
5983
describe("useMutation", () => {
6084
it("should work", async () => {
6185
const fetchClient = createFetchClient<paths>({ baseUrl });

0 commit comments

Comments
 (0)