import { getRegistryUrl, requireToken } from "./config.js"; export interface PublishResponse { message: string; skillId: string; versionId: string; url: string; pinnedUrl: string; } export interface VersionEntry { version: string; contentHash: string; publishedAt: string; } export interface MarketplaceListing { id: string; fullName: string; namespace: string; skillName: string; description: string; author: string; price: number; latestVersion: string; tags: string[]; modelTier: string; } export async function publishSkill(content: string): Promise { const token = requireToken(); const url = `${getRegistryUrl()}/api/v1/skills/publish`; const response = await fetch(url, { method: "POST", headers: { "Content-Type": "text/markdown", Authorization: `Bearer ${token}`, }, body: content, }); const body = await response.json() as Record; if (!response.ok) { const errMsg = typeof body.error === "string" ? body.error : `HTTP ${response.status}`; const details = body.details ? "\n" + JSON.stringify(body.details, null, 2) : ""; throw new Error(`Publish failed: ${errMsg}${details}`); } return body as unknown as PublishResponse; } export async function fetchVersions(skillName: string): Promise { const token = getToken(); const url = `${getRegistryUrl()}/api/v1/skills/${encodeURIComponent(skillName)}/versions`; const headers: Record = {}; if (token) headers["Authorization"] = `Bearer ${token}`; const response = await fetch(url, { headers }); const body = await response.json() as Record; if (!response.ok) { const errMsg = typeof body.error === "string" ? body.error : `HTTP ${response.status}`; throw new Error(`Failed to fetch versions: ${errMsg}`); } return (body.versions ?? body) as VersionEntry[]; } export async function fetchPreview(skillName: string): Promise<{ frontmatter: Record; preview: string; }> { const url = `${getRegistryUrl()}/api/v1/skills/${encodeURIComponent(skillName)}/preview`; const response = await fetch(url); const body = await response.json() as Record; if (!response.ok) { const errMsg = typeof body.error === "string" ? body.error : `HTTP ${response.status}`; throw new Error(`Failed to fetch preview: ${errMsg}`); } return body as { frontmatter: Record; preview: string }; } // Helper — get token without throwing (for public endpoints) function getToken(): string | undefined { try { return requireToken(); } catch { return undefined; } }