itap-cli-demo
Version:
> A lightweight, AI-powered npm package finder — directly from your terminal.
31 lines (30 loc) • 951 B
JavaScript
import keytar from "keytar";
import fetch from "node-fetch";
const API_URL = "https://itap.onrender.com";
export async function getKey() {
return keytar.getPassword("itap", "api-key");
}
export async function setKey(k) {
await keytar.setPassword("itap", "api-key", k);
}
export async function register() {
const res = await fetch(`${API_URL}/register`, { method: "POST" });
const txt = await res.text();
if (!res.ok)
throw new Error(txt || `${res.status} ${res.statusText}`);
const { key } = JSON.parse(txt);
await setKey(key);
return key;
}
export async function rotate(oldKey) {
const res = await fetch(`${API_URL}/register/rotate`, {
method: "POST",
headers: { "x-api-key": oldKey },
});
const txt = await res.text();
if (!res.ok)
throw new Error(txt || `${res.status} ${res.statusText}`);
const { key } = JSON.parse(txt);
await setKey(key);
return key;
}