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

api.ts

35 lines1.1 KB

1export const getEpisodes = async () => {
2 const res = await fetch('https://rickandmortyapi.com/api/episode/')
3 if (!res.ok) throw new Error('Failed to fetch episodes')
4 return await res.json()
5}
6​
7export const getEpisode = async (episodeId: string) => {
8 const res = await fetch(
9 `https://rickandmortyapi.com/api/episode/${episodeId}`,
10 )
11 if (!res.ok) throw new Error('Failed to fetch episode')
12 return await res.json()
14​
15export const getCharacters = async () => {
16 const res = await fetch('https://rickandmortyapi.com/api/character/')
17 if (!res.ok) throw new Error('Failed to fetch characters')
18 return await res.json()
20​
21export const getCharacter = async (characterId: string) => {
22 const res = await fetch(
23 `https://rickandmortyapi.com/api/character/${characterId}`,
24 )
25 if (!res.ok) throw new Error('Failed to fetch character')
26 return await res.json()
28​
29export const getLocation = async (locationId: string) => {
30 const res = await fetch(
31 `https://rickandmortyapi.com/api/location/${locationId}`,
32 )
33 if (!res.ok) throw new Error('Failed to fetch location')
34 return await res.json()