Apr 11, 2026 1.5 KB typescript of v4
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 headers: Record<string, string> = {
30 Accept: "application/json",
31 };
32 if (this.token) {
33 headers.Authorization = `Bearer ${this.token}`;
34 }
35 const response = await requestUrl({
36 url: `${API_BASE}${path}`,
37 headers,
38 });
39
40 if (response.status >= 400) {
41 throw new Error(
42 `API error ${response.status}: ${response.text}`
43 );
44 }
45
46 return response.json as T;
47 }
48
49 async getVersions(
50 spaceSlug: string,
51 itemId: string
52 ): Promise<Version[]> {
53 const data = await this.request<VersionsResponse>(
54 `/v1/spaces/${encodeURIComponent(spaceSlug)}/items/${encodeURIComponent(itemId)}/versions`
55 );
56 return data.versions;
57 }
58
59 async testConnection(): Promise<TestConnectionResult> {
60 try {
61 const data = await this.request<{ username: string }>(
62 "/v1/me"
63 );
64 return { ok: true, username: data.username };
65 } catch (e) {
66 return {
67 ok: false,
68 error: e instanceof Error ? e.message : "Unknown error",
69 };
70 }
71 }
72}