| 9 | return { |
| 10 | name: "local-api-server", |
| 11 | configureServer(server: any) { |
| 12 | server.middlewares.use(async (req: any, res: any, next: any) => { |
| 13 | const parsedUrl = urlParse(req.url || "", true); |
| 14 | const pathname = parsedUrl.pathname || ""; |
| 15 | |
| 16 | if (pathname.startsWith("/api/")) { |
| 17 | // Exclude already proxied paths |
| 18 | if ( |
| 19 | pathname.startsWith("/api/github-zip") || |
| 20 | pathname.startsWith("/api/pypi") |
| 21 | ) { |
| 22 | return next(); |
| 23 | } |
| 24 | |
| 25 | if (pathname.startsWith("/api/gitlab-zip/")) { |
| 26 | try { |
| 27 | const modulePath = path.resolve(__dirname, "./api/gitlab-zip.ts"); |
| 28 | const apiModule = await server.ssrLoadModule(modulePath); |
| 29 | const handler = apiModule.default || apiModule; |
| 30 | req.query = parsedUrl.query; |
| 31 | res.status = (code: number) => { |
| 32 | res.statusCode = code; |
| 33 | return res; |
| 34 | }; |
| 35 | res.json = (data: any) => { |
| 36 | if (!res.headersSent) { |
| 37 | res.setHeader("Content-Type", "application/json"); |
| 38 | } |
| 39 | res.end(JSON.stringify(data)); |
| 40 | return res; |
| 41 | }; |
| 42 | await handler(req, res); |
| 43 | } catch (err: any) { |
| 44 | console.error("GitLab zip API error:", err); |
| 45 | if (!res.headersSent) { |
| 46 | res.statusCode = 500; |
| 47 | res.end(JSON.stringify({ error: "GitLab zip proxy failed", details: err.message })); |
| 48 | } |
| 49 | } |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | const apiName = pathname.replace("/api/", ""); |
| 54 | try { |
| 55 | const modulePath = path.resolve(__dirname, `./api/${apiName}.ts`); |
| 56 | const exists = await fs.access(modulePath).then(() => true).catch(() => false); |
| 57 | if (!exists) { |
| 58 | res.statusCode = 404; |
| 59 | res.end(JSON.stringify({ error: `API endpoint /api/${apiName} not found` })); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // Load the API module using Vite's SSR loader (transpiles TS/ESM automatically) |
| 64 | const apiModule = await server.ssrLoadModule(modulePath); |
| 65 | const handler = apiModule.default || apiModule; |
| 66 | |
| 67 | // Mock Vercel request & response properties |
| 68 | req.query = parsedUrl.query; |