borsa / nextjs-app /src /lib /api-response.ts
hfpush
fix: add ai/sentiment_model.py facade
c3c81a6
import { NextResponse } from 'next/server'
/**
* Standard API response helpers.
* All API routes should use these for consistent response envelopes.
*
* Success: { ok: true, ...data }
* Error: { ok: false, error: string, ...extra }
*/
export function apiSuccess<T extends Record<string, unknown>>(
data: T,
status = 200
): NextResponse {
return NextResponse.json({ ok: true, ...data }, { status })
}
export function apiError(
error: string,
status = 500,
extra?: Record<string, unknown>
): NextResponse {
return NextResponse.json({ ok: false, error, ...extra }, { status })
}
/** Common error responses */
export const ApiErrors = {
noApiBase: () =>
apiError('API URL yapılandırılmamış. NEXT_PUBLIC_API_URL tanımlayın.', 500),
invalidJson: () => apiError('Geçersiz JSON body', 400),
bodyRequired: () => apiError('JSON body gerekli', 400),
invalidSymbol: () =>
apiError('Geçerli bir symbol alanı gerekli (ör: THYAO)', 400),
upstream: (route: string, status: number, detail?: unknown) =>
apiError(`Upstream ${route} error: ${status}`, status >= 400 ? status : 502, {
detail: detail ?? null,
}),
upstreamUnavailable: (
route: string,
options?: {
detail?: unknown
rootError?: string
hint?: string
}
) =>
NextResponse.json(
{
ok: false,
error: options?.rootError || `${route} backend geçici olarak kullanılamıyor`,
detail: options?.detail ?? null,
hint:
options?.hint ||
'HF backend geçici olarak ulaşılamaz durumda. Biraz sonra tekrar deneyin.',
unavailable: true,
},
{ status: 200 }
),
timeout: (route: string) =>
apiError(`${route} isteği zaman aşımına uğradı`, 504),
unknown: (msg?: string) =>
apiError(msg || 'Bilinmeyen hata', 502),
} as const