Skip to content

Commit

Permalink
feat(api): tmdb trending api wrapper (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
sct committed Sep 10, 2020
1 parent 839448f commit ba34e54
Showing 1 changed file with 72 additions and 6 deletions.
78 changes: 72 additions & 6 deletions server/api/themoviedb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
}

Expand Down Expand Up @@ -289,9 +289,9 @@ class TheMovieDb {
page = 1,
includeAdult = false,
language = 'en-US',
}: DiscoverMovieOptions = {}): Promise<TmdbDiscoverMovieResponse> => {
}: DiscoverMovieOptions = {}): Promise<TmdbSearchMovieResponse> => {
try {
const response = await this.axios.get<TmdbDiscoverMovieResponse>(
const response = await this.axios.get<TmdbSearchMovieResponse>(
'/discover/movie',
{
params: {
Expand All @@ -313,9 +313,9 @@ class TheMovieDb {
sortBy = 'popularity.desc',
page = 1,
language = 'en-US',
}: DiscoverTvOptions = {}): Promise<TmdbDiscoverTvResponse> => {
}: DiscoverTvOptions = {}): Promise<TmdbSearchTvResponse> => {
try {
const response = await this.axios.get<TmdbDiscoverTvResponse>(
const response = await this.axios.get<TmdbSearchTvResponse>(
'/discover/tv',
{
params: {
Expand All @@ -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<TmdbSearchMultiResponse>(
`/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<TmdbSearchMovieResponse>(
`/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<TmdbSearchTvResponse>(
`/trending/tv/${timeWindow}`,
{
params: {
page,
},
}
);

return response.data;
} catch (e) {
throw new Error(`[TMDB] Failed to fetch all trending: ${e.message}`);
}
};
}

export default TheMovieDb;

0 comments on commit ba34e54

Please sign in to comment.