// Dragonflight API client v2.1.0 // Wraps UXP fetch() with Bearer auth + manual redirect follow (UXP strips // Authorization across origins per Adobe security policy). // Persists serverUrl + apiToken in localStorage. (function () { const API = {}; const LS_URL = 'df.uxp.serverUrl'; const LS_TOKEN = 'df.uxp.apiToken'; API.state = { serverUrl: localStorage.getItem(LS_URL) || '', apiToken: localStorage.getItem(LS_TOKEN) || '', connected: false, }; API.save = function () { localStorage.setItem(LS_URL, API.state.serverUrl); localStorage.setItem(LS_TOKEN, API.state.apiToken); }; API.clear = function () { API.state.serverUrl = ''; API.state.apiToken = ''; API.state.connected = false; localStorage.removeItem(LS_URL); localStorage.removeItem(LS_TOKEN); }; function trimUrl(u) { return String(u || '').replace(/\/+$/, ''); } // Core request. `urlOrPath` may be a path (/api/...) or absolute URL. // Bearer header added only for same-server requests. API.request = async function (urlOrPath, opts) { opts = opts || {}; const isAbs = /^https?:\/\//i.test(urlOrPath); const base = trimUrl(API.state.serverUrl); const url = isAbs ? urlOrPath : (base + urlOrPath); const headers = Object.assign({}, opts.headers || {}); if (!isAbs || url.startsWith(base)) { if (API.state.apiToken) headers['Authorization'] = 'Bearer ' + API.state.apiToken; } return fetch(url, Object.assign({}, opts, { headers })); }; // Manual redirect follow: UXP strips Authorization on cross-origin // redirects — so we follow redirects manually and drop Bearer on hop // to a different host (e.g., proxy URL → presigned S3). API.requestFollow = async function (urlOrPath, opts) { opts = opts || {}; const isAbs = /^https?:\/\//i.test(urlOrPath); const base = trimUrl(API.state.serverUrl); let current = isAbs ? urlOrPath : (base + urlOrPath); for (let hop = 0; hop < 6; hop++) { const headers = Object.assign({}, opts.headers || {}); const sameOrigin = current.startsWith(base); if (sameOrigin && API.state.apiToken) { headers['Authorization'] = 'Bearer ' + API.state.apiToken; } else { delete headers['Authorization']; } const r = await fetch(current, Object.assign({}, opts, { headers, redirect: 'manual' })); if (r.status >= 200 && r.status < 300) return r; if (r.status >= 300 && r.status < 400) { const loc = r.headers.get('location'); if (!loc) throw new Error('Redirect with no Location header'); current = /^https?:\/\//i.test(loc) ? loc : new URL(loc, current).toString(); continue; } throw new Error('HTTP ' + r.status); } throw new Error('Too many redirects'); }; API.json = async function (path, opts) { const r = await API.request(path, opts); if (!r.ok) { const text = await r.text().catch(() => ''); throw new Error('HTTP ' + r.status + (text ? ' — ' + text.slice(0, 200) : '')); } return r.json(); }; // ── Auth ───────────────────────────────────────────────────────── API.connect = async function (serverUrl, apiToken) { API.state.serverUrl = trimUrl(serverUrl); API.state.apiToken = String(apiToken || '').trim(); if (!API.state.serverUrl || !API.state.apiToken) { throw new Error('Server URL and API token required'); } const me = await API.json('/api/v1/auth/me'); API.state.connected = true; API.save(); return me; }; API.disconnect = function () { API.clear(); }; // ── Assets ─────────────────────────────────────────────────────── API.listAssets = async function (query, projectId) { const p = new URLSearchParams({ limit: '60' }); if (query) p.set('q', query); if (projectId && projectId !== 'all') p.set('project_id', projectId); return API.json('/api/v1/assets?' + p.toString()); }; API.getAsset = async function (assetId) { return API.json('/api/v1/assets/' + assetId); }; // /stream → { url, type, source } API.getProxyUrl = async function (assetId) { const data = await API.json('/api/v1/assets/' + assetId + '/stream'); if (!data || !data.url) throw new Error('Asset has no proxy'); const u = data.url; const abs = /^https?:\/\//i.test(u) ? u : trimUrl(API.state.serverUrl) + u; return { url: abs, source: data.source || 'proxy' }; }; // /hires → { url, filename, ext, file_size, type } API.getHiresInfo = async function (assetId) { const data = await API.json('/api/v1/assets/' + assetId + '/hires'); if (!data || !data.url) throw new Error('Asset has no hi-res source'); return data; }; // /live-path → { win_path, posix_path, display_name } API.getLivePath = async function (assetId) { return API.json('/api/v1/assets/' + assetId + '/live-path'); }; // Batch trim: POST /api/v1/assets/batch-trim API.batchTrim = async function (clips) { return API.json('/api/v1/assets/batch-trim', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ clips }), }); }; // ── Projects ───────────────────────────────────────────────────── API.listProjects = async function () { const data = await API.json('/api/v1/projects'); return Array.isArray(data) ? data : []; }; // ── Sequences ──────────────────────────────────────────────────── API.listSequences = async function (projectId) { const p = new URLSearchParams(); if (projectId) p.set('project_id', projectId); const data = await API.json('/api/v1/sequences?' + p.toString()); return Array.isArray(data) ? data : []; }; API.createSequence = async function (projectId, name, frameRate, width, height) { return API.json('/api/v1/sequences', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ project_id: projectId, name, frame_rate: frameRate, width, height }), }); }; API.updateSequence = async function (seqId, fields) { return API.json('/api/v1/sequences/' + seqId, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(fields), }); }; API.pushClips = async function (seqId, clips) { const r = await API.request('/api/v1/sequences/' + seqId + '/clips', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(clips), }); if (!r.ok) throw new Error('Clip push HTTP ' + r.status); return r.json(); }; API.startConform = async function (seqId, opts) { return API.json('/api/v1/sequences/' + seqId + '/conform', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(opts), }); }; // ── Jobs ───────────────────────────────────────────────────────── API.getJob = async function (jobId) { return API.json('/api/v1/jobs/' + jobId); }; window.API = API; })();