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

api.ts

21 lines677 Bytes

1import { infiniteQueryOptions } from '@tanstack/react-query'
2​
3const fetchProjects = async (
4 cursor: number,
5): Promise<{
6 data: Array<{ name: string; id: number }>
7 previousId: number | null
8 nextId: number | null
9}> => {
10 const response = await fetch(`/api/projects?cursor=${cursor}`)
11 if (!response.ok) throw new Error('Failed to fetch projects')
12 return await response.json()
14​
15export const projectsInfiniteQueryOptions = infiniteQueryOptions({
16 queryKey: ['projects'],
17 queryFn: ({ pageParam }) => fetchProjects(pageParam),
18 initialPageParam: 0,
19 getPreviousPageParam: (firstPage) => firstPage.previousId,
20 getNextPageParam: (lastPage) => lastPage.nextId,
21})