From ba34e54d77d142d211df58d6ce9f53b6e673e004 Mon Sep 17 00:00:00 2001 From: sct Date: Thu, 10 Sep 2020 19:06:11 +0900 Subject: [PATCH] feat(api): tmdb trending api wrapper (#68) --- server/api/themoviedb.ts | 78 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 6 deletions(-) diff --git a/server/api/themoviedb.ts b/server/api/themoviedb.ts index f86ae0629..354ff0ffd 100644 --- a/server/api/themoviedb.ts +++ b/server/api/themoviedb.ts @@ -91,11 +91,11 @@ interface TmdbSearchMultiResponse extends TmdbPaginatedResponse { results: (TmdbMovieResult | TmdbTvResult | TmdbPersonResult)[]; } -interface TmdbDiscoverMovieResponse extends TmdbPaginatedResponse { +interface TmdbSearchMovieResponse extends TmdbPaginatedResponse { results: TmdbMovieResult[]; } -interface TmdbDiscoverTvResponse extends TmdbPaginatedResponse { +interface TmdbSearchTvResponse extends TmdbPaginatedResponse { results: TmdbTvResult[]; } @@ -289,9 +289,9 @@ class TheMovieDb { page = 1, includeAdult = false, language = 'en-US', - }: DiscoverMovieOptions = {}): Promise => { + }: DiscoverMovieOptions = {}): Promise => { try { - const response = await this.axios.get( + const response = await this.axios.get( '/discover/movie', { params: { @@ -313,9 +313,9 @@ class TheMovieDb { sortBy = 'popularity.desc', page = 1, language = 'en-US', - }: DiscoverTvOptions = {}): Promise => { + }: DiscoverTvOptions = {}): Promise => { try { - const response = await this.axios.get( + const response = await this.axios.get( '/discover/tv', { params: { @@ -331,6 +331,72 @@ class TheMovieDb { throw new Error(`[TMDB] Failed to fetch discover tv: ${e.message}`); } }; + + public getAllTrending = async ({ + page = 1, + timeWindow = 'day', + }: { page?: number; timeWindow?: 'day' | 'week' } = {}): Promise< + TmdbSearchMultiResponse + > => { + try { + const response = await this.axios.get( + `/trending/all/${timeWindow}`, + { + params: { + page, + }, + } + ); + + return response.data; + } catch (e) { + throw new Error(`[TMDB] Failed to fetch all trending: ${e.message}`); + } + }; + + public getMovieTrending = async ({ + page = 1, + timeWindow = 'day', + }: { page?: number; timeWindow?: 'day' | 'week' } = {}): Promise< + TmdbSearchMovieResponse + > => { + try { + const response = await this.axios.get( + `/trending/movie/${timeWindow}`, + { + params: { + page, + }, + } + ); + + return response.data; + } catch (e) { + throw new Error(`[TMDB] Failed to fetch all trending: ${e.message}`); + } + }; + + public getTvTrending = async ({ + page = 1, + timeWindow = 'day', + }: { page?: number; timeWindow?: 'day' | 'week' } = {}): Promise< + TmdbSearchTvResponse + > => { + try { + const response = await this.axios.get( + `/trending/tv/${timeWindow}`, + { + params: { + page, + }, + } + ); + + return response.data; + } catch (e) { + throw new Error(`[TMDB] Failed to fetch all trending: ${e.message}`); + } + }; } export default TheMovieDb;