(req: HTTPRequestLike, res: HTTPResponseLike)
| 218 | } |
| 219 | |
| 220 | private async handleRequest(req: HTTPRequestLike, res: HTTPResponseLike): Promise<void> { |
| 221 | try { |
| 222 | if (!this.applyCORSPolicy(req, res)) { |
| 223 | res.statusCode = 403; |
| 224 | res.setHeader("Content-Type", "application/json"); |
| 225 | res.end(JSON.stringify(this.errorResponse("CORS origin is not allowed"))); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | // Handle CORS preflight requests |
| 230 | if (req.method === "OPTIONS") { |
| 231 | await this.handleCORSPreflight(req, res); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | // Parse URL for authentication check |
| 236 | const pathname = parseRequestUrl(req).pathname; |
| 237 | |
| 238 | // Handle MCP endpoint |
| 239 | if (pathname === "/mcp") { |
| 240 | if (!this.mcpService) { |
| 241 | this.sendResponse(res, 404, this.errorResponse("MCP server is not enabled")); |
| 242 | return; |
| 243 | } |
| 244 | if (!this.authenticate(req)) { |
| 245 | this.sendResponse(res, 401, this.errorResponse("Authentication required")); |
| 246 | return; |
| 247 | } |
| 248 | const body = await this.parseBody(req); |
| 249 | await this.mcpService.handleRequest(req, res, body); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | // Check authentication for API routes |
| 254 | if (pathname.startsWith("/api/") && !this.authenticate(req)) { |
| 255 | this.sendResponse(res, 401, this.errorResponse("Authentication required")); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | // Try to route the request |
| 260 | const handled = await this.router.route(req, res); |
| 261 | |
| 262 | // If no route was found, return 404 |
| 263 | if (!handled) { |
| 264 | this.sendResponse(res, 404, this.errorResponse("Not found")); |
| 265 | } |
| 266 | } catch (error: unknown) { |
| 267 | tasknotesLogger.error("API Error:", { |
| 268 | category: "provider", |
| 269 | operation: "api", |
| 270 | error: error, |
| 271 | }); |
| 272 | this.sendResponse(res, 500, this.errorResponse("Internal server error")); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Webhook interface implementation - delegate to WebhookController |
| 277 | async triggerWebhook(event: WebhookEvent, data: unknown): Promise<void> { |
no test coverage detected