-
Notifications
You must be signed in to change notification settings - Fork 10
/
api.js
47 lines (41 loc) · 1.45 KB
/
api.js
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
import axios from "axios";
const TMDB_KEY = "10923b261ba94d897ac6b81148314a3f";
const makeRequest = (path, params) =>
axios.get(`https://api.themoviedb.org/3${path}`, {
params: {
...params,
api_key: TMDB_KEY
}
});
const getAnything = async (path, params = {}) => {
try {
const {
data: { results },
data
} = await makeRequest(path, params);
return [results || data, null];
} catch (e) {
console.log(e);
return [null, e];
}
};
export const movieApi = {
nowPlaying: () => getAnything("/movie/now_playing"),
popular: () => getAnything("/movie/popular"),
upcoming: () => getAnything("/movie/upcoming", { region: "kr" }),
search: query => getAnything("/search/movie", { query }),
movie: id => getAnything(`/movie/${id}`, { append_to_response: "videos" }),
discover: () => getAnything("/discover/movie")
};
export const tvApi = {
today: () => getAnything("/tv/airing_today"),
thisWeek: () => getAnything("/tv/on_the_air"),
topRated: () => getAnything("/tv/top_rated"),
popular: () => getAnything("/tv/popular"),
search: query => getAnything("/search/tv", { query }),
show: id => getAnything(`/tv/${id}`, { append_to_response: "videos" })
};
export const apiImage = (
path,
defaultPoster = "https://images.unsplash.com/photo-1571847140471-1d7766e825ea?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=673&q=80"
) => (path ? `https://image.tmdb.org/t/p/w500${path}` : defaultPoster);