import type { VercelRequest, VercelResponse } from '@vercel/node'; // Item Availability from Dropbox (using dl=1 for direct download) const STOCK_DROPBOX_URL = "https://www.dropbox.com/scl/fi/jaxc1y01ot7wj4ksswaoe/Item-Availability.xlsx?rlkey=v428jlri5jf1z5ggiebze7ovo&st=3uzg6csf&dl=1"; export default async function handler(req: VercelRequest, res: VercelResponse) { // CORS headers res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); if (req.method === 'OPTIONS') { return res.status(200).end(); } try { console.log('[fetch-stock] Fetching Stock from Dropbox...'); const response = await fetch(STOCK_DROPBOX_URL, { cache: 'no-store', headers: { 'Pragma': 'no-cache', 'Cache-Control': 'no-cache' } }); if (!response.ok) { throw new Error(`Dropbox responded with ${response.status}`); } const buffer = await response.arrayBuffer(); console.log('[fetch-stock] Successfully fetched Stock Excel, size:', buffer.byteLength); res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); res.status(200).send(Buffer.from(buffer)); } catch (error: any) { console.error('[fetch-stock] Error:', error); res.status(500).json({ error: error.message }); } }