borsa / nextjs-app /src /lib /api-response.ts
veteroner's picture
feat: live position monitoring with charts + trading system production ready
656ac31
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,
}),
timeout: (route: string) =>
apiError(`${route} isteği zaman aşımına uğradı`, 504),
unknown: (msg?: string) =>
apiError(msg || 'Bilinmeyen hata', 502),
} as const