Skip to content

Commit 1c51923

Browse files
brolnickijposva
andauthored
feat(query): add overloaded for optional define options params (#386)
Co-authored-by: Eduardo San Martin Morote <[email protected]>
1 parent 829c3ca commit 1c51923

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/define-query-options.test-d.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,62 @@ describe('typed query keys', () => {
256256
// allows non typed too with loose types
257257
queryCache.setQueryData(DOCUMENTS_KEYS.byId('1'), { toto: true })
258258
})
259+
260+
it('supports optional params as extra query modifiers', () => {
261+
const documentsListQuery = defineQueryOptions(
262+
({ page = 1, withComments = false }: { page?: number; withComments?: boolean } = {}) => ({
263+
key: ['documents', page, { comments: withComments }],
264+
query: async () => ({
265+
page,
266+
withComments,
267+
list: [] as { id: string; title: string; hasComments: boolean }[],
268+
}),
269+
}),
270+
)
271+
272+
const r0 = useQuery(documentsListQuery())
273+
expectTypeOf(r0.data.value).toEqualTypeOf<
274+
| {
275+
page: number
276+
withComments: boolean
277+
list: { id: string; title: string; hasComments: boolean }[]
278+
}
279+
| undefined
280+
>()
281+
282+
// can also be not called
283+
const r1 = useQuery(documentsListQuery)
284+
expectTypeOf(r1).toEqualTypeOf(r0)
285+
286+
const r2 = useQuery(documentsListQuery, () => ({ page: 2, withComments: true }))
287+
expectTypeOf(r2.data.value).toEqualTypeOf<
288+
| {
289+
page: number
290+
withComments: boolean
291+
list: { id: string; title: string; hasComments: boolean }[]
292+
}
293+
| undefined
294+
>()
295+
296+
const queryCache = useQueryCache()
297+
expectTypeOf(
298+
queryCache.getQueryData(documentsListQuery({ page: 3, withComments: true }).key),
299+
).toEqualTypeOf<
300+
| {
301+
page: number
302+
withComments: boolean
303+
list: { id: string; title: string; hasComments: boolean }[]
304+
}
305+
| undefined
306+
>()
307+
expectTypeOf(queryCache.getQueryData(documentsListQuery().key)).toEqualTypeOf<
308+
| {
309+
page: number
310+
withComments: boolean
311+
list: { id: string; title: string; hasComments: boolean }[]
312+
}
313+
| undefined
314+
>()
315+
})
259316
})
260317
})

src/define-query-options.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ export interface DefineQueryOptionsTagged<
1515
key: EntryKeyTagged<TData, TError, TDataInitial>
1616
}
1717

18+
/**
19+
* Define dynamic query options by passing a function that accepts an optional
20+
* arbitrary parameter and returns the query options. Enables type-safe query
21+
* keys. Must be passed to {@link useQuery} with or without a getter to
22+
* retrieve the params.
23+
*
24+
* @param setupOptions - A function that returns the query options.
25+
*/
26+
export function defineQueryOptions<
27+
Params,
28+
TData,
29+
TError = ErrorDefault,
30+
TDataInitial extends TData | undefined = undefined,
31+
>(
32+
setupOptions: (params?: Params) => DefineQueryOptions<TData, TError, TDataInitial>,
33+
): (params?: Params) => DefineQueryOptionsTagged<TData, TError, TDataInitial>
34+
1835
/**
1936
* Define dynamic query options by passing a function that accepts an arbitrary
2037
* parameter and returns the query options. Enables type-safe query keys.

0 commit comments

Comments
 (0)