MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / handler

Function handler

src/ui-server/api/index.ts:257–309  ·  view source on GitHub ↗
(req, res, ctx)

Source from the content-addressed store, hash-verified

255 // Async because `/api/source` highlights: everything else answers straight
256 // out of SQLite and resolves on the same tick.
257 const handler: UiApiHandler = async (req, res, ctx) => {
258 const route = normalize(ctx.pathname);
259 try {
260 // Writes first: they are the only requests that carry a body, and
261 // routing them beside the readers would put a `case` that mutates in a
262 // switch every other arm of which is a query.
263 if (ctx.method === 'POST' || ctx.method === 'DELETE') {
264 return await dispatchWrite(route, req, res, ctx, session, trails);
265 }
266 switch (route) {
267 case '/api':
268 return ok(res, API_INDEX, ctx.method);
269 case '/api/stats':
270 return ok(res, buildStats(session.acquire(), ctx.projectRoot), ctx.method);
271 case '/api/search':
272 return ok(res, buildSearch(session.acquire(), ctx.query), ctx.method);
273 case '/api/routes':
274 return ok(res, buildRoutes(session.acquire(), ctx.query), ctx.method);
275 case '/api/map':
276 return ok(res, buildMap(session.acquire(), ctx.projectRoot, ctx.query), ctx.method);
277 case '/api/screens':
278 return ok(res, await buildScreens(session.acquire(), ctx.projectRoot), ctx.method);
279 case '/api/steps':
280 return ok(res, await buildSteps(session.acquire(), ctx.projectRoot, ctx.query), ctx.method);
281 case '/api/deadcode':
282 return ok(res, buildDeadCode(session.acquire(), ctx.projectRoot, ctx.query), ctx.method);
283 case '/api/entrypoints':
284 return ok(res, buildEntryPoints(session.acquire(), ctx.query), ctx.method);
285 case '/api/trails':
286 return ok(res, buildTrails(session.acquire(), ctx.projectRoot, trails), ctx.method);
287 case '/api/nodes':
288 return ok(res, buildNodeRefs(session.acquire(), ctx.query), ctx.method);
289 case '/api/source':
290 return ok(res, await buildSource(session.acquire(), ctx.projectRoot, ctx.query), ctx.method);
291 case '/api/flow':
292 return ok(res, await buildFlow(session.acquire(), ctx.projectRoot, ctx.query), ctx.method);
293 case '/api/events':
294 // Streams instead of answering: it writes its own headers and keeps
295 // the socket open, so it never goes through `ok()`.
296 return events.subscribe(req, res, ctx.method);
297 default:
298 return await dispatchPathRoutes(route, res, ctx, session);
299 }
300 } catch (err) {
301 // A refusal from the read chokepoint is a 403 with the reason attached —
302 // the request asked for something outside the project, and there is no
303 // version of it we would serve.
304 if (err instanceof PathRefusalError) {
305 return fail(res, new ApiError('refused', err.message), ctx.method);
306 }
307 return fail(res, err, ctx.method);
308 }
309 };
310
311 return {
312 handler,

Callers

nothing calls this directly

Calls 15

okFunction · 0.90
buildStatsFunction · 0.90
buildSearchFunction · 0.90
buildRoutesFunction · 0.90
buildMapFunction · 0.90
buildScreensFunction · 0.90
buildStepsFunction · 0.90
buildDeadCodeFunction · 0.90
buildEntryPointsFunction · 0.90
buildTrailsFunction · 0.90
buildNodeRefsFunction · 0.90
buildSourceFunction · 0.90

Tested by

no test coverage detected