|
1 | 1 | import { getAccessToken } from '../auth/store.js'; |
2 | | - |
3 | | -const YAVY_BASE_URL = 'https://yavy.dev'; |
| 2 | +import { YAVY_BASE_URL } from '../config.js'; |
4 | 3 |
|
5 | 4 | export interface ApiProject { |
6 | | - id: number; |
7 | | - name: string; |
8 | | - slug: string; |
9 | | - description: string | null; |
10 | | - organization: { |
| 5 | + id: number; |
11 | 6 | name: string; |
12 | 7 | slug: string; |
13 | | - }; |
14 | | - pages_count: number; |
15 | | - last_indexed_at: string | null; |
16 | | - has_skill: boolean; |
| 8 | + description: string | null; |
| 9 | + organization: { |
| 10 | + name: string; |
| 11 | + slug: string; |
| 12 | + }; |
| 13 | + pages_count: number; |
| 14 | + last_indexed_at: string | null; |
| 15 | + has_skill: boolean; |
17 | 16 | } |
18 | 17 |
|
19 | 18 | export interface ApiSkill { |
20 | | - content: string; |
21 | | - format: string; |
22 | | - generated_at: string; |
23 | | - token_count: number; |
24 | | - project: { |
25 | | - name: string; |
26 | | - slug: string; |
27 | | - }; |
| 19 | + content: string; |
| 20 | + format: string; |
| 21 | + generated_at: string; |
| 22 | + token_count: number; |
| 23 | + project: { |
| 24 | + name: string; |
| 25 | + slug: string; |
| 26 | + }; |
28 | 27 | } |
29 | 28 |
|
30 | 29 | export class YavyApiClient { |
31 | | - private token: string; |
| 30 | + private token: string; |
32 | 31 |
|
33 | | - constructor(token: string) { |
34 | | - this.token = token; |
35 | | - } |
| 32 | + constructor(token: string) { |
| 33 | + this.token = token; |
| 34 | + } |
36 | 35 |
|
37 | | - static create(): YavyApiClient { |
38 | | - const token = getAccessToken(); |
39 | | - if (!token) { |
40 | | - throw new Error('Not authenticated. Run `yavy login` first.'); |
| 36 | + static async create(): Promise<YavyApiClient> { |
| 37 | + const token = await getAccessToken(); |
| 38 | + if (!token) { |
| 39 | + throw new Error('Not authenticated. Run `yavy login` first.'); |
| 40 | + } |
| 41 | + return new YavyApiClient(token); |
41 | 42 | } |
42 | | - return new YavyApiClient(token); |
43 | | - } |
44 | 43 |
|
45 | | - private async request<T>(method: string, path: string, body?: unknown): Promise<T> { |
46 | | - const url = `${YAVY_BASE_URL}/api/v1${path}`; |
47 | | - const headers: Record<string, string> = { |
48 | | - 'Authorization': `Bearer ${this.token}`, |
49 | | - 'Accept': 'application/json', |
50 | | - }; |
| 44 | + private async request<T>(method: string, path: string, body?: unknown): Promise<T> { |
| 45 | + const url = `${YAVY_BASE_URL}/api/v1${path}`; |
| 46 | + const headers: Record<string, string> = { |
| 47 | + Authorization: `Bearer ${this.token}`, |
| 48 | + Accept: 'application/json', |
| 49 | + }; |
51 | 50 |
|
52 | | - if (body) { |
53 | | - headers['Content-Type'] = 'application/json'; |
54 | | - } |
| 51 | + if (body) { |
| 52 | + headers['Content-Type'] = 'application/json'; |
| 53 | + } |
55 | 54 |
|
56 | | - const response = await fetch(url, { |
57 | | - method, |
58 | | - headers, |
59 | | - body: body ? JSON.stringify(body) : undefined, |
60 | | - }); |
| 55 | + const response = await fetch(url, { |
| 56 | + method, |
| 57 | + headers, |
| 58 | + body: body ? JSON.stringify(body) : undefined, |
| 59 | + }); |
61 | 60 |
|
62 | | - if (response.status === 401) { |
63 | | - throw new Error('Authentication expired. Run `yavy login` to re-authenticate.'); |
64 | | - } |
| 61 | + if (response.status === 401) { |
| 62 | + throw new Error('Authentication expired. Run `yavy login` to re-authenticate.'); |
| 63 | + } |
65 | 64 |
|
66 | | - if (!response.ok) { |
67 | | - const errorData = await response.json().catch(() => ({})) as { error?: string }; |
68 | | - throw new Error(errorData.error ?? `API request failed with status ${response.status}`); |
69 | | - } |
| 65 | + if (!response.ok) { |
| 66 | + const errorData = (await response.json().catch(() => ({}))) as { error?: string }; |
| 67 | + throw new Error(errorData.error ?? `API request failed with status ${response.status}`); |
| 68 | + } |
70 | 69 |
|
71 | | - return response.json() as Promise<T>; |
72 | | - } |
| 70 | + return response.json() as Promise<T>; |
| 71 | + } |
73 | 72 |
|
74 | | - async listProjects(): Promise<ApiProject[]> { |
75 | | - const result = await this.request<{ data: ApiProject[] }>('GET', '/projects'); |
76 | | - return result.data; |
77 | | - } |
| 73 | + async listProjects(): Promise<ApiProject[]> { |
| 74 | + const result = await this.request<{ data: ApiProject[] }>('GET', '/projects'); |
| 75 | + return result.data; |
| 76 | + } |
78 | 77 |
|
79 | | - async generateSkill(orgSlug: string, projectSlug: string, force = false): Promise<ApiSkill> { |
80 | | - return this.request<ApiSkill>('POST', `/${orgSlug}/${projectSlug}/skill/generate`, force ? { force: true } : undefined); |
81 | | - } |
| 78 | + async generateSkill(orgSlug: string, projectSlug: string, force = false): Promise<ApiSkill> { |
| 79 | + return this.request<ApiSkill>('POST', `/${orgSlug}/${projectSlug}/skill/generate`, force ? { force: true } : undefined); |
| 80 | + } |
82 | 81 |
|
83 | | - async getSkill(orgSlug: string, projectSlug: string): Promise<ApiSkill> { |
84 | | - return this.request<ApiSkill>('GET', `/${orgSlug}/${projectSlug}/skill`); |
85 | | - } |
| 82 | + async getSkill(orgSlug: string, projectSlug: string): Promise<ApiSkill> { |
| 83 | + return this.request<ApiSkill>('GET', `/${orgSlug}/${projectSlug}/skill`); |
| 84 | + } |
86 | 85 | } |
0 commit comments