Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

allowlist-demo.tsx

55 lines1.7 KB

1import { queryOptions } from '@tanstack/react-query'
2​
3export function todosOptions(userId: string) {
4 const api = useApiClient()
5 return queryOptions({
6 // βœ… passes: 'api' is in allowlist.variables
7 queryKey: ['todos', userId],
8 queryFn: () => api.fetchTodos(userId),
9 })
11​
12export function todosByApiOptions(userId: string) {
13 const todoApi = useApiClient()
14 // ❌ fails: 'api' is in allowlist.variables, but this variable is named 'todoApi'
15 // eslint-disable-next-line @tanstack/query/exhaustive-deps -- The following dependencies are missing in your queryKey: todoApi
16 return queryOptions({
17 queryKey: ['todos', userId],
18 queryFn: () => todoApi.fetchTodos(userId),
19 })
21​
22export function todosWithTrackingOptions(
23 tracker: AnalyticsClient,
24 userId: string,
25) {
26 return queryOptions({
27 // βœ… passes: AnalyticsClient is in allowlist.types
28 queryKey: ['todos', userId],
29 queryFn: async () => {
30 tracker.track('todos')
31 return fetch(`/api/todos?userId=${userId}`).then((r) => r.json())
32 },
33 })
35​
36export function todosWithClientOptions(client: ApiClient, userId: string) {
37 // ❌ fails: AnalyticsClient is in allowlist.types, but this param is typed as ApiClient
38 // eslint-disable-next-line @tanstack/query/exhaustive-deps -- The following dependencies are missing in your queryKey: client
39 return queryOptions({
40 queryKey: ['todos', userId],
41 queryFn: () => client.fetchTodos(userId),
42 })
44​
45interface ApiClient {
46 fetchTodos: (userId: string) => Promise<Array<{ id: string }>>
48​
49interface AnalyticsClient {
50 track: (event: string) => Promise<void>
52​
53function useApiClient(): ApiClient {
54 throw new Error('not implemented')