// HTTP-Client fuer den Preview-Runner (Bearer-Token, JSON) import { config } from '../config.js' const TIMEOUT_MS = 15_000 async function call(method, path, body) { const { url, token } = config.previewRunner let response try { response = await fetch(`${String(url).replace(/\/$/, '')}${path}`, { method, headers: { Authorization: `Bearer ${token}`, ...(body !== undefined ? { 'Content-Type': 'application/json' } : {}), }, body: body !== undefined ? JSON.stringify(body) : undefined, signal: AbortSignal.timeout(TIMEOUT_MS), }) } catch (err) { throw new Error(`Preview-Runner nicht erreichbar: ${err.message}`) } const text = await response.text() let data = null try { data = text ? JSON.parse(text) : null } catch { data = { error: text } } if (!response.ok) { const error = new Error(data?.error || `Preview-Runner ${response.status}`) error.status = response.status throw error } return data } export const submitBuild = (job) => call('POST', '/jobs/build', job) export const getRun = (runId) => call('GET', `/jobs/${encodeURIComponent(runId)}`) // Dev-Auftrag beim Runner hinterlegen (startet nicht, R12) export const startDev = (sub, job) => call('PUT', `/dev/${encodeURIComponent(sub)}`, job) // Dev-Server jetzt starten (mit gespeichertem Auftrag) export const startDevNow = (sub, job) => call('POST', `/dev/${encodeURIComponent(sub)}/start`, job) export const stopDev = (sub) => call('POST', `/dev/${encodeURIComponent(sub)}/stop`, {}) export const status = () => call('GET', '/status')