1import { requestUrl } from "obsidian";
2
3const API_BASE = "https://api.sublimated.com";
4
5export interface Version {
6 id: string;
7 number: number;
8 authorName: string;
9 authorType: string;
10 message: string | null;
11 createdAt: string;
12 commitHash: string | null;
13}
14
15export interface VersionsResponse {
16 versions: Version[];
17}
18
19export interface TestConnectionResult {
20 ok: boolean;
21 username?: string;
22 error?: string;
23}
24
25export class SublimatedApi {
26 constructor(private token: string) {}
27
28 private async request<T>(path: string): Promise<T> {
29 const response = await requestUrl({
30 url: `${API_BASE}${path}`,
31 headers: {
32 Authorization: `Bearer ${this.token}`,
33 Accept: "application/json",
34 },
35 });
36
37 if (response.status >= 400) {
38 throw new Error(
39 `API error ${response.status}: ${response.text}`
40 );
41 }
42
43 return response.json as T;
44 }
45
46 async getVersions(
47 spaceSlug: string,
48 itemId: string
49 ): Promise<Version[]> {
50 const data = await this.request<VersionsResponse>(
51 `/v1/spaces/${encodeURIComponent(spaceSlug)}/items/${encodeURIComponent(itemId)}/versions`
52 );
53 return data.versions;
54 }
55
56 async testConnection(): Promise<TestConnectionResult> {
57 try {
58 const data = await this.request<{ username: string }>(
59 "/v1/me"
60 );
61 return { ok: true, username: data.username };
62 } catch (e) {
63 return {
64 ok: false,
65 error: e instanceof Error ? e.message : "Unknown error",
66 };
67 }
68 }
69}