From 7a1815069a6d61308600161a884e5c977b8a69b3 Mon Sep 17 00:00:00 2001 From: ZyphrZero <133507172+ZyphrZero@users.noreply.github.com> Date: Fri, 5 Dec 2025 10:47:35 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(api):=20add=20Vercel=20serve?= =?UTF-8?q?rless=20function=20for=20Bilibili=20search=20suggestions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Create api/bilibili.ts to handle Bilibili API requests in production - Proxy requests to https://s.search.bilibili.com/main/suggest - Add proper headers (User-Agent, Referer) to avoid blocking - Set CORS headers and cache control (60s) - Handle errors gracefully with fallback response - Update src/utils/suggestions.ts to detect environment - Use /bilibili (Vite proxy) in development - Use /api/bilibili (Vercel Function) in production - Check import.meta.env.DEV to determine environment This fixes the 404 error in production where Vite proxy is not available. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- api/bilibili.ts | 49 ++++++++++++++++++++++++++++++++++++++++ src/utils/suggestions.ts | 8 +++++-- 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 api/bilibili.ts diff --git a/api/bilibili.ts b/api/bilibili.ts new file mode 100644 index 0000000..5b1c49d --- /dev/null +++ b/api/bilibili.ts @@ -0,0 +1,49 @@ +import type { VercelRequest, VercelResponse } from '@vercel/node'; + +export default async function handler( + req: VercelRequest, + res: VercelResponse +) { + // Only allow GET requests + if (req.method !== 'GET') { + return res.status(405).json({ error: 'Method not allowed' }); + } + + const { term } = req.query; + + if (!term || typeof term !== 'string') { + return res.status(400).json({ error: 'Missing term parameter' }); + } + + try { + // Request Bilibili search suggestion API + const bilibiliUrl = `https://s.search.bilibili.com/main/suggest?term=${encodeURIComponent(term)}`; + + const response = await fetch(bilibiliUrl, { + headers: { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', + 'Referer': 'https://www.bilibili.com', + }, + }); + + if (!response.ok) { + throw new Error(`Bilibili API returned ${response.status}`); + } + + const data = await response.json(); + + // Set CORS headers + res.setHeader('Access-Control-Allow-Origin', '*'); + res.setHeader('Access-Control-Allow-Methods', 'GET'); + res.setHeader('Cache-Control', 's-maxage=60, stale-while-revalidate'); + + return res.status(200).json(data); + } catch (error) { + console.error('Bilibili API error:', error); + return res.status(500).json({ + error: 'Failed to fetch suggestions', + code: -1, + result: { tag: [] } + }); + } +} diff --git a/src/utils/suggestions.ts b/src/utils/suggestions.ts index e38141c..db3cd04 100644 --- a/src/utils/suggestions.ts +++ b/src/utils/suggestions.ts @@ -22,9 +22,13 @@ export const fetchSuggestions = (engine: string, query: string): Promise response.json())