31 lines
1.0 KiB
TypeScript
31 lines
1.0 KiB
TypeScript
import type { CoolifyServiceWidget } from "@/lib/config/schema";
|
|||
|
|
import type { ServiceStat } from "@/lib/types/service-result";
|
||
|
|
import { serviceBaseUrl } from "./base-url";
|
||
|
|
|
||
|
|
const DETAIL_LIMIT = 5;
|
||
|
|
|
||
|
|
interface CoolifyProject {
|
||
|
|
name: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function collectCoolify(
|
||
|
|
widget: CoolifyServiceWidget
|
||
|
|
): Promise<{ stats: ServiceStat[]; detail?: string[] }> {
|
||
|
|
const base = serviceBaseUrl(widget);
|
||
|
|
const response = await fetch(`${base}/api/v1/projects`, {
|
||
|
|
headers: { Authorization: `Bearer ${widget.apiToken}` },
|
||
|
|
signal: AbortSignal.timeout(5_000),
|
||
|
|
});
|
||
|
|
if (!response.ok) {
|
||
|
|
throw new Error(`Coolify API returned ${response.status}`);
|
||
|
|
}
|
||
|
|
const projects = (await response.json()) as CoolifyProject[];
|
||
|
|
const names = projects.slice(0, DETAIL_LIMIT).map((p) => p.name);
|
||
|
|
if (projects.length > DETAIL_LIMIT) names.push(`+${projects.length - DETAIL_LIMIT} more`);
|
||
|
|
|
||
|
|
return {
|
||
|
|
stats: [{ label: "Projects", value: String(projects.length) }],
|
||
|
|
detail: names.length > 0 ? names : undefined,
|
||
|
|
};
|
||
|
|
}
|