-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
124 lines (115 loc) · 3.07 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { QueryFunction } from 'react-query';
const API_KEY = 'dfb5edcf6eba00b098c29320ec3ac4b7';
const BASE_URL = 'https://api.themoviedb.org/3/';
const options = {
method: 'GET',
headers: {
accept: 'application/json',
Authorization:
'Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJkZmI1ZWRjZjZlYmEwMGIwOThjMjkzMjBlYzNhYzRiNyIsIm5iZiI6MTcxOTMxMzk2My41NDkwOTcsInN1YiI6IjY2N2FhNTNjMDhiODY1ZTAzNDVhMzJlZiIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.Wh92Hh3-T5x04QYqJJTwfV5wdMsvl9eSQodXgOQC5jw'
}
};
export interface TV {
name: string;
original_name: string;
origin_country: string[];
vote_count: number;
backdrop_path: string | null;
vote_average: number;
genre_ids: number[];
id: number;
original_language: string;
overview: string;
poster_path: string | null;
first_air_date: string;
popularity: number;
media_type: string;
}
export interface Movie {
adult: boolean;
backdrop_path: string | null;
id: number;
title: string;
original_language: string;
original_title: string;
overview: string;
poster_path: string | null;
media_typ: string;
genre_ids: number[];
popularity: number;
release_date: string;
video: boolean;
vote_average: number;
vote_count: number;
}
interface BaseResponse {
page: number;
total_results: number;
total_pages: number;
}
export interface MovieResponse extends BaseResponse {
results: Movie[];
}
type MovieListResponse = QueryFunction<MovieResponse>;
interface MovieFetchers {
nowPlaying: MovieListResponse;
upComing: MovieListResponse;
trending: MovieListResponse;
}
export const moviesApi = {
nowPlaying: () =>
fetch(`${BASE_URL}movie/now_playing?language=en-US&page=1`, options).then(
(res) => res.json()
),
upComing: () =>
fetch(`${BASE_URL}movie/upcoming?language=en-US&page=1`, options).then(
(res) => res.json()
),
trending: () =>
fetch(`${BASE_URL}trending/movie/week?language=en-US`, options).then(
(res) => res.json()
),
search: ({ queryKey }) => {
const [_, query] = queryKey;
return fetch(
`${BASE_URL}search/movie?include_adult=true&language=en-US&page=1&query=${query}`,
options
).then((res) => res.json());
},
detail: ({ queryKey }) => {
const [_, query] = queryKey;
return fetch(
`${BASE_URL}movie/${query}?api_key=${API_KEY}&append_to_response=videos`,
options
).then((res) => res.json());
}
};
export const tvApi = {
airingToday: () =>
fetch(`${BASE_URL}tv/airing_today?language=en-US&page=1`, options).then(
(res) => res.json()
),
topRated: () =>
fetch(`${BASE_URL}tv/top_rated?language=en-US&page=1`, options).then(
(res) => res.json()
),
trending: () =>
fetch(`${BASE_URL}trending/tv/week?language=en-US`, options).then((res) =>
res.json()
),
search: ({ queryKey }) => {
const [_, query] = queryKey;
return fetch(
`${BASE_URL}search/tv?include_adult=true&language=en-US&page=1&query=${query}`,
options
).then((res) => res.json());
},
detail: ({ queryKey }) => {
const [_, query] = queryKey;
console.log(queryKey);
return fetch(
`${BASE_URL}tv/${query}?api_key=${API_KEY}&append_to_response=videos`,
options
).then((res) => res.json());
}
};