Files
aniplay-api/src/controllers/search/anilist.ts

46 lines
1.1 KiB
TypeScript

import { env } from "cloudflare:workers";
export async function fetchSearchResultsFromAnilist(
query: string,
page: number,
limit: number,
): Promise<SearchResultsResponse | undefined> {
const durableObjectId = env.ANILIST_DO.idFromName("GLOBAL");
const stub = env.ANILIST_DO.get(durableObjectId);
const Page = await stub.search(query, page, limit);
if (!Page || Page.media?.length === 0) {
return undefined;
}
const { media: results, pageInfo } = Page;
return {
results: results?.map((result: any) => {
if (!result) return null;
return {
id: result.id,
title: result.title?.userPreferred ?? result.title?.english,
coverImage: result.coverImage,
};
}),
hasNextPage: pageInfo?.hasNextPage,
};
}
type SearchResultsResponse = {
results:
| ({
id: number;
title: { userPreferred: string | null; english: string | null } | null;
coverImage: {
extraLarge: string | null;
large: string | null;
medium: string | null;
} | null;
} | null)[]
| null;
hasNextPage: boolean | null | undefined;
};