fix: same SearchResult

This commit is contained in:
shinya
2025-07-13 12:40:25 +08:00
parent 3c8f316ad7
commit ce8a01c577
2 changed files with 10 additions and 43 deletions

View File

@@ -3,20 +3,6 @@ import { SearchResult } from '@/lib/types';
import { getDetailFromApi, searchFromApi } from './downstream';
export interface VideoDetail {
id: string;
title: string;
poster: string;
episodes: string[];
source: string;
source_name: string;
class?: string;
year: string;
desc?: string;
type_name?: string;
douban_id?: number;
}
interface FetchVideoDetailOptions {
source: string;
id: string;
@@ -32,7 +18,7 @@ export async function fetchVideoDetail({
source,
id,
fallbackTitle = '',
}: FetchVideoDetailOptions): Promise<VideoDetail> {
}: FetchVideoDetailOptions): Promise<SearchResult> {
// 优先通过搜索接口查找精确匹配
const apiSites = getAvailableApiSites();
const apiSite = apiSites.find((site) => site.key === source);
@@ -48,19 +34,7 @@ export async function fetchVideoDetail({
item.id.toString() === id.toString()
);
if (exactMatch) {
return {
id: exactMatch.id,
title: exactMatch.title,
poster: exactMatch.poster,
episodes: exactMatch.episodes,
source: exactMatch.source,
source_name: exactMatch.source_name,
class: exactMatch.class,
year: exactMatch.year,
desc: exactMatch.desc,
type_name: exactMatch.type_name,
douban_id: exactMatch.douban_id,
} as VideoDetail;
return exactMatch;
}
} catch (error) {
// do nothing
@@ -69,17 +43,9 @@ export async function fetchVideoDetail({
// 调用 /api/detail 接口
const detail = await getDetailFromApi(apiSite, id);
if (!detail) {
throw new Error('获取视频详情失败');
}
return {
id: detail.id,
title: detail.title,
poster: detail.poster || '',
episodes: detail.episodes,
source: detail.source,
source_name: detail.source_name,
class: detail.class,
year: detail.year || '',
desc: detail.desc,
type_name: detail.type_name,
};
return detail;
}

View File

@@ -1,7 +1,8 @@
/* eslint-disable no-console */
import { db } from '@/lib/db';
import { type VideoDetail, fetchVideoDetail } from '@/lib/fetchVideoDetail';
import { fetchVideoDetail } from '@/lib/fetchVideoDetail';
import { SearchResult } from '@/lib/types';
const STORAGE_TYPE = process.env.NEXT_PUBLIC_STORAGE_TYPE ?? 'localstorage';
@@ -16,14 +17,14 @@ async function refreshRecordAndFavorites() {
users.push(process.env.USERNAME);
}
// 函数级缓存key 为 `${source}+${id}`,值为 Promise<VideoDetail | null>
const detailCache = new Map<string, Promise<VideoDetail | null>>();
const detailCache = new Map<string, Promise<SearchResult | null>>();
// 获取详情 Promise带缓存和错误处理
const getDetail = async (
source: string,
id: string,
fallbackTitle: string
): Promise<VideoDetail | null> => {
): Promise<SearchResult | null> => {
const key = `${source}+${id}`;
let promise = detailCache.get(key);
if (!promise) {