next.config.tsTypeScript57 lines1.5 KB
1
import type { Repository, FileEntry } from '@/lib/types';
2
 
3
/**
4
 * API client for the Drok sovereign code platform.
5
 * Communicates with the Rust backend via REST endpoints.
6
 * All requests are authenticated through auth.lemay.app.
7
 */
8
export class DrokClient {
9
  private readonly baseUrl: string;
10
  private readonly token: string | null;
11
 
12
  constructor(baseUrl: string, token?: string) {
13
    this.baseUrl = baseUrl;
14
    this.token = token ?? null;
15
  }
16
 
17
  private async request<T>(path: string, init?: RequestInit): Promise<T> {
18
    const headers: Record<string, string> = {
19
      'Content-Type': 'application/json',
20
    };
21
 
22
    if (this.token) {
23
      headers['Authorization'] = `Bearer ${this.token}`;
24
    }
25
 
26
    const response = await fetch(`${this.baseUrl}${path}`, {
27
      ...init,
28
      headers: { ...headers, ...init?.headers },
29
    });
30
 
31
    if (!response.ok) {
32
      throw new Error(`API error: ${response.status}`);
33
    }
34
 
35
    return response.json();
36
  }
37
 
38
  async getRepository(owner: string, repo: string): Promise<Repository> {
39
    return this.request<Repository>(`/api/v1/repos/${owner}/${repo}`);
40
  }
41
 
42
  async getFileTree(
43
    owner: string,
44
    repo: string,
45
    branch: string,
46
    path?: string,
47
  ): Promise<FileEntry[]> {
48
    const encodedPath = path ? `/${encodeURIComponent(path)}` : '';
49
    return this.request<FileEntry[]>(
50
      `/api/v1/repos/${owner}/${repo}/tree/${encodeURIComponent(branch)}${encodedPath}`,
51
    );
52
  }
53
}
54
 
55
export const api = new DrokClient(
56
  process.env.NEXT_PUBLIC_API_URL ?? 'https://api.drok.us',
57
);