Skip to content

Commit d1fa423

Browse files
committed
Working function
1 parent ccbc2e3 commit d1fa423

File tree

1 file changed

+96
-73
lines changed
  • packages/openapi-react-query/src

1 file changed

+96
-73
lines changed

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

Lines changed: 96 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import {
33
type UseMutationResult,
44
type UseQueryOptions,
55
type UseQueryResult,
6-
type UseSuspenseQueryOptions,
7-
type UseSuspenseQueryResult,
86
type UseInfiniteQueryOptions,
97
type UseInfiniteQueryResult,
8+
type UseSuspenseQueryOptions,
9+
type UseSuspenseQueryResult,
1010
type QueryClient,
1111
type QueryFunctionContext,
1212
type SkipToken,
@@ -15,7 +15,7 @@ import {
1515
useSuspenseQuery,
1616
useInfiniteQuery,
1717
} from "@tanstack/react-query";
18-
import type { ClientMethod, FetchResponse, MaybeOptionalInit, Client as FetchClient } from "openapi-fetch";
18+
import type { ClientMethod, FetchResponse, MaybeOptionalInit, Client as FetchClient, DefaultParamsOption } from "openapi-fetch";
1919
import type { HttpMethod, MediaType, PathsWithMethod, RequiredKeysOf } from "openapi-typescript-helpers";
2020

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

96+
export type UseInfiniteQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
97+
Method extends HttpMethod,
98+
Path extends PathsWithMethod<Paths, Method>,
99+
Init extends MaybeOptionalInit<Paths[Path], Method>,
100+
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>,
101+
>(
102+
method: Method,
103+
url: Path,
104+
...[init, options, queryClient]: RequiredKeysOf<Init> extends never
105+
? [
106+
InitWithUnknowns<Init>?,
107+
Omit<
108+
UseInfiniteQueryOptions<
109+
Response["data"],
110+
Response["error"],
111+
Response["data"],
112+
number,
113+
QueryKey<Paths, Method, Path>
114+
>,
115+
"queryKey" | "queryFn"
116+
>?,
117+
QueryClient?,
118+
]
119+
: [
120+
InitWithUnknowns<Init>,
121+
Omit<
122+
UseInfiniteQueryOptions<
123+
Response["data"],
124+
Response["error"],
125+
Response["data"],
126+
number,
127+
QueryKey<Paths, Method, Path>
128+
>,
129+
"queryKey" | "queryFn"
130+
>?,
131+
QueryClient?,
132+
]
133+
) => UseInfiniteQueryResult<Response["data"], Response["error"]>;
134+
96135
export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
97136
Method extends HttpMethod,
98137
Path extends PathsWithMethod<Paths, Method>,
@@ -115,45 +154,6 @@ export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMetho
115154
: [InitWithUnknowns<Init>, Options?, QueryClient?]
116155
) => UseSuspenseQueryResult<InferSelectReturnType<Response["data"], Options["select"]>, Response["error"]>;
117156

118-
export type UseInfiniteQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
119-
Method extends HttpMethod,
120-
Path extends PathsWithMethod<Paths, Method>,
121-
Init extends MaybeOptionalInit<Paths[Path], Method>,
122-
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>,
123-
>(
124-
method: Method,
125-
url: Path,
126-
...[init, options, queryClient]: RequiredKeysOf<Init> extends never
127-
? [
128-
InitWithUnknowns<Init>?,
129-
Omit<
130-
UseInfiniteQueryOptions<
131-
Response["data"],
132-
Response["error"],
133-
Response["data"],
134-
number,
135-
QueryKey<Paths, Method, Path>
136-
>,
137-
"queryKey" | "queryFn"
138-
>?,
139-
QueryClient?,
140-
]
141-
: [
142-
InitWithUnknowns<Init>,
143-
Omit<
144-
UseInfiniteQueryOptions<
145-
Response["data"],
146-
Response["error"],
147-
Response["data"],
148-
number,
149-
QueryKey<Paths, Method, Path>
150-
>,
151-
"queryKey" | "queryFn"
152-
>?,
153-
QueryClient?,
154-
]
155-
) => UseInfiniteQueryResult<Response["data"], Response["error"]>;
156-
157157
export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
158158
Method extends HttpMethod,
159159
Path extends PathsWithMethod<Paths, Method>,
@@ -199,30 +199,54 @@ export default function createClient<Paths extends {}, Media extends MediaType =
199199
...options,
200200
});
201201

202-
const infiniteQueryOptions = <
203-
Method extends HttpMethod,
204-
Path extends PathsWithMethod<Paths, Method>,
205-
Init extends MaybeOptionalInit<Paths[Path], Method & keyof Paths[Path]>,
206-
Response extends Required<FetchResponse<any, Init, Media>>,
207-
>(
208-
method: Method,
209-
path: Path,
210-
init?: InitWithUnknowns<Init>,
211-
options?: Omit<
212-
UseInfiniteQueryOptions<
213-
Response["data"],
214-
Response["error"],
215-
Response["data"],
216-
number,
217-
QueryKey<Paths, Method, Path>
218-
>,
219-
"queryKey" | "queryFn"
220-
>,
221-
) => ({
222-
queryKey: [method, path, init] as const,
223-
queryFn,
224-
...options,
225-
});
202+
const infiniteQueryOptions = <
203+
Method extends HttpMethod,
204+
Path extends PathsWithMethod<Paths, Method>,
205+
Init extends MaybeOptionalInit<Paths[Path], Method & keyof Paths[Path]>,
206+
Response extends Required<FetchResponse<any, Init, Media>>,
207+
>(
208+
method: Method,
209+
path: Path,
210+
init?: InitWithUnknowns<Init>,
211+
options?: Omit<
212+
UseInfiniteQueryOptions<
213+
Response["data"],
214+
Response["error"],
215+
Response["data"],
216+
number,
217+
QueryKey<Paths, Method, Path>
218+
>,
219+
"queryKey" | "queryFn"
220+
>,
221+
) => ({
222+
queryKey: [method, path, init] as const,
223+
queryFn: async ({
224+
queryKey: [method, path, init],
225+
pageParam = 0,
226+
signal,
227+
}: QueryFunctionContext<QueryKey<Paths, Method, Path>, number>) => {
228+
const mth = method.toUpperCase() as Uppercase<typeof method>;
229+
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
230+
const mergedInit = {
231+
...init,
232+
signal,
233+
params: {
234+
...(init?.params || {}),
235+
query: {
236+
...(init?.params as { query?: DefaultParamsOption })?.query,
237+
cursor: pageParam,
238+
},
239+
},
240+
};
241+
242+
const { data, error } = await fn(path, mergedInit as any);
243+
if (error) {
244+
throw error;
245+
}
246+
return data;
247+
},
248+
...options,
249+
});
226250

227251
return {
228252
queryOptions,
@@ -233,13 +257,12 @@ export default function createClient<Paths extends {}, Media extends MediaType =
233257
useInfiniteQuery: (method, path, ...[init, options, queryClient]) => {
234258
const baseOptions = infiniteQueryOptions(method, path, init as InitWithUnknowns<typeof init>, options as any); // TODO: find a way to avoid as any
235259
return useInfiniteQuery(
236-
{
237-
...baseOptions,
238-
initialPageParam: 0,
239-
getNextPageParam: (lastPage: any, allPages: any[], lastPageParam: number, allPageParams: number[]) =>
240-
options?.getNextPageParam?.(lastPage, allPages, lastPageParam, allPageParams) ?? allPages.length,
241-
} as any,
242-
queryClient,
260+
{
261+
...baseOptions,
262+
getNextPageParam: (lastPage: any, allPages: any[], lastPageParam: number, allPageParams: number[]) =>
263+
options?.getNextPageParam?.(lastPage, allPages, lastPageParam, allPageParams) ?? allPages.length,
264+
} as any,
265+
queryClient,
243266
);
244267
},
245268
useMutation: (method, path, options, queryClient) =>

0 commit comments

Comments
 (0)