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

api.ts

29 lines1012 Bytes

1export const getFilms = async () => {
2 const res = await fetch('https://swapi.dev/api/films/')
3 if (!res.ok) throw new Error('Failed to fetch films')
4 return await res.json()
5}
6​
7export const getFilm = async (filmId: string) => {
8 const res = await fetch(`https://swapi.dev/api/films/${filmId}/`)
9 if (!res.ok) throw new Error('Failed to fetch film')
10 return await res.json()
12​
13export const getCharacters = async () => {
14 const res = await fetch('https://swapi.dev/api/people/')
15 if (!res.ok) throw new Error('Failed to fetch characters')
16 return await res.json()
18​
19export const getCharacter = async (characterId: string) => {
20 const res = await fetch(`https://swapi.dev/api/people/${characterId}/`)
21 if (!res.ok) throw new Error('Failed to fetch character')
22 return await res.json()
24​
25export const getPlanet = async (planetId: string) => {
26 const res = await fetch(`https://swapi.dev/api/planets/${planetId}/`)
27 if (!res.ok) throw new Error('Failed to fetch planet')
28 return await res.json()