mirror of
https://github.com/sussy-code/smov.git
synced 2024-12-21 14:47:41 +01:00
refactor typedefs
This commit is contained in:
parent
1408fcde93
commit
7c3d4aac27
7 changed files with 118 additions and 135 deletions
|
@ -13,13 +13,14 @@ import {
|
||||||
JWMediaResult,
|
JWMediaResult,
|
||||||
JWSeasonMetaResult,
|
JWSeasonMetaResult,
|
||||||
JW_API_BASE,
|
JW_API_BASE,
|
||||||
MWMediaMeta,
|
} from "./types/justwatch";
|
||||||
MWMediaType,
|
import { MWMediaMeta, MWMediaType } from "./types/mw";
|
||||||
|
import {
|
||||||
TMDBMediaResult,
|
TMDBMediaResult,
|
||||||
TMDBMovieData,
|
TMDBMovieData,
|
||||||
TMDBSeasonMetaResult,
|
TMDBSeasonMetaResult,
|
||||||
TMDBShowData,
|
TMDBShowData,
|
||||||
} from "./types";
|
} from "./types/tmdb";
|
||||||
import { makeUrl, proxiedFetch } from "../helpers/fetch";
|
import { makeUrl, proxiedFetch } from "../helpers/fetch";
|
||||||
|
|
||||||
type JWExternalIdType =
|
type JWExternalIdType =
|
||||||
|
|
|
@ -3,10 +3,8 @@ import {
|
||||||
JWMediaResult,
|
JWMediaResult,
|
||||||
JWSeasonMetaResult,
|
JWSeasonMetaResult,
|
||||||
JW_IMAGE_BASE,
|
JW_IMAGE_BASE,
|
||||||
MWMediaMeta,
|
} from "./types/justwatch";
|
||||||
MWMediaType,
|
import { MWMediaMeta, MWMediaType, MWSeasonMeta } from "./types/mw";
|
||||||
MWSeasonMeta,
|
|
||||||
} from "./types";
|
|
||||||
|
|
||||||
export function mediaTypeToJW(type: MWMediaType): JWContentTypes {
|
export function mediaTypeToJW(type: MWMediaType): JWContentTypes {
|
||||||
if (type === MWMediaType.MOVIE) return "movie";
|
if (type === MWMediaType.MOVIE) return "movie";
|
||||||
|
|
|
@ -6,7 +6,8 @@ import {
|
||||||
mediaTypeToTMDB,
|
mediaTypeToTMDB,
|
||||||
searchMedia,
|
searchMedia,
|
||||||
} from "./tmdb";
|
} 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[]>();
|
const cache = new SimpleCache<MWQuery, MWMediaMeta[]>();
|
||||||
cache.setCompare((a, b) => {
|
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[];
|
if (cache.has(query)) return cache.get(query) as MWMediaMeta[];
|
||||||
const { searchQuery, type } = query;
|
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(
|
const results = await Promise.all(
|
||||||
data.results.map(async (v) => {
|
data.results.map(async (v) => {
|
||||||
const formattedResult = await formatTMDBSearchResult(
|
const formattedResult = await formatTMDBSearchResult(
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
import { conf } from "@/setup/config";
|
import { conf } from "@/setup/config";
|
||||||
|
|
||||||
|
import { MWMediaMeta, MWMediaType, MWSeasonMeta } from "./types/mw";
|
||||||
import {
|
import {
|
||||||
MWMediaMeta,
|
|
||||||
MWMediaType,
|
|
||||||
MWSeasonMeta,
|
|
||||||
TMDBContentTypes,
|
TMDBContentTypes,
|
||||||
TMDBEpisodeShort,
|
TMDBEpisodeShort,
|
||||||
TMDBExternalIds,
|
TMDBExternalIds,
|
||||||
|
@ -18,7 +16,7 @@ import {
|
||||||
TMDBShowExternalIds,
|
TMDBShowExternalIds,
|
||||||
TMDBShowResponse,
|
TMDBShowResponse,
|
||||||
TMDBShowResult,
|
TMDBShowResult,
|
||||||
} from "./types";
|
} from "./types/tmdb";
|
||||||
import { mwFetch } from "../helpers/fetch";
|
import { mwFetch } from "../helpers/fetch";
|
||||||
|
|
||||||
export function mediaTypeToTMDB(type: MWMediaType): TMDBContentTypes {
|
export function mediaTypeToTMDB(type: MWMediaType): TMDBContentTypes {
|
||||||
|
@ -111,7 +109,10 @@ async function get<T>(url: string): Promise<T> {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function searchMedia(query: string, type: TMDBContentTypes) {
|
export async function searchMedia(
|
||||||
|
query: string,
|
||||||
|
type: TMDBContentTypes
|
||||||
|
): Promise<TMDBMovieResponse | TMDBShowResponse> {
|
||||||
let data;
|
let data;
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
|
48
src/backend/metadata/types/justwatch.ts
Normal file
48
src/backend/metadata/types/justwatch.ts
Normal 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[];
|
||||||
|
};
|
53
src/backend/metadata/types/mw.ts
Normal file
53
src/backend/metadata/types/mw.ts
Normal 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;
|
||||||
|
}
|
|
@ -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 TMDBContentTypes = "movie" | "show";
|
||||||
|
|
||||||
export type TMDBSeasonShort = {
|
export type TMDBSeasonShort = {
|
||||||
|
@ -76,12 +28,6 @@ export type TMDBSeasonMetaResult = {
|
||||||
episodes: TMDBEpisodeShort[];
|
episodes: TMDBEpisodeShort[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface DetailedMeta {
|
|
||||||
meta: MWMediaMeta;
|
|
||||||
imdbId?: string;
|
|
||||||
tmdbId?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TMDBShowData {
|
export interface TMDBShowData {
|
||||||
adult: boolean;
|
adult: boolean;
|
||||||
backdrop_path: string | null;
|
backdrop_path: string | null;
|
||||||
|
@ -225,63 +171,6 @@ export interface TMDBMovieData {
|
||||||
vote_count: number;
|
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 {
|
export interface TMDBEpisodeResult {
|
||||||
season: number;
|
season: number;
|
||||||
number: number;
|
number: number;
|
||||||
|
@ -342,16 +231,6 @@ export interface TMDBMovieResponse {
|
||||||
total_results: number;
|
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 {
|
export interface TMDBEpisode {
|
||||||
air_date: string;
|
air_date: string;
|
||||||
episode_number: number;
|
episode_number: number;
|
Loading…
Reference in a new issue