Skip to content

Commit 1a9f7cf

Browse files
committed
Added explicit types for useQueries() options. Added jsdoc examples.
1 parent 8d001c4 commit 1a9f7cf

File tree

2 files changed

+117
-23
lines changed

2 files changed

+117
-23
lines changed

packages/tanstack-react-query/src/hooks/useQueries.ts

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,48 +31,86 @@ export type EnhancedExplicitQueryResults<T extends readonly unknown[]> = {
3131
[K in keyof T]: Tanstack.UseQueryResult<T[K][]> & { queryKey: Tanstack.QueryKey };
3232
};
3333

34+
export type UseQueriesExplicitWithCombineOptions<T extends readonly unknown[], TCombined> = {
35+
queries: readonly [...{ [K in keyof T]: PowerSyncQueryOption<T[K]> }];
36+
combine: (results: EnhancedExplicitQueryResults<T>) => TCombined;
37+
};
38+
39+
export type UseQueriesExplicitWithoutCombineOptions<T extends readonly unknown[]> = {
40+
queries: readonly [...{ [K in keyof T]: PowerSyncQueryOption<T[K]> }];
41+
combine?: undefined;
42+
};
43+
44+
export type UseQueriesAutoInferenceWithCombineOptions<TQueries extends readonly PowerSyncQueryOption[], TCombined> = {
45+
queries: readonly [...TQueries];
46+
combine: (results: EnhancedInferQueryResults<TQueries>) => TCombined;
47+
};
48+
49+
export type UseQueriesAutoInferenceWithoutCombineOptions<TQueries extends readonly PowerSyncQueryOption[]> = {
50+
queries: readonly [...TQueries];
51+
combine?: undefined;
52+
};
53+
54+
export type UseQueriesBaseOptions = {
55+
queries: readonly (Tanstack.UseQueryOptions & PowerSyncQueryOptions<unknown>)[];
56+
combine?: (results: (Tanstack.UseQueryResult<unknown, unknown> & { queryKey: Tanstack.QueryKey })[]) => unknown;
57+
};
58+
3459
// Explicit generic typing with combine
3560
export function useQueries<T extends readonly unknown[], TCombined>(
36-
options: {
37-
queries: readonly [...{ [K in keyof T]: PowerSyncQueryOption<T[K]> }];
38-
combine: (results: EnhancedExplicitQueryResults<T>) => TCombined;
39-
},
61+
options: UseQueriesExplicitWithCombineOptions<T, TCombined>,
4062
queryClient?: Tanstack.QueryClient
4163
): TCombined;
4264

4365
// Explicit generic typing without combine
4466
export function useQueries<T extends readonly unknown[]>(
45-
options: {
46-
queries: readonly [...{ [K in keyof T]: PowerSyncQueryOption<T[K]> }];
47-
combine?: undefined;
48-
},
67+
options: UseQueriesExplicitWithoutCombineOptions<T>,
4968
queryClient?: Tanstack.QueryClient
5069
): ExplicitQueryResults<T>;
5170

5271
// Auto inference with combine
5372
export function useQueries<TQueries extends readonly PowerSyncQueryOption[], TCombined>(
54-
options: {
55-
queries: readonly [...TQueries];
56-
combine: (results: EnhancedInferQueryResults<TQueries>) => TCombined;
57-
},
73+
options: UseQueriesAutoInferenceWithCombineOptions<TQueries, TCombined>,
5874
queryClient?: Tanstack.QueryClient
5975
): TCombined;
6076

6177
// Auto inference without combine
6278
export function useQueries<TQueries extends readonly PowerSyncQueryOption[]>(
63-
options: {
64-
queries: readonly [...TQueries];
65-
combine?: undefined;
66-
},
79+
options: UseQueriesAutoInferenceWithoutCombineOptions<TQueries>,
6780
queryClient?: Tanstack.QueryClient
6881
): InferQueryResults<TQueries>;
6982

70-
// Implementation
83+
/**
84+
* @example
85+
* ```
86+
* const { data, error, isLoading } = useQueries({
87+
* queries: [
88+
* { queryKey: ['lists'], query: 'SELECT * from lists' },
89+
* { queryKey: ['todos'], query: 'SELECT * from todos' }
90+
* ],
91+
* })
92+
* ```
93+
*
94+
* @example
95+
* ```
96+
* const ids = [1, 2, 3];
97+
* const combinedQueries = useQueries({
98+
* queries: ids.map((id) => ({
99+
* queryKey: ['post', id],
100+
* query: 'SELECT * from lists where id = ?',
101+
* parameters: [id],
102+
* })),
103+
* combine: (results) => {
104+
* return {
105+
* data: results.map((result) => result.data),
106+
* pending: results.some((result) => result.isPending),
107+
* }
108+
* },
109+
* });
110+
* ```
111+
*/
71112
export function useQueries(
72-
options: {
73-
queries: readonly (Tanstack.UseQueryOptions & PowerSyncQueryOptions<unknown>)[];
74-
combine?: (results: (Tanstack.UseQueryResult<unknown, unknown> & { queryKey: Tanstack.QueryKey })[]) => unknown;
75-
},
113+
options: UseQueriesBaseOptions,
76114
queryClient: Tanstack.QueryClient = Tanstack.useQueryClient()
77115
) {
78116
const powerSync = usePowerSync();

packages/tanstack-react-query/src/hooks/useQuery.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,44 @@ export type PowerSyncQueryOptions<T> = {
1010

1111
export type UseBaseQueryOptions<TQueryOptions> = TQueryOptions & PowerSyncQueryOptions<any>;
1212

13+
/**
14+
*
15+
* Uses the `queryFn` to execute the query. No different from the base `useQuery` hook.
16+
*
17+
* @example
18+
* ```
19+
* const { data, error, isLoading } = useQuery({
20+
* queryKey: ['lists'],
21+
* queryFn: getTodos,
22+
* });
23+
* ```
24+
*/
1325
export function useQuery<TData = unknown, TError = Tanstack.DefaultError>(
1426
options: UseBaseQueryOptions<Tanstack.UseQueryOptions<TData, TError>> & { query?: undefined },
1527
queryClient?: Tanstack.QueryClient
1628
): Tanstack.UseQueryResult<TData, TError>;
1729

18-
// Overload when 'query' is present
30+
/**
31+
*
32+
* Uses the `query` to execute the PowerSync query.
33+
*
34+
* @example
35+
* ```
36+
* const { data, error, isLoading } = useQuery({
37+
* queryKey: ['lists'],
38+
* query: 'SELECT * from lists where id = ?',
39+
* parameters: ['id-1']
40+
* });
41+
* ```
42+
*
43+
* @example
44+
* ```
45+
* const { data, error, isLoading } = useQuery({
46+
* queryKey: ['lists'],
47+
* query: compilableQuery,
48+
* });
49+
* ```
50+
*/
1951
export function useQuery<TData = unknown, TError = Tanstack.DefaultError>(
2052
options: UseBaseQueryOptions<Tanstack.UseQueryOptions<TData[], TError>> & { query: string | CompilableQuery<TData> },
2153
queryClient?: Tanstack.QueryClient
@@ -28,12 +60,36 @@ export function useQuery<TData = unknown, TError = Tanstack.DefaultError>(
2860
return useQueryCore(options, queryClient, Tanstack.useQuery);
2961
}
3062

63+
/**
64+
*
65+
* Uses the `queryFn` to execute the query. No different from the base `useSuspenseQuery` hook.
66+
*
67+
* @example
68+
* ```
69+
* const { data } = useSuspenseQuery({
70+
* queryKey: ['lists'],
71+
* queryFn: getTodos,
72+
* });
73+
* ```
74+
*/
3175
export function useSuspenseQuery<TData = unknown, TError = Tanstack.DefaultError>(
3276
options: UseBaseQueryOptions<Tanstack.UseSuspenseQueryOptions<TData, TError>> & { query?: undefined },
3377
queryClient?: Tanstack.QueryClient
3478
): Tanstack.UseSuspenseQueryResult<TData, TError>;
3579

36-
// Overload when 'query' is present
80+
/***
81+
*
82+
* Uses the `query` to execute the PowerSync query.
83+
*
84+
* @example
85+
* ```
86+
* const { data } = useSuspenseQuery({
87+
* queryKey: ['lists'],
88+
* query: 'SELECT * from lists where id = ?',
89+
* parameters: ['id-1']
90+
* });
91+
* ```
92+
*/
3793
export function useSuspenseQuery<TData = unknown, TError = Tanstack.DefaultError>(
3894
options: UseBaseQueryOptions<Tanstack.UseSuspenseQueryOptions<TData[], TError>> & {
3995
query: string | CompilableQuery<TData>;

0 commit comments

Comments
 (0)