| 117 | const apiUrl = options?.apiUrl ?? DEFAULT_API_URL; |
| 118 | |
| 119 | const handler = async function handler(req: Request): Promise<NextResponse> { |
| 120 | const url = new URL(req.url); |
| 121 | const pathname = url.pathname; |
| 122 | const method = req.method; |
| 123 | |
| 124 | // Handle script proxy: GET /op1.js |
| 125 | if (method === 'GET' && pathname.endsWith(SCRIPT_PATH)) { |
| 126 | return handleScriptProxyRoute(req); |
| 127 | } |
| 128 | |
| 129 | const apiPathMatch = pathname.indexOf('/track'); |
| 130 | if (apiPathMatch === -1) { |
| 131 | return NextResponse.json({ error: 'Not found' }, { status: 404 }); |
| 132 | } |
| 133 | |
| 134 | const apiPath = pathname.substring(apiPathMatch); |
| 135 | return handleApiRoute(req, apiUrl, apiPath); |
| 136 | }; |
| 137 | |
| 138 | handler.GET = handler; |
| 139 | handler.POST = handler; |