mirror of
https://github.com/KuekHaoYang/KVideo.git
synced 2026-08-15 00:33:44 +08:00
refactor: remove custom API handling from GET and POST methods; delete unused useMediaQuery hook and DetailRequest type
This commit is contained in:
+3
-45
@@ -4,16 +4,14 @@
|
||||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getVideoDetail, getVideoDetailCustom } from '@/lib/api/client';
|
||||
import { getVideoDetail } from '@/lib/api/client';
|
||||
import { getSourceById } from '@/lib/api/video-sources';
|
||||
import type { DetailRequest } from '@/lib/types';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const id = searchParams.get('id');
|
||||
const source = searchParams.get('source');
|
||||
const customApi = searchParams.get('customApi');
|
||||
|
||||
// Validate input
|
||||
if (!id) {
|
||||
@@ -23,26 +21,6 @@ export async function GET(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
// Handle custom API case
|
||||
if (customApi) {
|
||||
try {
|
||||
const videoDetail = await getVideoDetailCustom(id, customApi);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: videoDetail,
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Failed to fetch video detail',
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Validate source
|
||||
if (!source) {
|
||||
return NextResponse.json(
|
||||
@@ -99,8 +77,8 @@ export async function GET(request: NextRequest) {
|
||||
// Support POST method for complex requests
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body: DetailRequest = await request.json();
|
||||
const { id, source, customApi } = body;
|
||||
const body = await request.json();
|
||||
const { id, source } = body;
|
||||
|
||||
// Validate input
|
||||
if (!id) {
|
||||
@@ -110,26 +88,6 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
// Handle custom API case
|
||||
if (customApi) {
|
||||
try {
|
||||
const videoDetail = await getVideoDetailCustom(id, customApi);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: videoDetail,
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Failed to fetch video detail',
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Validate source
|
||||
if (!source) {
|
||||
return NextResponse.json(
|
||||
|
||||
@@ -250,20 +250,4 @@ export async function getVideoDetail(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get video detail with custom API URL
|
||||
*/
|
||||
export async function getVideoDetailCustom(
|
||||
id: string | number,
|
||||
customApiUrl: string
|
||||
): Promise<VideoDetail> {
|
||||
const customSource: VideoSource = {
|
||||
id: 'custom',
|
||||
name: 'Custom API',
|
||||
baseUrl: customApiUrl,
|
||||
searchPath: '',
|
||||
detailPath: '',
|
||||
};
|
||||
|
||||
return getVideoDetail(id, customSource);
|
||||
}
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
/**
|
||||
* useMediaQuery Hook
|
||||
* 媒体查询 Hook - 监听浏览器媒体查询状态变化
|
||||
*
|
||||
* Note: Ready-to-use utility hook for responsive features.
|
||||
* Currently not in active use but available for future enhancements.
|
||||
*
|
||||
* 遵循 Liquid Glass 设计系统原则:
|
||||
* - 响应式设计优先
|
||||
* - 性能优化(使用 matchMedia API)
|
||||
* - SSR 兼容
|
||||
*/
|
||||
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
/**
|
||||
* 使用媒体查询 Hook
|
||||
* @param query - CSS 媒体查询字符串(例如:'(min-width: 768px)')
|
||||
* @returns boolean - 媒体查询是否匹配
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 检测是否为移动设备
|
||||
* const isMobile = useMediaQuery('(max-width: 768px)');
|
||||
*
|
||||
* // 检测是否为暗色模式
|
||||
* const isDarkMode = useMediaQuery('(prefers-color-scheme: dark)');
|
||||
*
|
||||
* // 检测是否为触摸设备
|
||||
* const isTouchDevice = useMediaQuery('(hover: none) and (pointer: coarse)');
|
||||
* ```
|
||||
*/
|
||||
export function useMediaQuery(query: string): boolean {
|
||||
// 初始状态处理(SSR 兼容)
|
||||
const getMatches = (query: string): boolean => {
|
||||
// 防止 SSR 错误
|
||||
if (typeof window === 'undefined') {
|
||||
return false;
|
||||
}
|
||||
return window.matchMedia(query).matches;
|
||||
};
|
||||
|
||||
const [matches, setMatches] = useState<boolean>(getMatches(query));
|
||||
|
||||
useEffect(() => {
|
||||
// 防止 SSR 错误
|
||||
if (typeof window === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
const mediaQuery = window.matchMedia(query);
|
||||
|
||||
// 更新状态的处理函数
|
||||
const handleChange = (event: MediaQueryListEvent) => {
|
||||
setMatches(event.matches);
|
||||
};
|
||||
|
||||
// 初始化时检查一次
|
||||
setMatches(mediaQuery.matches);
|
||||
|
||||
// 添加监听器(现代浏览器使用 addEventListener)
|
||||
if (mediaQuery.addEventListener) {
|
||||
mediaQuery.addEventListener('change', handleChange);
|
||||
} else {
|
||||
// 兼容旧版浏览器(已废弃的 API)
|
||||
// @ts-ignore
|
||||
mediaQuery.addListener(handleChange);
|
||||
}
|
||||
|
||||
// 清理函数
|
||||
return () => {
|
||||
if (mediaQuery.removeEventListener) {
|
||||
mediaQuery.removeEventListener('change', handleChange);
|
||||
} else {
|
||||
// @ts-ignore
|
||||
mediaQuery.removeListener(handleChange);
|
||||
}
|
||||
};
|
||||
}, [query]);
|
||||
|
||||
return matches;
|
||||
}
|
||||
@@ -97,11 +97,3 @@ export interface ApiDetailResponse {
|
||||
vod_play_url?: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
// Detail Request Types
|
||||
export interface DetailRequest {
|
||||
id: string | number;
|
||||
source: string;
|
||||
customApi?: string;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user