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

queries.ts

26 lines765 Bytes

1import { queryOptions } from '@tanstack/react-query'
2import { contactsAPI } from './api'
3​
4export const contactsQueries = {
5 all: () => ['contacts'] as const,
6 lists: () => [...contactsQueries.all(), 'list'] as const,
7 list: (q?: string) =>
8 queryOptions({
9 queryKey: [...contactsQueries.lists(), { q }] as const,
10 queryFn: () => contactsAPI.getContacts(q),
11 }),
12 detail: (id: string) =>
13 queryOptions({
14 queryKey: [...contactsQueries.all(), 'detail', id] as const,
15 queryFn: async () => {
16 const contact = await contactsAPI.getContact(id)
17 if (!contact) {
18 throw new Response('', {
19 status: 404,
20 statusText: 'Not Found',
21 })
22 }
23 return contact
24 },
25 }),