2026-03-03 22:40:18 +01:00
|
|
|
/**
|
|
|
|
|
* @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)
|
2026-03-14 21:24:22 +01:00
|
|
|
* @loadorder 14 of 15 — loaded after ralph-wizard.js
|
2026-03-03 22:40:18 +01:00
|
|
|
*/
|
|
|
|
|
|
2026-03-01 04:57:37 +01:00
|
|
|
// 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;
|
2026-06-10 01:30:43 +02:00
|
|
|
let body;
|
2026-03-01 04:57:37 +01:00
|
|
|
try {
|
2026-06-10 01:30:43 +02:00
|
|
|
body = await res.json();
|
2026-03-01 04:57:37 +01:00
|
|
|
} catch {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-06-10 01:30:43 +02:00
|
|
|
// 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;
|
2026-03-01 04:57:37 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 });
|
|
|
|
|
},
|
|
|
|
|
|
2026-03-01 06:05:26 +01:00
|
|
|
/**
|
|
|
|
|
* 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 });
|
|
|
|
|
},
|
|
|
|
|
|
2026-03-01 04:57:37 +01:00
|
|
|
/**
|
|
|
|
|
* DELETE an API resource.
|
|
|
|
|
* @param {string} path - API path
|
|
|
|
|
* @returns {Promise<Response|null>}
|
|
|
|
|
*/
|
|
|
|
|
async _apiDelete(path) {
|
|
|
|
|
return this._api(path, { method: 'DELETE' });
|
|
|
|
|
},
|
|
|
|
|
});
|