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

api.ts

17 lines652 Bytes

1import type { Contact } from './types'
2​
3export const contactsAPI = (customFetch = fetch) => ({
4 getContacts: async (query: string): Promise<Array<Contact>> => {
5 const response = await customFetch(
6 `/api/contacts?q=${encodeURIComponent(query)}`,
7 )
8 if (!response.ok) throw new Error('Failed to fetch contacts')
9 return await response.json()
10 },
11 getContact: async (id: string): Promise<Contact> => {
12 const response = await customFetch(`/api/contacts/${id}`)
13 if (response.status === 404) throw new Error('Not Found')
14 if (!response.ok) throw new Error('Failed to fetch contact')
15 return await response.json()
16 },
17})