mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 11:45:23 +02:00
refactor: simplified data loading with direct fetch endpoint
This commit is contained in:
@@ -1,67 +0,0 @@
|
||||
import type { VercelRequest, VercelResponse } from '@vercel/node';
|
||||
import fetch from 'node-fetch'; // Ensure fetch is available
|
||||
|
||||
export default async function handler(req: VercelRequest, res: VercelResponse) {
|
||||
// Allow CORS
|
||||
res.setHeader('Access-Control-Allow-Credentials', 'true');
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
res.setHeader('Access-Control-Allow-Methods', 'GET,OPTIONS,PATCH,DELETE,POST,PUT');
|
||||
res.setHeader(
|
||||
'Access-Control-Allow-Headers',
|
||||
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'
|
||||
);
|
||||
|
||||
if (req.method === 'OPTIONS') {
|
||||
res.status(200).end();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. Reconstruct the Target Dropbox URL
|
||||
// The incoming request is to /api/dropbox/scl/fi/...
|
||||
// We want to fetch https://www.dropbox.com/scl/fi/...
|
||||
|
||||
// req.url in Vercel function might be just the suffix or full path depending on routing
|
||||
// Typically: /api/dropbox/scl/fi/xyz?dl=1
|
||||
|
||||
// We need to strip '/api/dropbox' from the start to get the dropbox path
|
||||
const requestPath = req.url || '';
|
||||
const dropboxPath = requestPath.replace(/^\/api\/dropbox/, ''); // e.g. /scl/fi/xyz?dl=1
|
||||
|
||||
let targetUrl = `https://www.dropbox.com${dropboxPath}`;
|
||||
|
||||
// Ensure dl=1 is present to force download
|
||||
if (targetUrl.includes('?')) {
|
||||
if (!targetUrl.includes('dl=1')) {
|
||||
targetUrl = targetUrl.replace('dl=0', 'dl=1');
|
||||
if (!targetUrl.includes('dl=1')) {
|
||||
targetUrl += '&dl=1';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
targetUrl += '?dl=1';
|
||||
}
|
||||
|
||||
console.log(`[Proxy] Incoming: ${req.url}`);
|
||||
console.log(`[Proxy] Target: ${targetUrl}`);
|
||||
|
||||
const response = await fetch(targetUrl);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Dropbox responded with ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
// Forward the content type (likely text/csv)
|
||||
const contentType = response.headers.get('content-type');
|
||||
if (contentType) {
|
||||
res.setHeader('Content-Type', contentType);
|
||||
}
|
||||
|
||||
const data = await response.text();
|
||||
res.status(200).send(data);
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('Proxy Error:', error);
|
||||
res.status(500).json({ error: 'Failed to fetch data from Dropbox', details: error.message });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import type { VercelRequest, VercelResponse } from '@vercel/node';
|
||||
|
||||
const DROPBOX_URL = "https://www.dropbox.com/scl/fi/b9zxn4z5i7sxwfakk5g5y/Amazon-Sell-Out-2023-2025.csv?rlkey=uoto6v0mm99py8nszy8ldtez8&st=pzn1zkrg&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-data] Fetching from Dropbox...');
|
||||
const response = await fetch(DROPBOX_URL);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Dropbox responded with ${response.status}`);
|
||||
}
|
||||
|
||||
const csvData = await response.text();
|
||||
console.log('[fetch-data] Successfully fetched CSV, size:', csvData.length);
|
||||
|
||||
res.setHeader('Content-Type', 'text/csv');
|
||||
res.status(200).send(csvData);
|
||||
} catch (error: any) {
|
||||
console.error('[fetch-data] Error:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user