-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
53 lines (52 loc) · 1.28 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
48
49
50
51
52
53
import { API_KEY } from "./config";
const genres = {
12: "Adventure",
14: "Fantasy",
16: "Animation",
18: "Drama",
27: "Horror",
28: "Action",
35: "Comedy",
36: "History",
37: "Western",
53: "Thriller",
80: "Crime",
99: "Documentary",
878: "Science Fiction",
9648: "Mystery",
10402: "Music",
10749: "Romance",
10751: "Family",
10752: "War",
10770: "TV Movie",
};
const API = `https://api.themoviedb.org/3/discover/movie?api_key=${API_KEY}&sort_by=popularity.desc`;
const getImagePath = (path) =>
`https://image.tmdb.org/t/p/w440_and_h660_face${path}`;
const getBackdropPath = (path) =>
`https://image.tmdb.org/t/p/w370_and_h556_multi_faces${path}`;
export const getMovies = async () => {
const { results } = await fetch(API).then((x) => x.json());
const movies = results.map(
({
id,
original_title,
poster_path,
backdrop_path,
vote_average,
overview,
release_date,
genre_ids,
}) => ({
key: String(id),
title: original_title,
poster: getImagePath(poster_path),
backdrop: getBackdropPath(backdrop_path),
rating: vote_average,
description: overview,
releaseDate: release_date,
genres: genre_ids.map((genre) => genres[genre]),
})
);
return movies;
};