feat: add multi-search, adjust types to allow type narrowing
This commit is contained in:
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "tmdb-ts",
|
||||
"version": "0.2.0",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "tmdb-ts",
|
||||
"version": "0.2.0",
|
||||
"version": "1.0.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"cross-fetch": "^3.1.4"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { BaseEndpoint } from './base';
|
||||
import { Search } from '../types/search';
|
||||
import { MultiSearchResult, Search } from '../types/search';
|
||||
import { Collection, Company, Movie, Person, TV } from '../types';
|
||||
|
||||
const BASE_SEARCH = '/search';
|
||||
@@ -24,6 +24,10 @@ export interface PeopleSearchOptions extends SearchOptions {
|
||||
include_adult?: boolean;
|
||||
}
|
||||
|
||||
export interface MultiSearchOptions extends SearchOptions {
|
||||
include_adult?: boolean;
|
||||
}
|
||||
|
||||
export class SearchEndpoint extends BaseEndpoint {
|
||||
constructor(protected readonly accessToken: string) {
|
||||
super(accessToken);
|
||||
@@ -60,9 +64,14 @@ export class SearchEndpoint extends BaseEndpoint {
|
||||
return await this.api.get<Search<Person>>(`${BASE_SEARCH}/person`, options);
|
||||
}
|
||||
|
||||
// TODO: Multi search
|
||||
|
||||
async tvShows(options: TvSearchOptions): Promise<Search<TV>> {
|
||||
return await this.api.get<Search<TV>>(`${BASE_SEARCH}/tv`, options);
|
||||
}
|
||||
|
||||
async multi(options: MultiSearchOptions): Promise<Search<MultiSearchResult>> {
|
||||
return await this.api.get<Search<MultiSearchResult>>(
|
||||
`${BASE_SEARCH}/multi`,
|
||||
options
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { MediaType, TimeWindow, TrendingResults } from '../types';
|
||||
import { TrendingMediaType, TimeWindow, TrendingResults } from '../types';
|
||||
import { BaseEndpoint } from './base';
|
||||
|
||||
export class TrendingEndpoint extends BaseEndpoint {
|
||||
@@ -6,7 +6,7 @@ export class TrendingEndpoint extends BaseEndpoint {
|
||||
super(accessToken);
|
||||
}
|
||||
|
||||
async trending<T extends MediaType>(
|
||||
async trending<T extends TrendingMediaType>(
|
||||
mediaType: T,
|
||||
timeWindow: TimeWindow
|
||||
): Promise<TrendingResults<T>> {
|
||||
|
||||
@@ -15,6 +15,8 @@ export * from './find';
|
||||
export * from './keywords';
|
||||
export * from './collections';
|
||||
|
||||
export type MediaType = 'movie' | 'tv' | 'person';
|
||||
|
||||
export interface AuthorDetails {
|
||||
name: string;
|
||||
username: string;
|
||||
@@ -22,23 +24,7 @@ export interface AuthorDetails {
|
||||
rating?: number;
|
||||
}
|
||||
|
||||
export interface KnownFor {
|
||||
id: number;
|
||||
overview: string;
|
||||
release_date: string;
|
||||
video: boolean;
|
||||
adult: boolean;
|
||||
backdrop_path: string;
|
||||
media_type: string;
|
||||
genre_ids: number[];
|
||||
title: string;
|
||||
original_language: string;
|
||||
original_title: string;
|
||||
poster_path: string;
|
||||
vote_count: number;
|
||||
vote_average: number;
|
||||
popularity: number;
|
||||
}
|
||||
export type KnownFor = MovieWithMediaType | TVWithMediaType;
|
||||
|
||||
export interface Person {
|
||||
id: number;
|
||||
@@ -51,6 +37,10 @@ export interface Person {
|
||||
popularity: number;
|
||||
}
|
||||
|
||||
export interface PersonWithMediaType extends Person {
|
||||
media_type: 'person';
|
||||
}
|
||||
|
||||
export interface Movie {
|
||||
id: number;
|
||||
poster_path: string;
|
||||
@@ -68,6 +58,10 @@ export interface Movie {
|
||||
vote_average: number;
|
||||
}
|
||||
|
||||
export interface MovieWithMediaType extends Movie {
|
||||
media_type: 'movie';
|
||||
}
|
||||
|
||||
export interface Company {
|
||||
id: number;
|
||||
logo_path: string;
|
||||
@@ -91,6 +85,10 @@ export interface TV {
|
||||
vote_average: number;
|
||||
}
|
||||
|
||||
export interface TVWithMediaType extends TV {
|
||||
media_type: 'tv';
|
||||
}
|
||||
|
||||
export interface Genre {
|
||||
id: number;
|
||||
name: string;
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
import { MovieWithMediaType, PersonWithMediaType, TVWithMediaType } from '.';
|
||||
|
||||
export interface Search<T> {
|
||||
page: number;
|
||||
results: T[];
|
||||
total_pages: number;
|
||||
total_results: number;
|
||||
}
|
||||
|
||||
export type MultiSearchResult =
|
||||
| MovieWithMediaType
|
||||
| TVWithMediaType
|
||||
| PersonWithMediaType;
|
||||
|
||||
@@ -1,19 +1,28 @@
|
||||
import { Movie, Person, TV } from '.';
|
||||
export type MediaType = 'all' | 'movie' | 'tv' | 'person';
|
||||
import {
|
||||
Movie,
|
||||
Person,
|
||||
TV,
|
||||
MediaType,
|
||||
MovieWithMediaType,
|
||||
TVWithMediaType,
|
||||
PersonWithMediaType,
|
||||
} from '.';
|
||||
|
||||
export type TimeWindow = 'day' | 'week';
|
||||
|
||||
type TrendingResult<T extends MediaType> = T extends 'tv'
|
||||
export type TrendingMediaType = MediaType | 'all';
|
||||
|
||||
type TrendingResult<T extends TrendingMediaType> = T extends 'tv'
|
||||
? TV
|
||||
: T extends 'movie'
|
||||
? Movie
|
||||
: T extends 'person'
|
||||
? Person
|
||||
: TV | Movie | Person;
|
||||
: TVWithMediaType | MovieWithMediaType | PersonWithMediaType;
|
||||
|
||||
export interface TrendingResults<T extends MediaType> {
|
||||
export interface TrendingResults<T extends TrendingMediaType> {
|
||||
page: number;
|
||||
results: (TrendingResult<T> & { media_type: MediaType })[];
|
||||
results: TrendingResult<T>[];
|
||||
total_pages: number;
|
||||
total_results: number;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user