// Vercel API Route: app/api/codeguard/scan/route.ts
import { NextRequest, NextResponse } from 'next/server';
const CODEGUARD_API = 'https://api.codeguard.ai/v1';
export const runtime = 'edge';
export const maxDuration = 30;
export async function POST(request: NextRequest) {
try {
const { code, filename, frameworks = ['lgpd', 'gdpr'] } = await request.json();
if (!code || !filename) {
return NextResponse.json(
{ error: 'code and filename required' },
{ status: 400 }
);
}
const apiKey = process.env.CODEGUARD_API_KEY;
if (!apiKey) {
return NextResponse.json(
{ error: 'CODEGUARD_API_KEY not configured' },
{ status: 500 }
);
}
const response = await fetch(`${CODEGUARD_API}/scan`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': apiKey,
},
body: JSON.stringify({
content: Buffer.from(code).toString('base64'),
filename,
frameworks,
}),
});
if (!response.ok) {
const error = await response.json();
return NextResponse.json(
{ error: error.error || 'Scan failed' },
{ status: response.status }
);
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
return NextResponse.json(
{ error: 'Scan failed', message: error instanceof Error ? error.message : 'Unknown' },
{ status: 500 }
);
}
}