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

api.ts

36 lines940 Bytes

1import type {
2 MovieResponse,
3 MoviesResponse,
4 UpdateMovieResponse,
5} from './types'
6​
7export const moviesAPI = {
8 getMovie: async (id: string): Promise<MovieResponse> => {
9 const response = await fetch(`/movies/${id}`)
10 if (!response.ok) {
11 throw new Error(await response.text())
12 }
13 return await response.json()
14 },
15 getMovies: async (): Promise<MoviesResponse> => {
16 const response = await fetch('/movies')
17 if (!response.ok) {
18 throw new Error(await response.text())
19 }
20 return await response.json()
21 },
22 updateMovie: async (
23 id: string,
24 comment: string,
25 ): Promise<UpdateMovieResponse> => {
26 const response = await fetch(`/movies/${id}`, {
27 method: 'POST',
28 body: JSON.stringify({ comment }),
29 headers: { 'Content-Type': 'application/json' },
30 })
31 if (!response.ok) {
32 throw new Error(await response.text())
33 }
34 return await response.json()
35 },