import type { MessageDto } from "./sessions"; import { request } from "./core"; /** * Single-message mutations (design/16 §4.2 D3, Docs/api-client.md §6a/6b). * Both succeed against the persisted row and broadcast the change over the WS * hub (`message_edited` / `message_deleted`), so other clients converge without * a re-fetch; the caller's REST response is only the acknowledgement. */ /** The gateway answers PATCH with `{data: }` (see * gateway/history_write.go `writeJSON(w, 200, toHistoryMessage(msg))` and * history_write_broadcast_test.go, which decodes `data` straight into the DTO). * Docs/api-client.md §6a writes that envelope as `{data:{message}}`; the optional * `message` field keeps this call working either way, so a plain DTO is never * mistaken for an empty result (which would blank the bubble). */ type PatchMessageEnvelope = MessageDto & { message?: MessageDto }; /** PATCH /api/v1/messages/{id} — replace one message's `content`. The backend * validates non-empty `≤4000` runes (400) and existence (404), and only ever * changes `content`; role / timestamps / platform are preserved. */ export async function patchMessage(id: string, content: string): Promise { const res = await request( `/messages/${encodeURIComponent(id)}`, { method: "PATCH", body: JSON.stringify({ content }) } ); return res.message ?? res; } /** DELETE /api/v1/messages/{id} — remove one message (404 when already gone). * The response echoes the deleted id, so a caller can assert what it removed. */ export function deleteMessage(id: string): Promise<{ deleted: string }> { return request<{ deleted: string }>(`/messages/${encodeURIComponent(id)}`, { method: "DELETE", }); }