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

api.ts

38 lines979 Bytes

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