MCPcopy
hub / github.com/callumalpass/tasknotes / handleRequest

Method handleRequest

src/services/HTTPAPIService.ts:220–274  ·  view source on GitHub ↗
(req: HTTPRequestLike, res: HTTPResponseLike)

Source from the content-addressed store, hash-verified

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> {

Callers 1

startMethod · 0.95

Calls 11

applyCORSPolicyMethod · 0.95
errorResponseMethod · 0.95
handleCORSPreflightMethod · 0.95
sendResponseMethod · 0.95
authenticateMethod · 0.95
parseBodyMethod · 0.95
parseRequestUrlFunction · 0.90
setHeaderMethod · 0.80
routeMethod · 0.80
errorMethod · 0.80
endMethod · 0.65

Tested by

no test coverage detected