From 839448fcc8cc14ea83092af82e2ba3d0d92c9b73 Mon Sep 17 00:00:00 2001 From: sct Date: Thu, 10 Sep 2020 18:38:11 +0900 Subject: [PATCH] feat(api): add tmdb discover api wrapper (#67) --- server/api/themoviedb.ts | 96 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/server/api/themoviedb.ts b/server/api/themoviedb.ts index 4002d1f5a..f86ae0629 100644 --- a/server/api/themoviedb.ts +++ b/server/api/themoviedb.ts @@ -6,6 +6,41 @@ interface SearchOptions { includeAdult?: boolean; } +interface DiscoverMovieOptions { + page?: number; + includeAdult?: boolean; + language?: string; + sortBy?: + | 'popularity.asc' + | 'popularity.desc' + | 'release_date.asc' + | 'release_date.desc' + | 'revenue.asc' + | 'revenue.desc' + | 'primary_release_date.asc' + | 'primary_release_date.desc' + | 'original_title.asc' + | 'original_title.desc' + | 'vote_average.asc' + | 'vote_average.desc' + | 'vote_count.asc' + | 'vote_count.desc'; +} + +interface DiscoverTvOptions { + page?: number; + language?: string; + sortBy?: + | 'popularity.asc' + | 'popularity.desc' + | 'vote_average.asc' + | 'vote_average.desc' + | 'vote_count.asc' + | 'vote_count.desc' + | 'first_air_date.asc' + | 'first_air_date.desc'; +} + interface TmdbMediaResult { id: number; media_type: string; @@ -46,13 +81,24 @@ export interface TmdbPersonResult { known_for: (TmdbMovieResult | TmdbTvResult)[]; } -interface TmdbSearchMultiResponse { +interface TmdbPaginatedResponse { page: number; total_results: number; total_pages: number; +} + +interface TmdbSearchMultiResponse extends TmdbPaginatedResponse { results: (TmdbMovieResult | TmdbTvResult | TmdbPersonResult)[]; } +interface TmdbDiscoverMovieResponse extends TmdbPaginatedResponse { + results: TmdbMovieResult[]; +} + +interface TmdbDiscoverTvResponse extends TmdbPaginatedResponse { + results: TmdbTvResult[]; +} + interface TmdbMovieDetails { id: number; imdb_id?: string; @@ -237,6 +283,54 @@ class TheMovieDb { throw new Error(`[TMDB] Failed to fetch tv show details: ${e.message}`); } }; + + public getDiscoverMovies = async ({ + sortBy = 'popularity.desc', + page = 1, + includeAdult = false, + language = 'en-US', + }: DiscoverMovieOptions = {}): Promise => { + try { + const response = await this.axios.get( + '/discover/movie', + { + params: { + sort_by: sortBy, + page, + include_adult: includeAdult, + language, + }, + } + ); + + return response.data; + } catch (e) { + throw new Error(`[TMDB] Failed to fetch discover movies: ${e.message}`); + } + }; + + public getDiscoverTv = async ({ + sortBy = 'popularity.desc', + page = 1, + language = 'en-US', + }: DiscoverTvOptions = {}): Promise => { + try { + const response = await this.axios.get( + '/discover/tv', + { + params: { + sort_by: sortBy, + page, + language, + }, + } + ); + + return response.data; + } catch (e) { + throw new Error(`[TMDB] Failed to fetch discover tv: ${e.message}`); + } + }; } export default TheMovieDb;