|
| 1 | +import { NextResponse } from "next/server"; |
| 2 | +import { |
| 3 | + getApiBaseUrl, |
| 4 | + getUpstreamAuthHeaders, |
| 5 | + isMockFallbackAllowed, |
| 6 | +} from "@/lib/api/config"; |
| 7 | +import { getApiKeyUsage } from "@/mock-data/api-key-usage"; |
| 8 | + |
| 9 | +/** 503 returned instead of mock data when no backend is configured in production. */ |
| 10 | +function backendUnavailableResponse() { |
| 11 | + return NextResponse.json( |
| 12 | + { |
| 13 | + error: "backend_unavailable", |
| 14 | + message: |
| 15 | + "No API key usage backend is configured for this production deployment. Set NEXT_PUBLIC_API_URL.", |
| 16 | + }, |
| 17 | + { status: 503 }, |
| 18 | + ); |
| 19 | +} |
| 20 | + |
| 21 | +type RouteContext = { |
| 22 | + params: |
| 23 | + | { |
| 24 | + id: string; |
| 25 | + } |
| 26 | + | Promise<{ |
| 27 | + id: string; |
| 28 | + }>; |
| 29 | +}; |
| 30 | + |
| 31 | +function backendHeaders(): Record<string, string> { |
| 32 | + return { |
| 33 | + "content-type": "application/json", |
| 34 | + ...getUpstreamAuthHeaders(), |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * GET /api/api-keys/[id]/usage |
| 40 | + * |
| 41 | + * Per-key usage analytics (roadmap item: "Per-key usage analytics"). |
| 42 | + * Proxies to the configured backend's real usage metrics |
| 43 | + * (NEXT_PUBLIC_API_URL or legacy aliases). Falls back to the local mock |
| 44 | + * generator only outside production and only when no backend is configured, |
| 45 | + * so local dev/CI keeps working without a running API server — mirrors the |
| 46 | + * production-safety rule used by `/api/api-keys` and `/api/wallets/[id]`. |
| 47 | + */ |
| 48 | +export async function GET(request: Request, { params }: RouteContext) { |
| 49 | + const { id } = await params; |
| 50 | + const apiKeyId = id.trim(); |
| 51 | + |
| 52 | + if (!apiKeyId) { |
| 53 | + return NextResponse.json({ error: "invalid_id" }, { status: 400 }); |
| 54 | + } |
| 55 | + |
| 56 | + const backendUrl = getApiBaseUrl(); |
| 57 | + |
| 58 | + if (backendUrl) { |
| 59 | + try { |
| 60 | + const upstream = await fetch( |
| 61 | + `${backendUrl}/api-keys/${encodeURIComponent(apiKeyId)}/usage`, |
| 62 | + { |
| 63 | + headers: backendHeaders(), |
| 64 | + cache: "no-store", |
| 65 | + }, |
| 66 | + ); |
| 67 | + const data = await upstream.json().catch(() => null); |
| 68 | + |
| 69 | + if (!upstream.ok || data === null) { |
| 70 | + return NextResponse.json( |
| 71 | + { error: "Unable to load API key usage from the backend" }, |
| 72 | + { status: upstream.status || 502 }, |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + return NextResponse.json({ data }); |
| 77 | + } catch { |
| 78 | + return NextResponse.json( |
| 79 | + { error: "Unable to reach the API key usage backend" }, |
| 80 | + { status: 502 }, |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (!isMockFallbackAllowed()) { |
| 86 | + return backendUnavailableResponse(); |
| 87 | + } |
| 88 | + |
| 89 | + // --- Mock fallback (no NEXT_PUBLIC_API_URL set, non-production only) --- |
| 90 | + return NextResponse.json({ data: getApiKeyUsage(apiKeyId) }); |
| 91 | +} |
0 commit comments