MCPcopy Create free account
hub / github.com/TanStack/query / fetchCharacters

Function fetchCharacters

examples/solid/batching/src/services/characters/api.ts:10–27  ·  view source on GitHub ↗
(ids: ReadonlyArray<number>)

Source from the content-addressed store, hash-verified

8
9// fetch function that returns characters by id
10const fetchCharacters = async (ids: ReadonlyArray<number>) => {
11 console.log(`Fetching characters: ${ids.join(', ')}`)
12
13 const res = await fetch(
14 `https://rickandmortyapi.com/api/character/[${ids.join(',')}]`,
15 )
16 if (!res.ok) throw new Error('Failed to fetch characters')
17 const characters = (await res.json()) as Array<CharacterT> | CharacterT
18
19 const characterMap = new Map<number, CharacterT>()
20 for (const character of Array.isArray(characters)
21 ? characters
22 : [characters]) {
23 characterMap.set(character.id, character)
24 }
25
26 return characterMap
27}
28
29const characterBatcher = new AsyncBatcher<CharacterRequest>(
30 (requests) => fetchCharacters([...new Set(requests.map(({ id }) => id))]),

Callers 1

api.tsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected