mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-10-02 13:39:41 +02:00
Point 1 of the v1.0 lock-in: commit to a stable HTTP API (the cleanest, fullest form).
Core (centralized):
- Every JSON /api response now uses ONE envelope via a Fastify preSerialization hook (src/web/server.ts): success -> { success:true, data:<payload> }; error -> { success:false, error, errorCode } with a conventional HTTP status. Non-JSON routes (file-raw, tail-file SSE, download, screenshots, /q redirect, WS) are skipped.
- Error-code -> HTTP status is a single source of truth (httpStatusForErrorCode in src/types/api.ts): 400/401/404/409/422/429/500. Expanded ApiErrorCode (added UNAUTHORIZED, CONFLICT, RATE_LIMITED). Errors are no longer HTTP 200.
- Versioned alias: /api/v1/* rewrites to /api/* (rewriteApiV1Url), so external clients pin to a stable surface while the bundled UI keeps using /api/*.
- Handlers stripped of manual 'success:true' (50 across 14 route files) so they return bare payloads the hook wraps uniformly; fixed the mux DELETE {success:<bool>} envelope collision (-> {killed}).
Frontend (48 call sites across 10 files):
- _apiJson() auto-unwraps { success:true, data } -> data (null on error), so most bare-shape readers are transparent. Raw-fetch sites relocate payload reads under .data; success/res.ok/error checks unchanged.
Docs: new docs/api-reference.md (envelope, status table, error codes, /api/v1, SSE); versioning-policy.md flipped — the HTTP/SSE API is now part of the stable, SemVer-covered surface.
Verification: full unit/route suite green (2680 passed) incl. ~166 updated assertions across 24 test files; typecheck/lint/format/frontend-syntax clean; a headless-chromium smoke loaded the migrated UI and drove the panels with 0 console/page errors; /api/status and /api/v1/status confirmed returning the uniform envelope live.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
92 lines
2.8 KiB
JavaScript
92 lines
2.8 KiB
JavaScript
/**
|
|
* @fileoverview Centralized API fetch helpers mixed into CodemanApp.prototype.
|
|
*
|
|
* Provides _api(), _apiJson(), _apiPost(), _apiPut(), _apiDelete() methods that handle
|
|
* JSON serialization, Content-Type headers, and error swallowing. All API calls in the
|
|
* frontend route through these helpers.
|
|
*
|
|
* @mixin Extends CodemanApp.prototype via Object.assign
|
|
* @dependency app.js (CodemanApp class must be defined)
|
|
* @loadorder 14 of 15 — loaded after ralph-wizard.js
|
|
*/
|
|
|
|
// Codeman — Centralized API fetch helpers for CodemanApp
|
|
// Loaded after app.js (needs CodemanApp class defined)
|
|
|
|
Object.assign(CodemanApp.prototype, {
|
|
/**
|
|
* Send a JSON API request. Handles Content-Type, JSON serialization, and error swallowing.
|
|
* @param {string} path - API path (e.g., '/api/sessions/123/input')
|
|
* @param {object} [opts] - { method, body, signal }
|
|
* @returns {Promise<Response|null>} Response or null on error
|
|
*/
|
|
async _api(path, opts = {}) {
|
|
const { method = 'GET', body, signal } = opts;
|
|
const fetchOpts = { method, signal };
|
|
if (body !== undefined) {
|
|
fetchOpts.headers = { 'Content-Type': 'application/json' };
|
|
fetchOpts.body = JSON.stringify(body);
|
|
}
|
|
try {
|
|
const res = await fetch(path, fetchOpts);
|
|
return res;
|
|
} catch {
|
|
return null;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Send a JSON API request and parse the response as JSON.
|
|
* @param {string} path - API path
|
|
* @param {object} [opts] - { method, body, signal }
|
|
* @returns {Promise<any|null>} Parsed JSON or null on error
|
|
*/
|
|
async _apiJson(path, opts = {}) {
|
|
const res = await this._api(path, opts);
|
|
if (!res || !res.ok) return null;
|
|
let body;
|
|
try {
|
|
body = await res.json();
|
|
} catch {
|
|
return null;
|
|
}
|
|
// Uniform API envelope (stable HTTP contract): unwrap { success:true, data } → data;
|
|
// { success:false } → null (errors also surface as a non-ok HTTP status above).
|
|
// Legacy/bare bodies pass through unchanged.
|
|
if (body && typeof body === 'object') {
|
|
if (body.success === false) return null;
|
|
if (body.success === true && 'data' in body) return body.data;
|
|
}
|
|
return body;
|
|
},
|
|
|
|
/**
|
|
* POST JSON to an API endpoint (most common pattern).
|
|
* @param {string} path - API path
|
|
* @param {object} body - JSON body
|
|
* @returns {Promise<Response|null>}
|
|
*/
|
|
async _apiPost(path, body) {
|
|
return this._api(path, { method: 'POST', body });
|
|
},
|
|
|
|
/**
|
|
* PUT JSON to an API endpoint.
|
|
* @param {string} path - API path
|
|
* @param {object} body - JSON body
|
|
* @returns {Promise<Response|null>}
|
|
*/
|
|
async _apiPut(path, body) {
|
|
return this._api(path, { method: 'PUT', body });
|
|
},
|
|
|
|
/**
|
|
* DELETE an API resource.
|
|
* @param {string} path - API path
|
|
* @returns {Promise<Response|null>}
|
|
*/
|
|
async _apiDelete(path) {
|
|
return this._api(path, { method: 'DELETE' });
|
|
},
|
|
});
|