Skip to content

feat(openapi-react-query): support for useInfiniteQuery() #2117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mighty-comics-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-react-query": minor
---

Implements useInfiniteQuery() in openapi-react-query
141 changes: 67 additions & 74 deletions packages/openapi-react-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import {
type UseMutationResult,
type UseQueryOptions,
type UseQueryResult,
type UseSuspenseQueryOptions,
type UseSuspenseQueryResult,
type InfiniteData,
type UseInfiniteQueryOptions,
type UseInfiniteQueryResult,
type UseSuspenseQueryOptions,
type UseSuspenseQueryResult,
type QueryClient,
type QueryFunctionContext,
type SkipToken,
Expand All @@ -15,7 +16,13 @@ import {
useSuspenseQuery,
useInfiniteQuery,
} from "@tanstack/react-query";
import type { ClientMethod, FetchResponse, MaybeOptionalInit, Client as FetchClient } from "openapi-fetch";
import type {
ClientMethod,
FetchResponse,
MaybeOptionalInit,
Client as FetchClient,
DefaultParamsOption,
} from "openapi-fetch";
import type { HttpMethod, MediaType, PathsWithMethod, RequiredKeysOf } from "openapi-typescript-helpers";

// Helper type to dynamically infer the type from the `select` property
Expand Down Expand Up @@ -93,6 +100,32 @@ export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>,
: [InitWithUnknowns<Init>, Options?, QueryClient?]
) => UseQueryResult<InferSelectReturnType<Response["data"], Options["select"]>, Response["error"]>;

export type UseInfiniteQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
Method extends HttpMethod,
Path extends PathsWithMethod<Paths, Method>,
Init extends MaybeOptionalInit<Paths[Path], Method>,
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>,
Options extends Omit<
UseInfiniteQueryOptions<
Response["data"],
Response["error"],
InfiniteData<Response["data"]>,
Response["data"],
QueryKey<Paths, Method, Path>,
unknown
>,
"queryKey" | "queryFn"
> & {
pageParamName?: string;
},
>(
method: Method,
url: Path,
init: InitWithUnknowns<Init>,
options: Options,
queryClient?: QueryClient,
) => UseInfiniteQueryResult<InfiniteData<Response["data"]>, Response["error"]>;

export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
Method extends HttpMethod,
Path extends PathsWithMethod<Paths, Method>,
Expand All @@ -115,45 +148,6 @@ export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMetho
: [InitWithUnknowns<Init>, Options?, QueryClient?]
) => UseSuspenseQueryResult<InferSelectReturnType<Response["data"], Options["select"]>, Response["error"]>;

export type UseInfiniteQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
Method extends HttpMethod,
Path extends PathsWithMethod<Paths, Method>,
Init extends MaybeOptionalInit<Paths[Path], Method>,
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>,
>(
method: Method,
url: Path,
...[init, options, queryClient]: RequiredKeysOf<Init> extends never
? [
InitWithUnknowns<Init>?,
Omit<
UseInfiniteQueryOptions<
Response["data"],
Response["error"],
Response["data"],
number,
QueryKey<Paths, Method, Path>
>,
"queryKey" | "queryFn"
>?,
QueryClient?,
]
: [
InitWithUnknowns<Init>,
Omit<
UseInfiniteQueryOptions<
Response["data"],
Response["error"],
Response["data"],
number,
QueryKey<Paths, Method, Path>
>,
"queryKey" | "queryFn"
>?,
QueryClient?,
]
) => UseInfiniteQueryResult<Response["data"], Response["error"]>;

export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
Method extends HttpMethod,
Path extends PathsWithMethod<Paths, Method>,
Expand Down Expand Up @@ -199,46 +193,45 @@ export default function createClient<Paths extends {}, Media extends MediaType =
...options,
});

const infiniteQueryOptions = <
Method extends HttpMethod,
Path extends PathsWithMethod<Paths, Method>,
Init extends MaybeOptionalInit<Paths[Path], Method & keyof Paths[Path]>,
Response extends Required<FetchResponse<any, Init, Media>>,
>(
method: Method,
path: Path,
init?: InitWithUnknowns<Init>,
options?: Omit<
UseInfiniteQueryOptions<
Response["data"],
Response["error"],
Response["data"],
number,
QueryKey<Paths, Method, Path>
>,
"queryKey" | "queryFn"
>,
) => ({
queryKey: [method, path, init] as const,
queryFn,
...options,
});

return {
queryOptions,
useQuery: (method, path, ...[init, options, queryClient]) =>
useQuery(queryOptions(method, path, init as InitWithUnknowns<typeof init>, options), queryClient),
useSuspenseQuery: (method, path, ...[init, options, queryClient]) =>
useSuspenseQuery(queryOptions(method, path, init as InitWithUnknowns<typeof init>, options), queryClient),
useInfiniteQuery: (method, path, ...[init, options, queryClient]) => {
const baseOptions = infiniteQueryOptions(method, path, init as InitWithUnknowns<typeof init>, options as any); // TODO: find a way to avoid as any
useInfiniteQuery: (method, path, init, options, queryClient) => {
const { pageParamName = "cursor", ...restOptions } = options;

return useInfiniteQuery(
{
...baseOptions,
initialPageParam: 0,
getNextPageParam: (lastPage: any, allPages: any[], lastPageParam: number, allPageParams: number[]) =>
options?.getNextPageParam?.(lastPage, allPages, lastPageParam, allPageParams) ?? allPages.length,
} as any,
queryKey: [method, path, init] as const,
queryFn: async <Method extends HttpMethod, Path extends PathsWithMethod<Paths, Method>>({
queryKey: [method, path, init],
pageParam = 0,
signal,
}: QueryFunctionContext<QueryKey<Paths, Method, Path>, unknown>) => {
const mth = method.toUpperCase() as Uppercase<typeof method>;
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
const mergedInit = {
...init,
signal,
params: {
...(init?.params || {}),
query: {
...(init?.params as { query?: DefaultParamsOption })?.query,
[pageParamName]: pageParam,
},
},
};

const { data, error } = await fn(path, mergedInit as any);
if (error) {
throw error;
}
return data;
},
...restOptions,
},
queryClient,
);
},
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-react-query/test/fixtures/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface paths {
parameters: {
query: {
limit: number;
cursor?: number;
};
header?: never;
path?: never;
Expand Down
5 changes: 5 additions & 0 deletions packages/openapi-react-query/test/fixtures/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ paths:
required: true
schema:
type: integer
- in: query
name: cursor
required: false
schema:
type: integer
responses:
'200':
description: Successful response
Expand Down
Loading
Loading