function getConfig() { const url = process.env.DOKPLOY_URL; const apiKey = process.env.DOKPLOY_API_KEY; if (!url || !apiKey) { throw new Error('DOKPLOY_URL / DOKPLOY_API_KEY が設定されていません'); } return { url, apiKey }; } async function trpcGet(endpoint, params) { const { url, apiKey } = getConfig(); const input = encodeURIComponent(JSON.stringify({ json: params })); const res = await fetch(`${url}/api/trpc/${endpoint}?input=${input}`, { headers: { 'x-api-key': apiKey }, }); if (!res.ok) { throw new Error(`Dokploy API error: ${res.status} ${endpoint}`); } const body = await res.json(); return body.result.data.json; } async function trpcPost(endpoint, data) { const { url, apiKey } = getConfig(); const res = await fetch(`${url}/api/trpc/${endpoint}`, { method: 'POST', headers: { 'x-api-key': apiKey, 'Content-Type': 'application/json' }, body: JSON.stringify({ json: data }), }); if (!res.ok) { throw new Error(`Dokploy API error: ${res.status} ${endpoint}`); } const body = await res.json(); return body.result?.data?.json; } async function listComposes(environmentId) { const env = await trpcGet('environment.one', { environmentId }); return env.compose; } async function getComposeEnv(composeId) { const full = await trpcGet('compose.one', { composeId }); return { env: full.env ?? null, appName: full.appName }; } async function getContainers(appName) { return trpcGet('docker.getContainersByAppNameMatch', { appName, appType: 'docker-compose' }); } async function readLogs(composeId, containerId, tail) { return trpcGet('compose.readLogs', { composeId, containerId, tail }); } async function startCompose(composeId) { return trpcPost('compose.start', { composeId }); } async function stopCompose(composeId) { return trpcPost('compose.stop', { composeId }); } async function restartContainer(containerId) { return trpcPost('docker.restartContainer', { containerId }); } module.exports = { trpcGet, trpcPost, listComposes, getComposeEnv, getContainers, readLogs, startCompose, stopCompose, restartContainer, };