Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2d7495b

Browse files
committedDec 14, 2024
feat(react-query): add useInfiniteQuery
1 parent 713ea1b commit 2d7495b

File tree

1 file changed

+50
-7
lines changed
  • packages/openapi-react-query/src

1 file changed

+50
-7
lines changed
 

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

+50-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import {
2-
type UseMutationOptions,
3-
type UseMutationResult,
4-
type UseQueryOptions,
5-
type UseQueryResult,
6-
type UseSuspenseQueryOptions,
7-
type UseSuspenseQueryResult,
82
type QueryClient,
93
type QueryFunctionContext,
104
type SkipToken,
5+
useInfiniteQuery,
6+
type UseInfiniteQueryOptions,
7+
type UseInfiniteQueryResult,
118
useMutation,
9+
type UseMutationOptions,
10+
type UseMutationResult,
1211
useQuery,
12+
type UseQueryOptions,
13+
type UseQueryResult,
1314
useSuspenseQuery,
15+
type UseSuspenseQueryOptions,
16+
type UseSuspenseQueryResult
1417
} from "@tanstack/react-query";
15-
import type { ClientMethod, FetchResponse, MaybeOptionalInit, Client as FetchClient } from "openapi-fetch";
18+
import type { ClientMethod, Client as FetchClient, FetchResponse, MaybeOptionalInit } from "openapi-fetch";
1619
import type { HttpMethod, MediaType, PathsWithMethod, RequiredKeysOf } from "openapi-typescript-helpers";
1720

1821
type InitWithUnknowns<Init> = Init & { [key: string]: unknown };
@@ -84,6 +87,19 @@ export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMetho
8487
: [InitWithUnknowns<Init>, Options?, QueryClient?]
8588
) => UseSuspenseQueryResult<Response["data"], Response["error"]>;
8689

90+
export type UseInfiniteQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
91+
Method extends HttpMethod,
92+
Path extends PathsWithMethod<Paths, Method>,
93+
Init extends MaybeOptionalInit<Paths[Path], Method>,
94+
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>,
95+
>(
96+
method: Method,
97+
url: Path,
98+
...[init, options, queryClient]: RequiredKeysOf<Init> extends never
99+
? [InitWithUnknowns<Init>?, Omit<UseInfiniteQueryOptions<Response["data"], Response["error"], Response["data"], number, QueryKey<Paths, Method, Path>>, "queryKey" | "queryFn">?, QueryClient?]
100+
: [InitWithUnknowns<Init>, Omit<UseInfiniteQueryOptions<Response["data"], Response["error"], Response["data"], number, QueryKey<Paths, Method, Path>>, "queryKey" | "queryFn">?, QueryClient?]
101+
) => UseInfiniteQueryResult<Response["data"], Response["error"]>;
102+
87103
export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
88104
Method extends HttpMethod,
89105
Path extends PathsWithMethod<Paths, Method>,
@@ -101,6 +117,7 @@ export interface OpenapiQueryClient<Paths extends {}, Media extends MediaType =
101117
queryOptions: QueryOptionsFunction<Paths, Media>;
102118
useQuery: UseQueryMethod<Paths, Media>;
103119
useSuspenseQuery: UseSuspenseQueryMethod<Paths, Media>;
120+
useInfiniteQuery: UseInfiniteQueryMethod<Paths, Media>;
104121
useMutation: UseMutationMethod<Paths, Media>;
105122
}
106123

@@ -128,12 +145,38 @@ export default function createClient<Paths extends {}, Media extends MediaType =
128145
...options,
129146
});
130147

148+
149+
const infiniteQueryOptions = <
150+
Method extends HttpMethod,
151+
Path extends PathsWithMethod<Paths, Method>,
152+
Init extends MaybeOptionalInit<Paths[Path], Method & keyof Paths[Path]>,
153+
Response extends Required<FetchResponse<any, Init, Media>>,
154+
>(
155+
method: Method,
156+
path: Path,
157+
init?: InitWithUnknowns<Init>,
158+
options?: Omit<UseInfiniteQueryOptions<Response["data"], Response["error"], Response["data"], number, QueryKey<Paths, Method, Path>>, "queryKey" | "queryFn">
159+
) => ({
160+
queryKey: [method, path, init] as const,
161+
queryFn,
162+
...options,
163+
});
164+
131165
return {
132166
queryOptions,
133167
useQuery: (method, path, ...[init, options, queryClient]) =>
134168
useQuery(queryOptions(method, path, init as InitWithUnknowns<typeof init>, options), queryClient),
135169
useSuspenseQuery: (method, path, ...[init, options, queryClient]) =>
136170
useSuspenseQuery(queryOptions(method, path, init as InitWithUnknowns<typeof init>, options), queryClient),
171+
useInfiniteQuery: (method, path, ...[init, options, queryClient]) => {
172+
const baseOptions = infiniteQueryOptions(method, path, init as InitWithUnknowns<typeof init>, options as any); // TODO: find a way to avoid as any
173+
return useInfiniteQuery({
174+
...baseOptions,
175+
initialPageParam: 0,
176+
getNextPageParam: (lastPage: any, allPages: any[], lastPageParam: number, allPageParams: number[]) =>
177+
options?.getNextPageParam?.(lastPage, allPages, lastPageParam, allPageParams) ?? allPages.length,
178+
} as any, queryClient);
179+
},
137180
useMutation: (method, path, options, queryClient) =>
138181
useMutation(
139182
{

0 commit comments

Comments
 (0)
Please sign in to comment.