mirror of
https://github.com/p-stream/p-stream.git
synced 2026-03-11 17:55:33 +00:00
This commit updates the import statements in the codebase to comply with ESLint rules for import ordering. All imports have been sorted alphabetically and grouped according to the specified import groups in the ESLint configuration. This improves the codebase's consistency and maintainability.
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
import { SimpleCache } from "@/utils/cache";
|
|
|
|
import {
|
|
JWContentTypes,
|
|
JWMediaResult,
|
|
JW_API_BASE,
|
|
formatJWMeta,
|
|
mediaTypeToJW,
|
|
} from "./justwatch";
|
|
import { MWMediaMeta, MWQuery } from "./types";
|
|
import { proxiedFetch } from "../helpers/fetch";
|
|
|
|
const cache = new SimpleCache<MWQuery, MWMediaMeta[]>();
|
|
cache.setCompare((a, b) => {
|
|
return a.type === b.type && a.searchQuery.trim() === b.searchQuery.trim();
|
|
});
|
|
cache.initialize();
|
|
|
|
type JWSearchQuery = {
|
|
content_types: JWContentTypes[];
|
|
page: number;
|
|
page_size: number;
|
|
query: string;
|
|
};
|
|
|
|
type JWPage<T> = {
|
|
items: T[];
|
|
page: number;
|
|
page_size: number;
|
|
total_pages: number;
|
|
total_results: number;
|
|
};
|
|
|
|
export async function searchForMedia(query: MWQuery): Promise<MWMediaMeta[]> {
|
|
if (cache.has(query)) return cache.get(query) as MWMediaMeta[];
|
|
const { searchQuery, type } = query;
|
|
|
|
const contentType = mediaTypeToJW(type);
|
|
const body: JWSearchQuery = {
|
|
content_types: [contentType],
|
|
page: 1,
|
|
query: searchQuery,
|
|
page_size: 40,
|
|
};
|
|
|
|
const data = await proxiedFetch<JWPage<JWMediaResult>>(
|
|
"/content/titles/en_US/popular",
|
|
{
|
|
baseURL: JW_API_BASE,
|
|
params: {
|
|
body: JSON.stringify(body),
|
|
},
|
|
}
|
|
);
|
|
|
|
const returnData = data.items.map<MWMediaMeta>((v) => formatJWMeta(v));
|
|
cache.set(query, returnData, 3600); // cache for an hour
|
|
return returnData;
|
|
}
|