From b89bfe6569134843c62d2e94dd12b611d2c78148 Mon Sep 17 00:00:00 2001 From: kuekhaoyang Date: Tue, 18 Nov 2025 17:06:12 +0800 Subject: [PATCH] refactor: consolidate video detail fetching logic into a shared handler; remove redundant validation in GET and POST methods --- app/api/detail/route.ts | 152 +++++++++++++++------------------------- public/file.svg | 1 - public/globe.svg | 1 - public/next.svg | 1 - public/vercel.svg | 1 - public/window.svg | 1 - 6 files changed, 56 insertions(+), 101 deletions(-) delete mode 100644 public/file.svg delete mode 100644 public/globe.svg delete mode 100644 public/next.svg delete mode 100644 public/vercel.svg delete mode 100644 public/window.svg diff --git a/app/api/detail/route.ts b/app/api/detail/route.ts index a9ba576..1b9b551 100644 --- a/app/api/detail/route.ts +++ b/app/api/detail/route.ts @@ -7,60 +7,67 @@ import { NextRequest, NextResponse } from 'next/server'; import { getVideoDetail } from '@/lib/api/client'; import { getSourceById } from '@/lib/api/video-sources'; +/** + * Shared handler for fetching video details + */ +async function handleDetailRequest(id: string | null, source: string | null, method: string) { + // Validate input + if (!id) { + return NextResponse.json( + { error: 'Missing video ID parameter' }, + { status: 400 } + ); + } + + // Validate source + if (!source) { + return NextResponse.json( + { error: 'Missing source parameter' }, + { status: 400 } + ); + } + + const sourceConfig = getSourceById(source); + + if (!sourceConfig) { + return NextResponse.json( + { error: 'Invalid source ID' }, + { status: 400 } + ); + } + + // Fetch video detail without validation (already validated during search) + try { + const videoDetail = await getVideoDetail(id, sourceConfig); + + // Skip validation - videos are already checked during search + // Just return the episodes as-is + console.log(`[${method}] Fetching video details for ${id} from ${sourceConfig.name}`); + + return NextResponse.json({ + success: true, + data: videoDetail, + }); + } catch (error) { + console.error('Detail API error:', error); + + return NextResponse.json( + { + success: false, + error: error instanceof Error ? error.message : 'Failed to fetch video detail', + }, + { status: 500 } + ); + } +} + export async function GET(request: NextRequest) { try { const searchParams = request.nextUrl.searchParams; const id = searchParams.get('id'); const source = searchParams.get('source'); - // Validate input - if (!id) { - return NextResponse.json( - { error: 'Missing video ID parameter' }, - { status: 400 } - ); - } - - // Validate source - if (!source) { - return NextResponse.json( - { error: 'Missing source parameter' }, - { status: 400 } - ); - } - - const sourceConfig = getSourceById(source); - - if (!sourceConfig) { - return NextResponse.json( - { error: 'Invalid source ID' }, - { status: 400 } - ); - } - - // Fetch video detail without validation (already validated during search) - try { - const videoDetail = await getVideoDetail(id, sourceConfig); - - // Skip validation - videos are already checked during search - // Just return the episodes as-is - console.log(`[GET] Fetching video details for ${id} from ${sourceConfig.name}`); - - return NextResponse.json({ - success: true, - data: videoDetail, - }); - } catch (error) { - console.error('Detail API error:', error); - - return NextResponse.json( - { - success: false, - error: error instanceof Error ? error.message : 'Failed to fetch video detail', - }, - { status: 500 } - ); - } + return await handleDetailRequest(id, source, 'GET'); } catch (error) { console.error('Detail API error:', error); @@ -80,54 +87,7 @@ export async function POST(request: NextRequest) { const body = await request.json(); const { id, source } = body; - // Validate input - if (!id) { - return NextResponse.json( - { error: 'Missing video ID parameter' }, - { status: 400 } - ); - } - - // Validate source - if (!source) { - return NextResponse.json( - { error: 'Missing source parameter' }, - { status: 400 } - ); - } - - const sourceConfig = getSourceById(source); - - if (!sourceConfig) { - return NextResponse.json( - { error: 'Invalid source ID' }, - { status: 400 } - ); - } - - // Fetch video detail without validation (already validated during search) - try { - const videoDetail = await getVideoDetail(id, sourceConfig); - - // Skip validation - videos are already checked during search - // Just return the episodes as-is - console.log(`[POST] Fetching video details for ${id} from ${sourceConfig.name}`); - - return NextResponse.json({ - success: true, - data: videoDetail, - }); - } catch (error) { - console.error('Detail API error:', error); - - return NextResponse.json( - { - success: false, - error: error instanceof Error ? error.message : 'Failed to fetch video detail', - }, - { status: 500 } - ); - } + return await handleDetailRequest(id, source, 'POST'); } catch (error) { console.error('Detail API error:', error); diff --git a/public/file.svg b/public/file.svg deleted file mode 100644 index 004145c..0000000 --- a/public/file.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/public/globe.svg b/public/globe.svg deleted file mode 100644 index 567f17b..0000000 --- a/public/globe.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/public/next.svg b/public/next.svg deleted file mode 100644 index 5174b28..0000000 --- a/public/next.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/public/vercel.svg b/public/vercel.svg deleted file mode 100644 index 7705396..0000000 --- a/public/vercel.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/public/window.svg b/public/window.svg deleted file mode 100644 index b2b2a44..0000000 --- a/public/window.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file