generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
68 lines (60 loc) · 1.6 KB
/
api.ts
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
66
67
68
interface ReadwiseReview {
review_id: number;
review_url: string;
review_completed: boolean;
highlights: ReviewHighlight[];
}
interface ReviewHighlight {
id: number;
text: string;
title: string;
author: string;
}
export interface Highlight extends ReviewHighlight {
bookId: number;
}
export interface HighlightModalEntry {
highlightId: string;
text: string;
title: string;
// author: string;
}
function getAuthHeaders(token: string) {
return { AUTHORIZATION: `Token ${token}` };
}
async function getDailyReview(token: string): Promise<ReadwiseReview> {
const response = await fetch('https://readwise.io/api/v2/review/', {
method: 'GET',
headers: getAuthHeaders(token),
});
const review: ReadwiseReview = await response.json();
return review;
}
async function getHighlightBookId(
highlight: ReviewHighlight,
token: string,
): Promise<{ bookId: number }> {
const response = await fetch(
`https://readwise.io/api/v2/highlights/${highlight.id}`,
{
method: 'GET',
headers: getAuthHeaders(token),
},
);
const highlightDetail = await response.json();
return { bookId: highlightDetail.book_id };
}
/**
* Fetch your Readwise daily review, then (for each highlight) augment the
* review with the book ID.
*/
export async function getHighlights(token: string): Promise<Highlight[]> {
const review = await getDailyReview(token);
const highlightDetails = await Promise.all(
review.highlights.map(async (highlight) => ({
...highlight,
bookId: (await getHighlightBookId(highlight, token)).bookId,
})),
);
return highlightDetails;
}