Changeset 0.37.14 (#226)
Co-authored-by: gobha <jasafpro@gmail.com> Co-committed-by: gobha <jasafpro@gmail.com>
This commit is contained in:
@@ -227,16 +227,35 @@
|
||||
};
|
||||
|
||||
// ─── API Wrappers ───────────────────────────────────────────
|
||||
// API._get/_post/_put already prepend __BASE__. No base prefix here.
|
||||
// v0.37.14: raw fetch — old API._* globals removed in scorched earth.
|
||||
|
||||
T.apiGet = async function (path) { return await API._get('/api/v1' + path); };
|
||||
T.apiPost = async function (path, body) { return await API._post('/api/v1' + path, body); };
|
||||
T.apiPut = async function (path, body) { return await API._put('/api/v1' + path, body); };
|
||||
async function _fetchJSON(method, path, body) {
|
||||
var token = await T.getAuthToken();
|
||||
var opts = {
|
||||
method: method,
|
||||
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
|
||||
credentials: 'same-origin'
|
||||
};
|
||||
if (body !== undefined && method !== 'GET') opts.body = JSON.stringify(body);
|
||||
var resp = await fetch(T.base + '/api/v1' + path, opts);
|
||||
if (resp.status === 204) return { _status: 204 };
|
||||
var text = await resp.text();
|
||||
var data;
|
||||
try { data = text ? JSON.parse(text) : {}; } catch (e) { data = { _raw: text }; }
|
||||
data._status = resp.status;
|
||||
if (!resp.ok) {
|
||||
var err = new Error(method + ' ' + path + ' → ' + resp.status + (data.error ? ': ' + data.error : ''));
|
||||
err.status = resp.status;
|
||||
err.data = data;
|
||||
throw err;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
T.apiPatch = async function (path, body) {
|
||||
if (typeof API._patch === 'function') return await API._patch('/api/v1' + path, body);
|
||||
return await API._put('/api/v1' + path, body);
|
||||
};
|
||||
T.apiGet = async function (path) { return await _fetchJSON('GET', path); };
|
||||
T.apiPost = async function (path, body) { return await _fetchJSON('POST', path, body); };
|
||||
T.apiPut = async function (path, body) { return await _fetchJSON('PUT', path, body); };
|
||||
T.apiPatch = async function (path, body) { return await _fetchJSON('PATCH', path, body); };
|
||||
|
||||
// ─── Token Capture ──────────────────────────────────────────
|
||||
|
||||
@@ -244,23 +263,8 @@
|
||||
|
||||
T.captureAuthToken = async function () {
|
||||
if (_capturedToken) return _capturedToken;
|
||||
var origFetch = window.fetch;
|
||||
window.fetch = function (url, opts) {
|
||||
if (!_capturedToken && opts && opts.headers) {
|
||||
var auth = '';
|
||||
if (opts.headers instanceof Headers) {
|
||||
auth = opts.headers.get('Authorization') || '';
|
||||
} else {
|
||||
auth = opts.headers['Authorization'] || opts.headers.authorization || '';
|
||||
}
|
||||
if (typeof auth === 'string' && auth.indexOf('Bearer ') === 0) {
|
||||
_capturedToken = auth.slice(7);
|
||||
}
|
||||
}
|
||||
return origFetch.apply(this, arguments);
|
||||
};
|
||||
try { await API._get('/api/v1/health'); } catch (e) { /* swallow */ }
|
||||
window.fetch = origFetch;
|
||||
// v0.37.14: read directly from localStorage (old API._get interceptor removed)
|
||||
_capturedToken = getAuthTokenFromStorage();
|
||||
return _capturedToken;
|
||||
};
|
||||
|
||||
@@ -291,18 +295,7 @@
|
||||
// ─── DELETE (needs raw fetch fallback) ──────────────────────
|
||||
|
||||
T.apiDelete = async function (path) {
|
||||
if (typeof API._delete === 'function') return await API._delete('/api/v1' + path);
|
||||
var token = await T.getAuthToken();
|
||||
var resp = await fetch(T.base + '/api/v1' + path, {
|
||||
method: 'DELETE',
|
||||
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
|
||||
credentials: 'same-origin'
|
||||
});
|
||||
if (resp.status === 204 || resp.status === 200) {
|
||||
var text = await resp.text();
|
||||
return text ? JSON.parse(text) : {};
|
||||
}
|
||||
throw new Error('DELETE ' + path + ' → ' + resp.status);
|
||||
return await _fetchJSON('DELETE', path);
|
||||
};
|
||||
|
||||
T.safeDelete = async function (path, retries) {
|
||||
|
||||
Reference in New Issue
Block a user