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

allowlist-demo.tsx

61 lines1.8 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: () => fetchTodos(api, 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: () => fetchTodos(todoApi, 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: () => fetchTrackedTodos(tracker, userId),
30 })
32​
33export function todosWithClientOptions(client: ApiClient, userId: string) {
34 // ❌ fails: AnalyticsClient is in allowlist.types, but this param is typed as ApiClient
35 // eslint-disable-next-line @tanstack/query/exhaustive-deps -- The following dependencies are missing in your queryKey: client
36 return queryOptions({
37 queryKey: ['todos', userId],
38 queryFn: () => fetchTodos(client, userId),
39 })
41​
42interface ApiClient {
43 fetchTodos: (userId: string) => Promise<Array<{ id: string }>>
45​
46interface AnalyticsClient {
47 track: (event: string) => Promise<void>
49​
50function useApiClient(): ApiClient {
51 throw new Error('not implemented')
53​
54function fetchTodos(api: ApiClient, userId: string) {
55 return api.fetchTodos(userId)
57​
58async function fetchTrackedTodos(tracker: AnalyticsClient, userId: string) {
59 await tracker.track('todos')
60 return fetch(`/api/todos?userId=${userId}`).then((r) => r.json())