forked from next-step/js-movie-review
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPopularMovies.js
More file actions
62 lines (55 loc) · 1.34 KB
/
Copy pathgetPopularMovies.js
File metadata and controls
62 lines (55 loc) · 1.34 KB
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
export async function getPopularMovies(params) {
const { language = "ko-KR", page = 1 } = params;
const url = new URL("https://api.themoviedb.org/3/movie/popular");
url.searchParams.set("language", language);
url.searchParams.set("page", page);
const response = await fetch(url.toString(), {
headers: {
accept: "application/json",
Authorization: `Bearer ${import.meta.env.VITE_TMDB_API_KEY}`,
},
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.status_message || "일시적으로 에러가 발생했어요.");
}
const popularMovies = await response.json();
return {
...popularMovies,
results: popularMovies.results.map(toClientEntity),
};
}
function toClientEntity(popularMovies) {
const {
adult,
backdrop_path,
genre_ids,
id,
original_language,
original_title,
overview,
popularity,
poster_path,
release_date,
title,
video,
vote_average,
vote_count,
} = popularMovies;
return {
adult,
backdropPath: backdrop_path,
genreIds: genre_ids,
id,
originalLanguage: original_language,
originalTitle: original_title,
overview,
popularity,
posterPath: poster_path,
releaseDate: release_date,
title,
video,
voteAverage: vote_average,
voteCount: vote_count,
};
}