In-Memory Todo REST API
Medium · REST
Write runRequests(requests) that runs a list of { method, path, body? } requests against an in-memory todo API and returns [{ status, body }], one per request. A todo is { id, title, done }.
- POST /todos: body.title must be a non-empty string after trimming, else 400 { error: "Title is required" }. Creates { id, title: <trimmed>, done: false } → 201 with the todo. Ids start at 1, go up by one per created todo and are never reused.
- GET /todos → 200 with all todos in id order.
- GET /todos/:id → 200 with the todo.
- PATCH /todos/:id: may change title (same rule as POST) and/or done (must be a boolean, else 400 { error: "done must be a boolean" }). Other fields are ignored. On a 400 nothing changes; otherwise → 200 with the updated todo.
- DELETE /todos/:id → 204 with body null.
- Any other method or path → 404 { error: "Not found" }. Check this first, so PUT /todos/1 gets "Not found" even if todo 1 does not exist.
- A supported route with an unknown id → 404 { error: "Todo not found" }.
Each body must be a snapshot (a copy), like a JSON response that was already sent: later changes must not alter earlier results.