From 4040d3a81a2381c4dec138bca7aa8f2d62cc2a06 Mon Sep 17 00:00:00 2001 From: tomato6966 Date: Sun, 22 Dec 2024 00:23:33 +0100 Subject: [PATCH] cors proxy --- .env | 3 --- .github/workflows/deploy.yml | 4 ---- ...ooFinanceService.tsx => yahooFinanceService.ts} | 14 +++++--------- vite.config.ts | 8 ++++---- 4 files changed, 9 insertions(+), 20 deletions(-) delete mode 100644 .env rename src/services/{yahooFinanceService.tsx => yahooFinanceService.ts} (89%) diff --git a/.env b/.env deleted file mode 100644 index 94a5fd7..0000000 --- a/.env +++ /dev/null @@ -1,3 +0,0 @@ - -VITE_YAHOO_API_URL=https://query1.finance.yahoo.com -VITE_YAHOO_ORIGIN=https://finance.yahoo.com diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 282a291..e471b7c 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -37,10 +37,6 @@ jobs: - name: Build run: npm run build env: - VITE_BASE_URL: '/${{ github.event.repository.name }}' - VITE_YAHOO_API_URL: 'https://query1.finance.yahoo.com' - VITE_YAHOO_ORIGIN: 'https://tomato6966.github.io/' - - name: Create 404.html run: cp dist/index.html dist/404.html diff --git a/src/services/yahooFinanceService.tsx b/src/services/yahooFinanceService.ts similarity index 89% rename from src/services/yahooFinanceService.tsx rename to src/services/yahooFinanceService.ts index 55096e9..4e8f01f 100644 --- a/src/services/yahooFinanceService.tsx +++ b/src/services/yahooFinanceService.ts @@ -38,7 +38,9 @@ interface YahooChartResult { } const isDev = import.meta.env.DEV; -const API_BASE = isDev ? '/yahoo' : 'https://query1.finance.yahoo.com'; +const CORS_PROXY = 'https://corsproxy.io/?url='; +const YAHOO_API = 'https://query1.finance.yahoo.com'; +const API_BASE = isDev ? '/yahoo' : `${CORS_PROXY}${encodeURIComponent(YAHOO_API)}`; export const searchAssets = async (query: string): Promise => { try { @@ -48,10 +50,7 @@ export const searchAssets = async (query: string): Promise => { type: 'equity,etf', }); - const url = isDev - ? `${API_BASE}/v1/finance/lookup?${params}` - : `${API_BASE}/v1/finance/lookup?${params}`; - + const url = `${API_BASE}/v1/finance/lookup?${params}`; const response = await fetch(url); if (!response.ok) throw new Error('Network response was not ok'); @@ -97,10 +96,7 @@ export const getHistoricalData = async (symbol: string, startDate: string, endDa interval: '1d', }); - const url = isDev - ? `/yahoo/v8/finance/chart/${symbol}?${params}` - : `${API_BASE}/v8/finance/chart/${symbol}?${params}`; - + const url = `${API_BASE}/v8/finance/chart/${symbol}?${params}`; const response = await fetch(url); if (!response.ok) throw new Error('Network response was not ok'); diff --git a/vite.config.ts b/vite.config.ts index 54d6982..3f58647 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -12,8 +12,8 @@ export default defineConfig(({ mode }) => { optimizeDeps: { exclude: ['lucide-react'], }, - server: { - proxy: isDev ? { + server: isDev ? { + proxy: { '/yahoo': { target: 'https://query1.finance.yahoo.com', changeOrigin: true, @@ -22,8 +22,8 @@ export default defineConfig(({ mode }) => { 'Origin': 'https://finance.yahoo.com' } } - } : undefined - }, + } + } : undefined, base: env.VITE_BASE_URL || '/', }; });