MotorSignal-App/website/app/api/ops/infrastructure/route.ts

66 lines
1.8 KiB
TypeScript

import { cookies } from 'next/headers';
import { NextRequest, NextResponse } from 'next/server';
import { normalizeInfrastructureResponse } from '../../../../lib/ops-data';
export async function GET(request: NextRequest) {
const cookieStore = await cookies();
const isProduction =
process.env.NODE_ENV === 'production' &&
process.env.NEXT_PUBLIC_APP_ENV === 'production';
const opsToken = isProduction
? cookieStore.get('__Host-motorsignal_ops')?.value
: cookieStore.get('__Host-motorsignal_ops')?.value ??
cookieStore.get('motorsignal_ops')?.value;
if (!opsToken) {
return NextResponse.json(
{ detail: 'Authentication required.' },
{ status: 401 }
);
}
const baseUrl =
process.env.MOTORSIGNAL_API_BASE_URL ?? 'https://api.motorsignal.app';
const headers: Record<string, string> = {
accept: 'application/json',
authorization: `Bearer ${opsToken}`,
};
try {
const response = await fetch(`${baseUrl}/api/v1/ops/infrastructure`, {
method: 'GET',
headers,
cache: 'no-store',
});
if (!response.ok) {
if (response.status === 401) {
return NextResponse.json(
{ detail: 'Authentication required.' },
{ status: 401 }
);
}
if (response.status === 403) {
return NextResponse.json(
{ detail: 'Access restricted.' },
{ status: 403 }
);
}
return NextResponse.json(
normalizeInfrastructureResponse(null),
{ status: 503 }
);
}
const data = await response.json();
return NextResponse.json(normalizeInfrastructureResponse(data), {
status: 200,
});
} catch {
return NextResponse.json(
normalizeInfrastructureResponse(null),
{ status: 503 }
);
}
}