/
client.ts
158 lines (142 loc) · 4.48 KB
/
client.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import {AnkiConfig} from "../plugins/anki/anki_config";
import {ReviewedCard} from "./model";
import {log} from "../config";
interface AnkiConnectRequest {
action: string;
params?: object;
}
export interface NoteInfo {
noteId: number;
cards: number[],
fields: {
noteId: number;
},
tags: string[];
}
const ANKI_CONNECT_VERSION = 6;
export class AnkiClient {
config: AnkiConfig;
constructor(config: AnkiConfig) {
this.config = config;
}
private async ankiRequest(config: AnkiConfig, request: AnkiConnectRequest, silent: boolean = false) {
const stringifiedBody = JSON.stringify({
...request,
version: ANKI_CONNECT_VERSION,
});
if (!silent) {
log.info(`Doing request ${stringifiedBody}`)
}
const res = await fetch(config.url, {
method: 'POST',
body: stringifiedBody,
});
return await res.json();
}
async ankiIsUp() : Promise<boolean>{
try {
const res = await this.ankiRequest(this.config, {
action: 'version',
}, true).then(data => data as ({ result: number }))
return res.result == 6;
} catch (e) {
return false;
}
}
async findNotes(deckName: string): Promise<{ result: number[] }> {
return this.ankiRequest(this.config, {
action: 'findNotes',
params: {
query: `"deck:${deckName}"`,
}
}).then(data => data as ({ result: number[] }))
}
async getNotesInfo(noteIds: number[]): Promise<any> {
return this.ankiRequest(this.config, {
action: 'notesInfo',
params: {
notes: noteIds
}
})
}
async updateNoteFields(note: {
id: number;
fields: Record<string, string>
}): Promise<any> {
return this.ankiRequest(this.config, {
action: 'updateNoteFields',
params: {
note: note
}
})
}
async getDeckNames(): Promise<{ result: string[] }> {
return this.ankiRequest(this.config, {
action: 'deckNames',
}).then(data => data as ({ result: string[] }))
}
async createDeck(deckName: string): Promise<void> {
await this.ankiRequest(this.config, {
action: 'createDeck',
params: {
deck: deckName
}
})
}
async addNotes(notes: {
deckName: string,
modelName: string;
fields: Record<string, string>,
tags: string[]
}[]): Promise<void> {
await this.ankiRequest(this.config, {
action: 'addNotes',
params: {
notes: notes
}
})
}
// (reviewTime, cardID, usn, buttonPressed, newInterval, previousInterval, newFactor, reviewDuration, reviewType)
async getDeckReviews(deckName: string, startId?: number): Promise<{ result: ReviewedCard[] }> {
return this.ankiRequest(this.config, {
action: 'cardReviews',
params: {
deck: deckName,
startID: startId ?? 0,
}
}).then(data => data as ({ result: number[][] }))
.then(res => {
return {
result: res.result.map(tuple => {
return {
reviewId: tuple[0],
cardId: tuple[1],
usn: tuple[2],
buttonPressed: tuple[3],
previousInterval: tuple[4],
newInterval: tuple[5],
newFactor: tuple[6],
reviewDurationMs: tuple[7],
reviewType: tuple[8],
} as ReviewedCard
})
}
})
}
async cardsToNotes(cards: number[]): Promise<{ result: number[] }> {
return this.ankiRequest(this.config, {
action: 'cardsToNotes',
params: {
cards: cards,
}
}).then(data => data as ({ result: number[] }))
}
async notesInfo(notes: number[]): Promise<{ result: NoteInfo[] }> {
return this.ankiRequest(this.config, {
action: 'notesInfo',
params: {
notes: notes,
}
}).then(data => data as ({ result: NoteInfo[] }))
}
}