(request: NextRequest)
| 11 | const EXCLUDED_PATHS = ['/api/docs', '/api/openapi.json', '/api/screenshot', '/api/github/stars', '/api/auth', '/_next', '/favicon.ico']; |
| 12 | |
| 13 | export function proxy(request: NextRequest) { |
| 14 | const { pathname } = request.nextUrl; |
| 15 | |
| 16 | if (EXCLUDED_PATHS.some(path => pathname.startsWith(path))) { |
| 17 | return NextResponse.next(); |
| 18 | } |
| 19 | |
| 20 | if (pathname.startsWith('/api/')) { |
| 21 | const origin = request.headers.get('origin') || ''; |
| 22 | const apiKey = request.headers.get('x-api-key'); |
| 23 | |
| 24 | if (!Settings.DEBUG && !apiKey) { |
| 25 | return NextResponse.json( |
| 26 | { detail: 'API Key header is missing' }, |
| 27 | { status: 401 } |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | if (!Settings.DEBUG && apiKey && !Settings.API_KEYS.includes(apiKey)) { |
| 32 | return NextResponse.json( |
| 33 | { detail: 'Invalid API Key' }, |
| 34 | { status: 403 } |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | const response = NextResponse.next(); |
| 39 | |
| 40 | if (ALLOWED_ORIGINS.includes(origin)) { |
| 41 | response.headers.set('Access-Control-Allow-Origin', origin); |
| 42 | } else if (Settings.DEBUG) { |
| 43 | response.headers.set('Access-Control-Allow-Origin', '*'); |
| 44 | } |
| 45 | |
| 46 | response.headers.set('Access-Control-Allow-Credentials', 'true'); |
| 47 | response.headers.set('Access-Control-Allow-Methods', 'GET, OPTIONS'); |
| 48 | response.headers.set('Access-Control-Allow-Headers', 'Authorization, Content-Type, X-API-Key'); |
| 49 | response.headers.set('Access-Control-Max-Age', '600'); |
| 50 | |
| 51 | if (request.method === 'OPTIONS') { |
| 52 | return new NextResponse(null, { status: 200, headers: response.headers }); |
| 53 | } |
| 54 | |
| 55 | return response; |
| 56 | } |
| 57 | |
| 58 | return NextResponse.next(); |
| 59 | } |
| 60 | |
| 61 | export const config = { |
| 62 | matcher: ['/api/:path*'], |
nothing calls this directly
no outgoing calls
no test coverage detected