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

Function createGraphApi

src/ui-server/api/index.ts:242–320  ·  view source on GitHub ↗
(options: GraphApiOptions)

Source from the content-addressed store, hash-verified

240};
241
242export function createGraphApi(options: GraphApiOptions): GraphApi {
243 const session = new GraphSession(options.projectRoot);
244 // Watches nothing until a browser subscribes, and stops again when the last
245 // one goes away — mounting the API costs no watch descriptors.
246 const events = new EventHub(options.projectRoot, session);
247 const trails: TrailsOptions = {
248 readOnly: options.readOnly === true,
249 readOnlyReason:
250 options.readOnly === true
251 ? options.readOnlyReason ?? 'This viewer is running read-only, so trails cannot be saved.'
252 : null,
253 };
254
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 }

Callers 9

serveFunction · 0.90
ui-trails.test.tsFile · 0.90
dead-code.test.tsFile · 0.90
ui-map-api.test.tsFile · 0.90
mainFunction · 0.85

Calls 2

closeMethod · 0.95
closeMethod · 0.95

Tested by 1

serveFunction · 0.72