32 lines
1.4 KiB
JavaScript
32 lines
1.4 KiB
JavaScript
// Thin client for stock Kutt's REST API + release-version compare. fetch injected
|
|
// for tests; defaults to global fetch (Node 22). No Kutt source coupling.
|
|
const norm = v => String(v || '').replace(/^v/, '');
|
|
|
|
export function compareVersions(running, latest) {
|
|
return { running, latest, updateAvailable: norm(running) !== '' && norm(latest) !== '' && norm(running) !== norm(latest) };
|
|
}
|
|
|
|
export async function fetchLatestKuttRelease({ fetch = globalThis.fetch } = {}) {
|
|
const res = await fetch('https://api.github.com/repos/thedevs-network/kutt/releases/latest',
|
|
{ headers: { 'Accept': 'application/vnd.github+json', 'User-Agent': 'void' } });
|
|
if (!res.ok) throw new Error(`github ${res.status}`);
|
|
const j = await res.json();
|
|
return { latest: j.tag_name, url: j.html_url };
|
|
}
|
|
|
|
export async function createLink(body, { base, key, fetch = globalThis.fetch }) {
|
|
const res = await fetch(`${base}/api/v2/links`, {
|
|
method: 'POST',
|
|
headers: { 'X-API-KEY': key, 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(body)
|
|
});
|
|
if (!res.ok) throw new Error(`kutt ${res.status}`);
|
|
return res.json();
|
|
}
|
|
|
|
export async function recentLinks({ base, key, fetch = globalThis.fetch, limit = 5 }) {
|
|
const res = await fetch(`${base}/api/v2/links?limit=${limit}`, { headers: { 'X-API-KEY': key } });
|
|
if (!res.ok) throw new Error(`kutt ${res.status}`);
|
|
return res.json();
|
|
}
|