Open History to see the last commit
1import { keepPreviousData, queryOptions } from '@tanstack/solid-query'
2β
3const MAX_POKEMONS = 100
4β
5type Pokemon = {
6 name: string
7 stats: Array<{ base_stat: number; stat: { name: string } }>
8}
9β
10type PokemonList = {
11 results: Array<{ name: string; url: string }>
12}
13β
14const getPokemon = async (id: string): Promise<Pokemon> => {
15 const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`)
16 if (!response.ok) throw new Error('Failed to fetch pokemon')
17 return await response.json()
18}
19β
20const getPokemons = async (): Promise<PokemonList> => {
21 const response = await fetch(
22 `https://pokeapi.co/api/v2/pokemon?limit=${MAX_POKEMONS}`,
23 )
24 if (!response.ok) throw new Error('Failed to fetch pokemons')
25 return await response.json()
26}
27β
28export const pokemonQueryOptions = (id: string) =>
29 queryOptions({
30 queryKey: ['pokemon', id],
31 queryFn: () => getPokemon(id),
32 placeholderData: keepPreviousData,
33 throwOnError: true,
34 })
35β
36export const pokemonsQueryOptions = queryOptions({
37 queryKey: ['pokemons'],
38 queryFn: getPokemons,
39})