refactor typedefs

This commit is contained in:
adrifcastr 2023-06-21 13:23:39 +02:00
parent 1408fcde93
commit 7c3d4aac27
7 changed files with 118 additions and 135 deletions

View File

@ -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 =

View File

@ -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";

View File

@ -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<MWQuery, MWMediaMeta[]>();
cache.setCompare((a, b) => {
@ -18,7 +19,9 @@ export async function searchForMedia(query: MWQuery): Promise<MWMediaMeta[]> {
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(

View File

@ -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<T>(url: string): Promise<T> {
return res;
}
export async function searchMedia(query: string, type: TMDBContentTypes) {
export async function searchMedia(
query: string,
type: TMDBContentTypes
): Promise<TMDBMovieResponse | TMDBShowResponse> {
let data;
switch (type) {

View File

@ -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<T> = {
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[];
};

View File

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

View File

@ -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<TMDBShowData | TMDBMovieData>;
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<T> = {
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;