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

4
.eslintignore Normal file
View File

@@ -0,0 +1,4 @@
node_modules
dist
lib
coverage

43
.eslintrc.js Normal file
View File

@@ -0,0 +1,43 @@
module.exports = {
env: {
browser: false,
es2021: true,
},
extends: [
'plugin:@typescript-eslint/recommended',
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
},
plugins: [
'@typescript-eslint',
],
rules: {
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-empty-interface': 'off',
'@typescript-eslint/explicit-function-return-type': ['warn', { allowExpressions: true }],
indent: 'off',
'@typescript-eslint/indent': ['error', 2],
'no-trailing-spaces': 'error',
quotes: ['error', 'single', { allowTemplateLiterals: true }],
semi: ['error', 'always'],
'spaced-comment': ['error', 'always'],
'no-irregular-whitespace': ['error', { 'skipComments': true }],
'space-infix-ops': 'error',
'array-bracket-spacing': 'error',
'object-curly-spacing': ['error', 'always'],
'space-before-function-paren': ['error', {
named: 'never',
anonymous: 'never',
asyncArrow: 'always',
}],
'comma-dangle': ['warn', 'always-multiline'],
'no-multiple-empty-lines': 'error',
},
};

2
.gitignore vendored
View File

@@ -1,4 +1,6 @@
/node_modules/
/.env
dist
/.npmignore
.idea

4502
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,27 +1,55 @@
{
"name": "tmdb-ts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"version": "0.0.3",
"description": "TMDB v3 library wrapper",
"main": "dist/index.ts",
"types": "dist/types/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "ts-node"
"compile": "rm -rf dist && tsc -d && npm run copy-types",
"lint": "eslint --ext .ts src/",
"lint:fix": "eslint --ext .ts src/ --fix",
"prepublish": "tsc",
"copy-types": "mkdir dist/types && cp src/types/*.d.ts dist/types"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/blakejoy/tmdb-ts/issues",
"email": "blake@bjoynes.com"
},
"repository": {
"type": "git",
"url": "https://github.com/blakejoy/tmdb-ts.git"
},
"keywords": [
"tmdb",
"movies",
"database",
"api",
"typescript"
],
"author": "Blake Joynes",
"license": "MIT",
"devDependencies": {
"@types/node": "^15.3.0",
"@types/node-fetch": "^2.5.10",
"@typescript-eslint/eslint-plugin": "^4.23.0",
"@typescript-eslint/parser": "^4.23.0",
"dotenv": "^9.0.2",
"eslint": "^7.26.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.23.1",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
},
"dependencies": {
"dotenv": "^9.0.2",
"node-fetch": "^2.6.1"
},
"volta": {
"node": "15.13.0",
"yarn": "1.19.2"
}
"yarn": "1.19.2",
"npm": "7.12.1"
},
"files": [
"dist"
]
}

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;
}

View File

@@ -1,69 +1,25 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es2020" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
"sourceMap": true /* Generates corresponding '.map' file. */,
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "lib" /* Redirect output structure to the directory. */,
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"outDir": "dist" /* Redirect output structure to the directory. */,
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"types": ["node"],
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
"experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,
"emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */,
/* Advanced Options */
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
},
"types": ["node"],
"include": [
"src"
],
"exclude": [
"node_modules",
"dist",
"./src/types/*.ts"
]
}