From 7c3d4aac272a77debe89a02a77e4c54c9b09ed5e Mon Sep 17 00:00:00 2001 From: adrifcastr Date: Wed, 21 Jun 2023 13:23:39 +0200 Subject: [PATCH] refactor typedefs --- src/backend/metadata/getmeta.ts | 7 +- src/backend/metadata/justwatch.ts | 6 +- src/backend/metadata/search.ts | 7 +- src/backend/metadata/tmdb.ts | 11 +- src/backend/metadata/types/justwatch.ts | 48 +++++++ src/backend/metadata/types/mw.ts | 53 ++++++++ .../metadata/{types.ts => types/tmdb.ts} | 121 ------------------ 7 files changed, 118 insertions(+), 135 deletions(-) create mode 100644 src/backend/metadata/types/justwatch.ts create mode 100644 src/backend/metadata/types/mw.ts rename src/backend/metadata/{types.ts => types/tmdb.ts} (69%) diff --git a/src/backend/metadata/getmeta.ts b/src/backend/metadata/getmeta.ts index aa4267c5..b347e720 100644 --- a/src/backend/metadata/getmeta.ts +++ b/src/backend/metadata/getmeta.ts @@ -13,13 +13,14 @@ import { JWMediaResult, JWSeasonMetaResult, JW_API_BASE, - MWMediaMeta, - MWMediaType, +} from "./types/justwatch"; +import { MWMediaMeta, MWMediaType } from "./types/mw"; +import { TMDBMediaResult, TMDBMovieData, TMDBSeasonMetaResult, TMDBShowData, -} from "./types"; +} from "./types/tmdb"; import { makeUrl, proxiedFetch } from "../helpers/fetch"; type JWExternalIdType = diff --git a/src/backend/metadata/justwatch.ts b/src/backend/metadata/justwatch.ts index 857ff006..724c4acf 100644 --- a/src/backend/metadata/justwatch.ts +++ b/src/backend/metadata/justwatch.ts @@ -3,10 +3,8 @@ import { JWMediaResult, JWSeasonMetaResult, JW_IMAGE_BASE, - MWMediaMeta, - MWMediaType, - MWSeasonMeta, -} from "./types"; +} from "./types/justwatch"; +import { MWMediaMeta, MWMediaType, MWSeasonMeta } from "./types/mw"; export function mediaTypeToJW(type: MWMediaType): JWContentTypes { if (type === MWMediaType.MOVIE) return "movie"; diff --git a/src/backend/metadata/search.ts b/src/backend/metadata/search.ts index 9e2883d4..99cb51ba 100644 --- a/src/backend/metadata/search.ts +++ b/src/backend/metadata/search.ts @@ -6,7 +6,8 @@ import { mediaTypeToTMDB, searchMedia, } from "./tmdb"; -import { MWMediaMeta, MWQuery } from "./types"; +import { MWMediaMeta, MWQuery } from "./types/mw"; +import { TMDBMovieResponse, TMDBShowResponse } from "./types/tmdb"; const cache = new SimpleCache(); cache.setCompare((a, b) => { @@ -18,7 +19,9 @@ export async function searchForMedia(query: MWQuery): Promise { if (cache.has(query)) return cache.get(query) as MWMediaMeta[]; const { searchQuery, type } = query; - const data = await searchMedia(searchQuery, mediaTypeToTMDB(type)); + const data = (await searchMedia(searchQuery, mediaTypeToTMDB(type))) as + | TMDBMovieResponse + | TMDBShowResponse; const results = await Promise.all( data.results.map(async (v) => { const formattedResult = await formatTMDBSearchResult( diff --git a/src/backend/metadata/tmdb.ts b/src/backend/metadata/tmdb.ts index cf327070..4a9271ba 100644 --- a/src/backend/metadata/tmdb.ts +++ b/src/backend/metadata/tmdb.ts @@ -1,9 +1,7 @@ import { conf } from "@/setup/config"; +import { MWMediaMeta, MWMediaType, MWSeasonMeta } from "./types/mw"; import { - MWMediaMeta, - MWMediaType, - MWSeasonMeta, TMDBContentTypes, TMDBEpisodeShort, TMDBExternalIds, @@ -18,7 +16,7 @@ import { TMDBShowExternalIds, TMDBShowResponse, TMDBShowResult, -} from "./types"; +} from "./types/tmdb"; import { mwFetch } from "../helpers/fetch"; export function mediaTypeToTMDB(type: MWMediaType): TMDBContentTypes { @@ -111,7 +109,10 @@ async function get(url: string): Promise { return res; } -export async function searchMedia(query: string, type: TMDBContentTypes) { +export async function searchMedia( + query: string, + type: TMDBContentTypes +): Promise { let data; switch (type) { diff --git a/src/backend/metadata/types/justwatch.ts b/src/backend/metadata/types/justwatch.ts new file mode 100644 index 00000000..cb3ac092 --- /dev/null +++ b/src/backend/metadata/types/justwatch.ts @@ -0,0 +1,48 @@ +export type JWContentTypes = "movie" | "show"; + +export type JWSearchQuery = { + content_types: JWContentTypes[]; + page: number; + page_size: number; + query: string; +}; + +export type JWPage = { + items: T[]; + page: number; + page_size: number; + total_pages: number; + total_results: number; +}; + +export const JW_API_BASE = "https://apis.justwatch.com"; +export const JW_IMAGE_BASE = "https://images.justwatch.com"; + +export type JWSeasonShort = { + title: string; + id: number; + season_number: number; +}; + +export type JWEpisodeShort = { + title: string; + id: number; + episode_number: number; +}; + +export type JWMediaResult = { + title: string; + poster?: string; + id: number; + original_release_year?: number; + jw_entity_id: string; + object_type: JWContentTypes; + seasons?: JWSeasonShort[]; +}; + +export type JWSeasonMetaResult = { + title: string; + id: string; + season_number: number; + episodes: JWEpisodeShort[]; +}; diff --git a/src/backend/metadata/types/mw.ts b/src/backend/metadata/types/mw.ts new file mode 100644 index 00000000..e7cc26fe --- /dev/null +++ b/src/backend/metadata/types/mw.ts @@ -0,0 +1,53 @@ +export enum MWMediaType { + MOVIE = "movie", + SERIES = "series", + ANIME = "anime", +} + +export type MWSeasonMeta = { + id: string; + number: number; + title: string; +}; + +export type MWSeasonWithEpisodeMeta = { + id: string; + number: number; + title: string; + episodes: { + id: string; + number: number; + title: string; + }[]; +}; + +type MWMediaMetaBase = { + title: string; + id: string; + year?: string; + poster?: string; +}; + +type MWMediaMetaSpecific = + | { + type: MWMediaType.MOVIE | MWMediaType.ANIME; + seasons: undefined; + } + | { + type: MWMediaType.SERIES; + seasons: MWSeasonMeta[]; + seasonData: MWSeasonWithEpisodeMeta; + }; + +export type MWMediaMeta = MWMediaMetaBase & MWMediaMetaSpecific; + +export interface MWQuery { + searchQuery: string; + type: MWMediaType; +} + +export interface DetailedMeta { + meta: MWMediaMeta; + imdbId?: string; + tmdbId?: string; +} diff --git a/src/backend/metadata/types.ts b/src/backend/metadata/types/tmdb.ts similarity index 69% rename from src/backend/metadata/types.ts rename to src/backend/metadata/types/tmdb.ts index fa7a7ef0..cb5e9aa4 100644 --- a/src/backend/metadata/types.ts +++ b/src/backend/metadata/types/tmdb.ts @@ -1,51 +1,3 @@ -export enum MWMediaType { - MOVIE = "movie", - SERIES = "series", - ANIME = "anime", -} - -export type MWSeasonMeta = { - id: string; - number: number; - title: string; -}; - -export type MWSeasonWithEpisodeMeta = { - id: string; - number: number; - title: string; - episodes: { - id: string; - number: number; - title: string; - }[]; -}; - -type MWMediaMetaBase = { - title: string; - id: string; - year?: string; - poster?: string; -}; - -type MWMediaMetaSpecific = - | { - type: MWMediaType.MOVIE | MWMediaType.ANIME; - seasons: undefined; - } - | { - type: MWMediaType.SERIES; - seasons: MWSeasonMeta[]; - seasonData: MWSeasonWithEpisodeMeta; - }; - -export type MWMediaMeta = MWMediaMetaBase & MWMediaMetaSpecific; - -export interface MWQuery { - searchQuery: string; - type: MWMediaType; -} - export type TMDBContentTypes = "movie" | "show"; export type TMDBSeasonShort = { @@ -76,12 +28,6 @@ export type TMDBSeasonMetaResult = { episodes: TMDBEpisodeShort[]; }; -export interface DetailedMeta { - meta: MWMediaMeta; - imdbId?: string; - tmdbId?: string; -} - export interface TMDBShowData { adult: boolean; backdrop_path: string | null; @@ -225,63 +171,6 @@ export interface TMDBMovieData { vote_count: number; } -export type TMDBMediaDetailsPromise = Promise; - -export interface TMDBMediaStatic { - getMediaDetails(id: string, type: "show"): TMDBMediaDetailsPromise; - getMediaDetails(id: string, type: "movie"): TMDBMediaDetailsPromise; - getMediaDetails(id: string, type: TMDBContentTypes): TMDBMediaDetailsPromise; -} - -export type JWContentTypes = "movie" | "show"; - -export type JWSearchQuery = { - content_types: JWContentTypes[]; - page: number; - page_size: number; - query: string; -}; - -export type JWPage = { - items: T[]; - page: number; - page_size: number; - total_pages: number; - total_results: number; -}; - -export const JW_API_BASE = "https://apis.justwatch.com"; -export const JW_IMAGE_BASE = "https://images.justwatch.com"; - -export type JWSeasonShort = { - title: string; - id: number; - season_number: number; -}; - -export type JWEpisodeShort = { - title: string; - id: number; - episode_number: number; -}; - -export type JWMediaResult = { - title: string; - poster?: string; - id: number; - original_release_year?: number; - jw_entity_id: string; - object_type: JWContentTypes; - seasons?: JWSeasonShort[]; -}; - -export type JWSeasonMetaResult = { - title: string; - id: string; - season_number: number; - episodes: JWEpisodeShort[]; -}; - export interface TMDBEpisodeResult { season: number; number: number; @@ -342,16 +231,6 @@ export interface TMDBMovieResponse { total_results: number; } -export type TMDBSearchResultsPromise = Promise< - TMDBShowResponse | TMDBMovieResponse ->; - -export interface TMDBSearchResultStatic { - searchMedia(query: string, type: TMDBContentTypes): TMDBSearchResultsPromise; - searchMedia(query: string, type: "movie"): TMDBSearchResultsPromise; - searchMedia(query: string, type: "show"): TMDBSearchResultsPromise; -} - export interface TMDBEpisode { air_date: string; episode_number: number;