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

queries.ts

22 lines681 Bytes

1import { infiniteQueryOptions } from '@tanstack/svelte-query'
2import { planetsAPI } from './api'
3​
4export const planetsQueries = {
5 all: () => ['planets'] as const,
6 list: () =>
7 infiniteQueryOptions({
8 queryKey: [...planetsQueries.all(), 'list'] as const,
9 queryFn: ({ pageParam }) => planetsAPI.getPlanets(pageParam),
10 initialPageParam: 1,
11 getNextPageParam: (lastPage) => {
12 if (lastPage.next) {
13 const nextUrl = new URLSearchParams(new URL(lastPage.next).search)
14 const nextCursor = nextUrl.get('page')
15 if (nextCursor) {
16 return +nextCursor
17 }
18 }
19 return undefined
20 },
21 }),