UNPKG

@cloud-cli/cli

Version:

CLI for the Cloud CLI project

36 lines (35 loc) 1.08 kB
const baseURL = "__API_BASEURL__"; const headers = { Authorization: "" }; const cloud = {}; const fetchOptions = { method: "POST", headers, mode: "cors" }; export async function run(command, args = null) { const url = new URL(command, baseURL); const options = { ...fetchOptions, body: '{}', }; if (args) { options.body = JSON.stringify(args); } const response = await fetch(url, options); if (response.ok) { return await response.json(); } throw new Error(response.status + ": " + response.statusText); } export async function auth(key) { headers.Authorization = key; const request = await fetch(new URL(".help", baseURL), fetchOptions); if (!request.ok) { throw new Error("Unauthorized"); } const commands = (await request.json()); const list = Object.entries(commands); for (const [root, leaves] of list) { cloud[root] = {}; for (const leaf of leaves) { cloud[root][leaf] = run.bind(null, root + "." + leaf); } } } export default cloud;