@micrio/tiler-base
Version:
The base Micrio client-side tiler package used by the Micrio CLI and GUI tools
26 lines (25 loc) • 1.17 kB
JavaScript
const urlAccountBase = 'https://account.micr.io';
const to = (fn, ms = 1000) => new Promise(ok => setTimeout(async () => { await fn?.(); ok(); }, ms));
/** Async function giving a login URL and waiting until login process has been completed */
export const login = (sendLoginUrl) => new Promise(async (ok, err) => {
const id = crypto.randomUUID();
async function check() {
return new Promise(async (ok, err) => {
do {
const resp = await fetch(`${urlAccountBase}/api/cli/${id}/status`).then(r => r?.ok && r.status == 200 ? r.json() : { status: 'error' });
if (resp.status == 'ok')
return ok(resp);
else if (resp.status == 'error')
return err(resp);
else
await to(undefined, 3000);
} while (true);
});
}
if (await fetch(`${urlAccountBase}/api/cli/${id}/create`).then(r => r.text()) == 'OK') {
sendLoginUrl(`${urlAccountBase}/cli-login/${id}`);
check().then((r) => ok(r.token), err);
}
else
throw new Error('Something went wrong. Please try again later.');
});