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

Function dispatchWrite

src/ui-server/api/index.ts:332–361  ·  view source on GitHub ↗

* The write half: `/api/trails` and nothing else. * * Kept to one function so the answer to "what can this server change?" is one * place a reviewer can read in full. Anything else that arrives with a write * method is a 405 naming the endpoint that does accept one — by the time this * runs, `i

(
  route: string,
  req: Parameters<UiApiHandler>[0],
  res: Parameters<UiApiHandler>[1],
  ctx: UiRequestContext,
  session: GraphSession,
  trails: TrailsOptions
)

Source from the content-addressed store, hash-verified

330 * only confuse the person poking at their own API.
331 */
332async function dispatchWrite(
333 route: string,
334 req: Parameters<UiApiHandler>[0],
335 res: Parameters<UiApiHandler>[1],
336 ctx: UiRequestContext,
337 session: GraphSession,
338 trails: TrailsOptions
339): Promise<boolean> {
340 if (ctx.method === 'POST' && route === '/api/trails') {
341 const body = await readJsonBody(req);
342 return ok(res, saveTrail(session.acquire(), ctx.projectRoot, body, trails), ctx.method);
343 }
344
345 if (ctx.method === 'DELETE') {
346 const id = suffixAfter(route, '/api/trails/');
347 if (id !== null && id !== '') {
348 return ok(res, removeTrail(session.acquire(), ctx.projectRoot, id, trails), ctx.method);
349 }
350 if (route === '/api/trails') {
351 throw badRequest('Deleting a trail needs its id: DELETE /api/trails/<id>.');
352 }
353 }
354
355 res.setHeader('Allow', 'GET, HEAD');
356 throw new ApiError(
357 'bad-request',
358 `${ctx.method} ${route} is not something this server changes.`,
359 'The only endpoint that writes is /api/trails (POST to save, DELETE /api/trails/<id> to remove).'
360 );
361}
362
363/**
364 * The two endpoints that carry their argument in the path.

Callers 1

handlerFunction · 0.85

Calls 7

readJsonBodyFunction · 0.90
okFunction · 0.90
saveTrailFunction · 0.90
removeTrailFunction · 0.90
badRequestFunction · 0.90
suffixAfterFunction · 0.85
acquireMethod · 0.45

Tested by

no test coverage detected