Homelabtoolkit v2

This commit is contained in:
2026-06-08 21:58:16 +12:00
parent 040fbacc70
commit 3c77066beb
75 changed files with 16945 additions and 374 deletions
+69
View File
@@ -0,0 +1,69 @@
import { readFileSync, existsSync } from 'fs';
import { resolve } from 'path';
const CONFIG_PATH = resolve('config.json');
export function loadEmbyConfig() {
if (!existsSync(CONFIG_PATH)) return {};
try {
return JSON.parse(readFileSync(CONFIG_PATH, 'utf-8'));
} catch {
return {};
}
}
export function normalizeEmbyGuid(value) {
return typeof value === 'string' ? value.replace(/-/g, '').trim().toLowerCase() : '';
}
export function buildEmbyUrl(pathname, params = {}) {
const { embyUrl, apiKey } = loadEmbyConfig();
if (!embyUrl || !apiKey) {
throw new Error('Emby URL and API key not configured');
}
const base = embyUrl.replace(/\/+$/, '');
const path = pathname.startsWith('/') ? pathname : `/${pathname}`;
const url = new URL(`${base}${path}`);
for (const [key, value] of Object.entries({
...params,
api_key: apiKey
})) {
if (value === undefined || value === null || value === '') continue;
url.searchParams.set(key, String(value));
}
return { url, apiKey };
}
export async function fetchEmby(pathname, options = {}) {
const {
params = {},
method = 'GET',
headers = {},
body
} = options;
const { url, apiKey } = buildEmbyUrl(pathname, params);
const response = await fetch(url, {
method,
headers: {
Accept: 'application/json',
'X-Emby-Token': apiKey,
...headers
},
body
});
if (!response.ok) {
const text = await response.text().catch(() => response.statusText);
throw new Error(text || `${response.status} ${response.statusText}`);
}
return response;
}
export async function fetchEmbyJson(pathname, options = {}) {
const response = await fetchEmby(pathname, options);
return response.json();
}