|
| 1 | +/** |
| 2 | + * Skills API client for CLI |
| 3 | + */ |
| 4 | + |
| 5 | +import { DEFAULT_API_URL } from '../../constants.js'; |
| 6 | + |
| 7 | +export interface Skill { |
| 8 | + id: string; |
| 9 | + slug: string; |
| 10 | + name: string; |
| 11 | + description: string; |
| 12 | + type: 'template' | 'integration' | 'ui' | 'ai' | 'workflow'; |
| 13 | + authorName: string; |
| 14 | + latestVersion: string; |
| 15 | + downloads: number; |
| 16 | + stars: number; |
| 17 | + repository?: string; |
| 18 | + homepage?: string; |
| 19 | + license: string; |
| 20 | + keywords: string[]; |
| 21 | + createdAt: string; |
| 22 | + updatedAt: string; |
| 23 | +} |
| 24 | + |
| 25 | +export interface SkillVersion { |
| 26 | + id: string; |
| 27 | + version: string; |
| 28 | + changelog: string; |
| 29 | + fileSize: number; |
| 30 | + createdAt: string; |
| 31 | +} |
| 32 | + |
| 33 | +export interface Installation { |
| 34 | + id: string; |
| 35 | + skillId: string; |
| 36 | + version: string; |
| 37 | + installedAt: string; |
| 38 | + skill?: { |
| 39 | + slug: string; |
| 40 | + name: string; |
| 41 | + description: string; |
| 42 | + type: string; |
| 43 | + latestVersion: string; |
| 44 | + authorName: string; |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +async function fetchApi<T>( |
| 49 | + endpoint: string, |
| 50 | + options: RequestInit = {} |
| 51 | +): Promise<T> { |
| 52 | + const url = `${DEFAULT_API_URL}${endpoint}`; |
| 53 | + const response = await fetch(url, { |
| 54 | + ...options, |
| 55 | + headers: { |
| 56 | + 'Content-Type': 'application/json', |
| 57 | + ...options.headers, |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + if (!response.ok) { |
| 62 | + const error = await response.json().catch(() => ({ error: response.statusText })); |
| 63 | + throw new Error(error.error || `API error: ${response.status}`); |
| 64 | + } |
| 65 | + |
| 66 | + return response.json(); |
| 67 | +} |
| 68 | + |
| 69 | +export async function searchSkills(query: string, type?: string): Promise<{ skills: Skill[] }> { |
| 70 | + const params = new URLSearchParams(); |
| 71 | + if (type) params.set('type', type); |
| 72 | + const url = `/skills?${params.toString()}`; |
| 73 | + const result = await fetchApi<{ skills: Skill[] }>(url); |
| 74 | + |
| 75 | + // Client-side filter by query (API doesn't support search yet) |
| 76 | + const q = query.toLowerCase(); |
| 77 | + return { |
| 78 | + skills: result.skills.filter( |
| 79 | + (s) => |
| 80 | + s.name.toLowerCase().includes(q) || |
| 81 | + s.description.toLowerCase().includes(q) || |
| 82 | + s.keywords.some((k) => k.toLowerCase().includes(q)) |
| 83 | + ), |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +export async function listSkills(options: { |
| 88 | + type?: string; |
| 89 | + limit?: number; |
| 90 | + cursor?: string; |
| 91 | +}): Promise<{ skills: Skill[]; nextCursor: string | null }> { |
| 92 | + const params = new URLSearchParams(); |
| 93 | + if (options.type) params.set('type', options.type); |
| 94 | + if (options.limit) params.set('limit', options.limit.toString()); |
| 95 | + if (options.cursor) params.set('cursor', options.cursor); |
| 96 | + |
| 97 | + return fetchApi(`/skills?${params.toString()}`); |
| 98 | +} |
| 99 | + |
| 100 | +export async function getSkill(slug: string): Promise<Skill> { |
| 101 | + return fetchApi(`/skills/${slug}`); |
| 102 | +} |
| 103 | + |
| 104 | +export async function getSkillVersions( |
| 105 | + slug: string |
| 106 | +): Promise<{ skill: { id: string; slug: string; name: string }; versions: SkillVersion[] }> { |
| 107 | + return fetchApi(`/skills/${slug}/versions`); |
| 108 | +} |
| 109 | + |
| 110 | +export async function getDownloadUrl( |
| 111 | + slug: string, |
| 112 | + version?: string |
| 113 | +): Promise<{ |
| 114 | + skill: { id: string; slug: string; name: string }; |
| 115 | + version: { version: string; changelog: string; fileSize: number }; |
| 116 | + downloadUrl: string; |
| 117 | + expiresIn: number; |
| 118 | +}> { |
| 119 | + const params = version ? `?version=${version}` : ''; |
| 120 | + return fetchApi(`/skills/${slug}/download${params}`); |
| 121 | +} |
| 122 | + |
| 123 | +export async function listInstalledSkills( |
| 124 | + token: string |
| 125 | +): Promise<{ installations: Installation[] }> { |
| 126 | + return fetchApi('/skills/installed', { |
| 127 | + headers: { Authorization: `Bearer ${token}` }, |
| 128 | + }); |
| 129 | +} |
| 130 | + |
| 131 | +export async function recordInstallation( |
| 132 | + token: string, |
| 133 | + slug: string, |
| 134 | + version?: string |
| 135 | +): Promise<{ installation: { id: string; version: string } }> { |
| 136 | + return fetchApi(`/skills/${slug}/install`, { |
| 137 | + method: 'POST', |
| 138 | + headers: { Authorization: `Bearer ${token}` }, |
| 139 | + body: JSON.stringify({ version }), |
| 140 | + }); |
| 141 | +} |
0 commit comments