add changes, credits, search

This commit is contained in:
Blake Joynes
2021-05-15 11:05:40 -04:00
parent adaad60d4c
commit 4cc6ea50df
22 changed files with 4938 additions and 202 deletions

View File

@@ -1,22 +1,20 @@
import fetch from 'node-fetch';
const BASE_URL_V3 = 'https://api.themoviedb.org/3';
const BASE_URL_V3 = 'https://api.themoviedb.org/3'
export default class Api {
constructor(private accessToken: string) {
this.accessToken = accessToken;
}
export class Api{
constructor(private accessToken: string){
this.accessToken = accessToken
}
async get<T>(path: string): Promise<T> {
const response = await fetch(`${BASE_URL_V3}${path}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${this.accessToken}`,
'Content-Type': 'application/json;charset=utf-8'
},
});
return await response.json() as T;
}
async get<T>(path: string): Promise<T> {
const response = await fetch(`${BASE_URL_V3}${path}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${this.accessToken}`,
'Content-Type': 'application/json;charset=utf-8',
},
});
return await response.json() as T;
}
}

View File

@@ -1,6 +0,0 @@
require('dotenv').config()
export class config {
static accessToken = process.env.ACCESS_TOKEN;
}

View File

@@ -1,17 +1,11 @@
import { Api } from "../api";
import {Base} from "./base";
import { BaseEndpoint } from './base';
export class AccountEndpoint extends BaseEndpoint {
constructor(accessToken: string) {
super(accessToken);
}
export class Account extends Base{
constructor(accessToken: string){
super(accessToken);
}
async details(): Promise<any> {
return await this.api.get('/account');
}
async details(): Promise<any> {
return await this.api.get('/account');
}
}

View File

@@ -1,12 +1,9 @@
import {Api} from "../api";
import Api from '../api';
export class BaseEndpoint {
protected api: Api;
export class Base {
protected api: Api;
constructor(protected readonly accessToken: string){
this.api = new Api(accessToken);
}
constructor(protected readonly accessToken: string) {
this.api = new Api(accessToken);
}
}

View File

@@ -1,20 +1,16 @@
import {Base} from "./base";
import {Certifications} from "../types/certification";
import { BaseEndpoint } from './base';
import { Certifications } from '../types/certification';
export class CertificationEndpoint extends BaseEndpoint {
constructor(protected readonly accessToken: string) {
super(accessToken);
}
export class Certification extends Base{
async movies(): Promise<Certifications> {
return await this.api.get<Certifications>('/certification/movie/list');
}
constructor(protected readonly accessToken: string){
super(accessToken);
}
async movies(): Promise<Certifications> {
return await this.api.get<Certifications>('/certification/movie/list');
}
async tvShows(): Promise<Certifications> {
return await this.api.get<Certifications>('/certification/tv/list');
}
async tvShows(): Promise<Certifications> {
return await this.api.get<Certifications>('/certification/tv/list');
}
}

View File

@@ -1,35 +1,31 @@
import querystring, {ParsedUrlQueryInput} from 'querystring';
import {Base} from "./base";
import {Changes} from "../types/changes";
import querystring, { ParsedUrlQueryInput } from 'querystring';
import { BaseEndpoint } from './base';
import { Changes } from '../types/changes';
export interface ChangeOptions extends ParsedUrlQueryInput {
end_date?: string;
start_date?: string;
page?: number;
end_date?: string;
start_date?: string;
page?: number;
}
export class Change extends Base{
export class ChangeEndpoint extends BaseEndpoint {
constructor(protected readonly accessToken: string) {
super(accessToken);
}
constructor(protected readonly accessToken: string){
super(accessToken);
}
async movies(options?: ChangeOptions): Promise<Changes> {
const params = querystring.encode(options);
return await this.api.get<Changes>(`/movie/changes?${params}`);
}
async tvShows(options?: ChangeOptions): Promise<Changes> {
const params = querystring.stringify(options);
return await this.api.get<Changes>(`/tv/changes?${params}`);
}
async movies(options?: ChangeOptions): Promise<Changes> {
const params = querystring.encode(options);
return await this.api.get<Changes>(`/movie/changes?${params}`);
async person(options?: ChangeOptions): Promise<Changes> {
const params = querystring.stringify(options);
}
async tvShows(options?: ChangeOptions): Promise<Changes> {
const params = querystring.stringify(options);
return await this.api.get<Changes>(`/tv/changes?${params}`);
}
async person(options?: ChangeOptions): Promise<Changes> {
const params = querystring.stringify(options);
return await this.api.get<Changes>(`/person/changes${params}`);
}
return await this.api.get<Changes>(`/person/changes${params}`);
}
}

14
src/endpoints/credits.ts Normal file
View File

@@ -0,0 +1,14 @@
import { BaseEndpoint } from './base';
import { CreditResponse } from '../types/credits';
export class CreditsEndpoint extends BaseEndpoint {
constructor(protected readonly accessToken: string) {
super(accessToken);
}
async getById(id: string): Promise<CreditResponse> {
return await this.api.get<CreditResponse>(`/credit/${id}`);
}
}

7
src/endpoints/index.ts Normal file
View File

@@ -0,0 +1,7 @@
export * from './account';
export * from './certification';
export * from './changes';
export * from './credits';
export * from './search';

65
src/endpoints/search.ts Normal file
View File

@@ -0,0 +1,65 @@
import { BaseEndpoint } from './base';
import querystring, { ParsedUrlQueryInput } from 'querystring';
import { Search } from '../types/search';
import { Collection, Company, Movie, Person, TV } from '../types';
const BASE_SEARCH = '/search';
export interface SearchOptions extends ParsedUrlQueryInput {
query: string;
page?: number;
}
export interface MovieSearchOptions extends SearchOptions {
include_adult?: boolean;
year?: number;
primary_release_year?: number;
}
export interface TvSearchOptions extends SearchOptions{
include_adult?: boolean;
first_air_date_year?: number;
}
export interface PeopleSearchOptions extends SearchOptions{
include_adult?: boolean;
}
export class SearchEndpoint extends BaseEndpoint {
constructor(protected readonly accessToken: string) {
super(accessToken);
}
async companies(options: SearchOptions): Promise<Search<Company>>{
const params = querystring.encode(options);
return await this.api.get<Search<Company>>(`${BASE_SEARCH}/company?${params}`);
}
async collections(options: SearchOptions): Promise<Search<Collection>>{
const params = querystring.encode(options);
return await this.api.get<Search<Collection>>(`${BASE_SEARCH}/collection?${params}`);
}
async keywords(options: SearchOptions): Promise<Search<{ id: string, name: string }>>{
const params = querystring.encode(options);
return await this.api.get<Search<{ id: string, name: string }>>(`${BASE_SEARCH}/keyword?${params}`);
}
async movies(options: MovieSearchOptions): Promise<Search<Movie>>{
const params = querystring.encode(options);
return await this.api.get<Search<Movie>>(`${BASE_SEARCH}/movie?${params}`);
}
async people(options: PeopleSearchOptions): Promise<Search<Person>>{
const params = querystring.encode(options);
return await this.api.get<Search<Person>>(`${BASE_SEARCH}/person?${params}`);
}
// TODO: Multi search
async tvShows(options: TvSearchOptions): Promise<Search<TV>>{
const params = querystring.encode(options);
return await this.api.get<Search<TV>>(`${BASE_SEARCH}/tv?${params}`);
}
}

View File

@@ -0,0 +1,3 @@
import TMDB from './tmdb';
export default TMDB;

View File

@@ -1,28 +1,35 @@
import { Account } from "./endpoints/account";
import {
AccountEndpoint,
CertificationEndpoint,
ChangeEndpoint,
CreditsEndpoint,
SearchEndpoint,
} from './endpoints';
import {Certification} from "./endpoints/certification";
import {Change} from "./endpoints/changes";
export default class TMDB {
private readonly accessToken: string;
export class TMDB {
private accessToken: string;
constructor(accessToken: string) {
this.accessToken = accessToken;
}
constructor(accessToken: string){
this.accessToken = accessToken;
}
get account(): AccountEndpoint {
return new AccountEndpoint(this.accessToken);
}
get account(){
return new Account(this.accessToken);
}
get certifications(): CertificationEndpoint {
return new CertificationEndpoint(this.accessToken);
}
get certifications(){
return new Certification(this.accessToken);
}
get changes(): ChangeEndpoint {
return new ChangeEndpoint(this.accessToken);
}
get changes(){
return new Change(this.accessToken);
}
get credits(): CreditsEndpoint {
return new CreditsEndpoint(this.accessToken);
}
get search(): SearchEndpoint{
return new SearchEndpoint(this.accessToken);
}
}

View File

@@ -1,20 +1,19 @@
export interface Certification {
certification: string;
meaning: string;
order: number;
}
export interface Certifications {
certifications: {
US: Certification[],
CA: Certification[],
DE: Certification[],
GB: Certification[],
AU: Certification[],
BR: Certification[],
FR: Certification[],
NZ: Certification[],
IN: Certification[],
}
}
export interface Certification {
certification: string;
meaning: string;
order: number;
certifications: {
US: Certification[],
CA: Certification[],
DE: Certification[],
GB: Certification[],
AU: Certification[],
BR: Certification[],
FR: Certification[],
NZ: Certification[],
IN: Certification[],
}
}

View File

@@ -1,11 +1,11 @@
export interface Changes{
results: Change[];
page: number;
total_pages: nubmer;
total_results: number;
export interface Change {
id: number;
adult: boolean | undefined;
}
export interface Change {
id: number;
adult: boolean | undefined;
export interface Changes{
results: Change[];
page: number;
total_pages: number;
total_results: number;
}

37
src/types/credits.d.ts vendored Normal file
View File

@@ -0,0 +1,37 @@
export interface CreditSeason {
air_date?: string;
poster_path?: string;
season_number?: number;
}
export interface Media{
i?: number;
name?:string;
first_air_date?: string;
vote_count?: number;
overview?: string;
vote_average?: number;
backdrop_path?: string;
genre_ids?: number[];
original_name?: string;
origin_country?: string[];
poster_path?: string;
original_language?: string;
popularity?: number;
character?: string;
episodes?: string[];
seasons?: CreditSeason[];
}
export interface CreditResponse {
credit_type?: string;
department?: string;
job?: string;
media?: Media;
media_type?: string;
id?: string;
person?: Person;
}

88
src/types/index.d.ts vendored
View File

@@ -0,0 +1,88 @@
import * as certs from './certification';
import * as credits from './credits';
import * as changes from './changes';
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 interface Person {
id: number;
name: string;
known_for: KnownFor[];
profile_path: string;
adult: boolean;
known_for_department: string;
gender: number;
popularity: number;
}
export interface Collection {
id:number;
backdrop_path: string;
name: string;
poster_path: string;
}
export interface Movie {
id: number;
logo_path: string;
name: string;
poster_path: string;
adult: boolean;
overview: string;
release_date: string;
genre_ids: number[];
original_title: string;
original_language: string;
title: string;
backdrop_path: string;
popularity: number;
vote_count: number;
video: boolean;
vote_average: number;
}
export interface Company {
id: number;
logo_path: string;
name: string;
origin_country: string;
}
export interface TV {
id: number;
name:string;
first_air_date: string;
backdrop_path: string;
genre_ids: number[];
origin_country: string[];
original_language: string;
original_name: string;
overview: string;
poster_path: string;
popularity: number;
vote_count: number;
vote_average: number;
}
export {
certs,
credits,
changes,
};

8
src/types/search.ts Normal file
View File

@@ -0,0 +1,8 @@
export interface Search<T> {
page: number;
results: T[];
total_pages: number;
total_results: number;
}