| import { NextRequest, NextResponse } from 'next/server' |
|
|
| import { API_BASE } from '@/lib/runtime-config' |
| import { requireAuth } from '@/lib/api-auth' |
|
|
| export const dynamic = 'force-dynamic' |
|
|
| export async function GET(req: NextRequest) { |
| const auth = await requireAuth(req) |
| if (!auth.authenticated) return auth.response |
|
|
| if (!API_BASE) { |
| return NextResponse.json( |
| { ok: false, error: 'API URL yapılandırılmamış. NEXT_PUBLIC_API_URL tanımlayın.' }, |
| { status: 500 } |
| ) |
| } |
|
|
| try { |
| const { searchParams } = new URL(req.url) |
| const symbol = (searchParams.get('symbol') || '').trim().toUpperCase() |
| const period = (searchParams.get('period') || '6mo').trim() |
| const interval = (searchParams.get('interval') || '1d').trim() |
|
|
| if (!symbol) { |
| return NextResponse.json({ ok: false, error: 'symbol is required' }, { status: 400 }) |
| } |
|
|
| const qs = new URLSearchParams({ symbol, period, interval }) |
|
|
| const controller = new AbortController() |
| const timeout = setTimeout(() => controller.abort(), 20000) |
|
|
| let resp: Response |
| try { |
| resp = await fetch(`${API_BASE}/api/stock-data?${qs.toString()}`, { |
| headers: { accept: 'application/json' }, |
| cache: 'no-store', |
| signal: controller.signal, |
| }) |
| } finally { |
| clearTimeout(timeout) |
| } |
|
|
| const payload = await resp.json().catch(() => null) |
| if (!resp.ok) { |
| return NextResponse.json( |
| { ok: false, error: `Upstream stock-data error: ${resp.status}`, detail: payload }, |
| { status: resp.status } |
| ) |
| } |
|
|
| return NextResponse.json(payload ?? { ok: false, error: 'Invalid upstream payload' }, { status: 200 }) |
| } catch (e: unknown) { |
| const message = e instanceof Error ? e.message : 'Unknown error' |
| return NextResponse.json({ ok: false, error: message }, { status: 502 }) |
| } |
| } |
|
|