Skip to content

Commit a97c5b3

Browse files
committed
Better usage
1 parent d5f5af0 commit a97c5b3

File tree

1 file changed

+64
-92
lines changed
  • packages/openapi-react-query/src

1 file changed

+64
-92
lines changed

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

+64-92
Original file line numberDiff line numberDiff line change
@@ -95,37 +95,33 @@ export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>,
9595
: [InitWithUnknowns<Init>, Options?, QueryClient?]
9696
) => UseQueryResult<InferSelectReturnType<Response["data"], Options["select"]>, Response["error"]>;
9797

98-
export type UseInfiniteQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
99-
Method extends HttpMethod,
100-
Path extends PathsWithMethod<Paths, Method>,
101-
Init extends MaybeOptionalInit<Paths[Path], Method>,
102-
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>,
103-
Options extends Omit<
104-
UseInfiniteQueryOptions<
105-
Response["data"],
106-
Response["error"],
107-
InfiniteData<Response["data"]>,
108-
Response["data"],
109-
QueryKey<Paths, Method, Path>,
110-
unknown
111-
>,
112-
"queryKey" | "queryFn"
113-
>
114-
>(
115-
method: Method,
116-
url: Path,
117-
...[init, options, queryClient]: RequiredKeysOf<Init> extends never
118-
? [
119-
InitWithUnknowns<Init>?,
120-
Options?,
121-
QueryClient?,
122-
]
123-
: [
124-
InitWithUnknowns<Init>,
125-
Options?,
126-
QueryClient?,
127-
]
128-
) => UseInfiniteQueryResult<InfiniteData<Response["data"]>, Response["error"]>;
98+
export type UseInfiniteQueryMethod<
99+
Paths extends Record<string, Record<HttpMethod, {}>>,
100+
Media extends MediaType
101+
> =
102+
(<
103+
Method extends HttpMethod,
104+
Path extends PathsWithMethod<Paths, Method>,
105+
Init extends MaybeOptionalInit<Paths[Path], Method>,
106+
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>,
107+
Options extends Omit<
108+
UseInfiniteQueryOptions<
109+
Response["data"],
110+
Response["error"],
111+
InfiniteData<Response["data"]>,
112+
Response["data"],
113+
QueryKey<Paths, Method, Path>,
114+
unknown
115+
>,
116+
"queryKey" | "queryFn"
117+
>
118+
>(
119+
method: Method,
120+
url: Path,
121+
init: InitWithUnknowns<Init>,
122+
options: Options,
123+
queryClient?: QueryClient
124+
) => UseInfiniteQueryResult<InfiniteData<Response["data"]>, Response["error"]>);
129125

130126
export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
131127
Method extends HttpMethod,
@@ -194,73 +190,48 @@ export default function createClient<Paths extends {}, Media extends MediaType =
194190
...options,
195191
});
196192

197-
const infiniteQueryOptions = <
198-
Method extends HttpMethod,
199-
Path extends PathsWithMethod<Paths, Method>,
200-
Init extends MaybeOptionalInit<Paths[Path], Method & keyof Paths[Path]>,
201-
Response extends Required<FetchResponse<any, Init, Media>>,
202-
>(
203-
method: Method,
204-
path: Path,
205-
init?: InitWithUnknowns<Init>,
206-
options?: Omit<
207-
UseInfiniteQueryOptions<
208-
Response["data"],
209-
Response["error"],
210-
Response["data"],
211-
Response["data"],
212-
QueryKey<Paths, Method, Path>,
213-
unknown
214-
>,
215-
"queryKey" | "queryFn"
216-
>,
217-
) => ({
218-
queryKey: [method, path, init] as const,
219-
queryFn: async ({
220-
queryKey: [method, path, init],
221-
pageParam = 0,
222-
signal,
223-
}: QueryFunctionContext<QueryKey<Paths, Method, Path>, number>) => {
224-
const mth = method.toUpperCase() as Uppercase<typeof method>;
225-
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
226-
const mergedInit = {
227-
...init,
228-
signal,
229-
params: {
230-
...(init?.params || {}),
231-
query: {
232-
...(init?.params as { query?: DefaultParamsOption })?.query,
233-
cursor: pageParam,
234-
},
235-
},
236-
};
237-
238-
const { data, error } = await fn(path, mergedInit as any);
239-
if (error) {
240-
throw error;
241-
}
242-
return data;
243-
},
244-
...options,
245-
});
246-
247193
return {
248194
queryOptions,
249195
useQuery: (method, path, ...[init, options, queryClient]) =>
250196
useQuery(queryOptions(method, path, init as InitWithUnknowns<typeof init>, options), queryClient),
251197
useSuspenseQuery: (method, path, ...[init, options, queryClient]) =>
252198
useSuspenseQuery(queryOptions(method, path, init as InitWithUnknowns<typeof init>, options), queryClient),
253-
useInfiniteQuery: (method, path, ...[init, options, queryClient]) => {
254-
const baseOptions = infiniteQueryOptions(method, path, init as InitWithUnknowns<typeof init>, options as any); // TODO: find a way to avoid as any
255-
return useInfiniteQuery(
256-
{
257-
...baseOptions,
258-
getNextPageParam: (lastPage: any, allPages: any[], lastPageParam: number, allPageParams: number[]) =>
259-
options?.getNextPageParam?.(lastPage, allPages, lastPageParam, allPageParams) ?? allPages.length,
260-
} as any,
261-
queryClient,
262-
);
263-
},
199+
useInfiniteQuery: (method, path, init, options, queryClient) =>
200+
useInfiniteQuery(
201+
{
202+
queryKey: [method, path, init] as const,
203+
queryFn: async <
204+
Method extends HttpMethod,
205+
Path extends PathsWithMethod<Paths, Method>,
206+
>({
207+
queryKey: [method, path, init],
208+
pageParam = 0,
209+
signal,
210+
}: QueryFunctionContext<QueryKey<Paths, Method, Path>, unknown>) => {
211+
const mth = method.toUpperCase() as Uppercase<typeof method>;
212+
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
213+
const mergedInit = {
214+
...init,
215+
signal,
216+
params: {
217+
...(init?.params || {}),
218+
query: {
219+
...(init?.params as { query?: DefaultParamsOption })?.query,
220+
cursor: pageParam,
221+
},
222+
},
223+
};
224+
225+
const { data, error } = await fn(path, mergedInit as any);
226+
if (error) {
227+
throw error;
228+
}
229+
return data;
230+
},
231+
...options,
232+
},
233+
queryClient,
234+
),
264235
useMutation: (method, path, options, queryClient) =>
265236
useMutation(
266237
{
@@ -281,3 +252,4 @@ export default function createClient<Paths extends {}, Media extends MediaType =
281252
),
282253
};
283254
}
255+

0 commit comments

Comments
 (0)