feat(bsr): fetch BSR.xlsx from Dropbox instead of local file

- Rewrite api/fetch-bsr.ts to proxy BSR.xlsx from the Dropbox URL,
  matching the same pattern as fetch-buybox.ts and other data sources
- Add /api/fetch-bsr proxy in vite.config.ts for local dev

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
christian.vidal
2026-03-02 18:37:39 +01:00
co-authored by Claude Sonnet 4.6
parent ce13d17eda
commit 17a3df9fb3
2 changed files with 35 additions and 18 deletions
+29 -18
View File
@@ -1,27 +1,38 @@
import { promises as fs } from 'fs';
import path from 'path';
import type { VercelRequest, VercelResponse } from '@vercel/node'; import type { VercelRequest, VercelResponse } from '@vercel/node';
export default async function handler(req: VercelRequest, res: VercelResponse) { const BSR_DROPBOX_URL = "https://www.dropbox.com/scl/fi/r32r9x6r7bvgg6ckiaowk/BSR.xlsx?rlkey=vka50xyhc637riq901qo5bk16&st=y6ys2oz8&dl=1";
try {
const filePath = path.join(process.cwd(), 'BSR.xlsx');
try { export default async function handler(req: VercelRequest, res: VercelResponse) {
await fs.access(filePath); // CORS headers
} catch { res.setHeader('Access-Control-Allow-Origin', '*');
return res.status(404).json({ error: 'BSR data file not found' }); 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-bsr] Fetching BSR.xlsx from Dropbox...');
const response = await fetch(BSR_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 fileBuffer = await fs.readFile(filePath); const buffer = await response.arrayBuffer();
console.log('[fetch-bsr] Successfully fetched BSR Excel, size:', buffer.byteLength);
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader('Content-Disposition', 'attachment; filename="BSR.xlsx"'); res.status(200).send(Buffer.from(buffer));
res.setHeader('Cache-Control', 'public, max-age=3600'); } catch (error: any) {
console.error('[fetch-bsr] Error:', error);
// Convert Buffer to ArrayBuffer before sending for fetch compatibility on client res.status(500).json({ error: error.message });
res.send(fileBuffer);
} catch (error) {
console.error('Error serving BSR Excel file:', error);
res.status(500).json({ error: 'Failed to serve BSR file' });
} }
} }
+6
View File
@@ -44,6 +44,12 @@ export default defineConfig(({ mode }) => {
changeOrigin: true, changeOrigin: true,
rewrite: () => '', rewrite: () => '',
followRedirects: true followRedirects: true
},
'/api/fetch-bsr': {
target: 'https://www.dropbox.com/scl/fi/r32r9x6r7bvgg6ckiaowk/BSR.xlsx?rlkey=vka50xyhc637riq901qo5bk16&st=y6ys2oz8&dl=1',
changeOrigin: true,
rewrite: () => '',
followRedirects: true
} }
} }
}, },