1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
import { createKarakeepClient } from "@karakeep/sdk";
import type { Bookmark } from "./types";
import { config } from "./config";
export class KarakeepAPIClient {
private readonly client: ReturnType<typeof createKarakeepClient>;
constructor() {
this.client = createKarakeepClient({
baseUrl: `${config.KARAKEEP_SERVER_ADDR}/api/v1/`,
headers: {
"Content-Type": "application/json",
authorization: `Bearer ${config.KARAKEEP_API_KEY}`,
},
});
}
async fetchBookmarks(limit: number): Promise<Bookmark[]> {
const bookmarks: Bookmark[] = [];
let cursor: string | null = null;
let hasMore = true;
while (hasMore && bookmarks.length < limit) {
const params: {
limit: number;
includeContent: true;
archived?: boolean;
cursor?: string;
} = {
limit: Math.min(limit - bookmarks.length, 50),
includeContent: true,
archived: false,
};
if (cursor) {
params.cursor = cursor;
}
const { data, response, error } = await this.client.GET("/bookmarks", {
params: {
query: params,
},
});
if (error) {
throw new Error(`Failed to fetch bookmarks: ${String(error)}`);
}
if (!response.ok) {
throw new Error(`Failed to fetch bookmarks: ${response.status}`);
}
const batchBookmarks = (data?.bookmarks || [])
.filter((b) => b.content?.type === "link")
.map((b) => b as Bookmark);
bookmarks.push(...batchBookmarks);
cursor = data?.nextCursor || null;
hasMore = !!cursor;
}
return bookmarks.slice(0, limit);
}
}
|