* 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 )
| 330 | * only confuse the person poking at their own API. |
| 331 | */ |
| 332 | async 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. |
no test coverage detected