UNPKG

@micrio/tiler-base

Version:

The base Micrio client-side tiler package used by the Micrio CLI and GUI tools

46 lines (45 loc) 1.6 kB
import https from 'https'; const urlDashBase = 'https://dash.micr.io'; /** * Talk with the Micrio dashboard CLI API (https://dash.micr.io/api/cli/*) * @see https://github.com:Q42/Micrio/server/dash.micr.io for the server code (Q42 only -- might open source one day) */ export const api = (account, agent, path, data = {}) => new Promise((ok, err) => { if (!account) return err(new Error('Not logged in')); const url = new URL(urlDashBase + '/api/cli' + path); const blob = JSON.stringify(data); const req = https.request({ host: url.host, path: url.pathname + url.search, method: 'POST', agent: agent, headers: { 'Cookie': `.AspNetCore.Identity.Application=${account.base64};`, 'Content-Type': 'application/json', 'Content-Length': blob.length } }, res => { const body = []; res.on('data', chunk => { body.push(chunk); }) .on('end', () => { const resp = Buffer.concat(body).toString(); const b = resp?.startsWith('{') ? JSON.parse(resp) : {}; if (res.statusCode != 200) { const message = res.statusCode == 403 ? 'Access denied' : 'Unknown error'; err(new Error(`${res.statusCode} ${res.statusMessage}: ${b?.error ?? url.pathname + ': ' + message}`)); } else ok(b); req.destroy(); }); }); req.on('error', (e) => { err(e); req.destroy(); }); req.write(blob); req.end(); });