11import { requestUrl , RequestUrlParam , RequestUrlResponse } from "obsidian" ;
2- import { AnkiActionResponse , CreateModelInput , Note , NoteFields } from "./types" ;
2+ import { AnkiActionResponse , AnkiMultiAction , CreateModelInput , Note , NoteFields } from "./types" ;
33
44const DEFAULT_ANKI_CONNECT_URL = "http://localhost:8765" ;
55const DEFAULT_ANKI_CONNECT_VERSION = 6 ;
6+ const REQUEST_LOG_PREFIX = "[anki-link][network]" ;
67
78type AnkiAction =
89 | "createDeck"
910 | "createModel"
1011 | "addNote"
12+ | "addNotes"
1113 | "addTags"
1214 | "changeDeck"
1315 | "deckNames"
@@ -17,7 +19,8 @@ type AnkiAction =
1719 | "findNotes"
1820 | "deleteNotes"
1921 | "notesInfo"
20- | "updateNoteFields" ;
22+ | "updateNoteFields"
23+ | "multi" ;
2124
2225interface AnkiConnectClientOptions {
2326 url ?: string ;
@@ -42,8 +45,24 @@ export class AnkiConnectClient {
4245 }
4346
4447 private async send < T > ( action : AnkiAction , params ?: unknown ) : Promise < AnkiActionResponse < T > > {
45- const response : RequestUrlResponse = await requestUrl ( this . buildRequest ( action , params ) ) ;
46- return response . json as AnkiActionResponse < T > ;
48+ const request = this . buildRequest ( action , params ) ;
49+ const startedAt = Date . now ( ) ;
50+ console . debug ( `${ REQUEST_LOG_PREFIX } -> ${ request . method } ${ request . url } action=${ action } ` ) ;
51+ try {
52+ const response : RequestUrlResponse = await requestUrl ( request ) ;
53+ const elapsedMs = Date . now ( ) - startedAt ;
54+ const result = response . json as AnkiActionResponse < T > ;
55+ const status = result . error ? "error" : "ok" ;
56+ console . debug ( `${ REQUEST_LOG_PREFIX } <- action=${ action } status=${ status } elapsedMs=${ elapsedMs } ` ) ;
57+ if ( result . error ) {
58+ console . debug ( `${ REQUEST_LOG_PREFIX } !! action=${ action } error="${ result . error } "` ) ;
59+ }
60+ return result ;
61+ } catch ( error ) {
62+ const elapsedMs = Date . now ( ) - startedAt ;
63+ console . debug ( `${ REQUEST_LOG_PREFIX } xx action=${ action } threw elapsedMs=${ elapsedMs } ` ) ;
64+ throw error ;
65+ }
4766 }
4867
4968 async deckNames ( ) : Promise < AnkiActionResponse < string [ ] > > {
@@ -81,6 +100,10 @@ export class AnkiConnectClient {
81100 return this . send < number > ( "addNote" , { note } ) ;
82101 }
83102
103+ async addNotes ( notes : Note [ ] ) : Promise < AnkiActionResponse < ( number | null ) [ ] > > {
104+ return this . send < ( number | null ) [ ] > ( "addNotes" , { notes } ) ;
105+ }
106+
84107 async addTags ( notes : number [ ] , tags : string ) : Promise < AnkiActionResponse < null > > {
85108 return this . send < null > ( "addTags" , { notes, tags } ) ;
86109 }
@@ -104,6 +127,10 @@ export class AnkiConnectClient {
104127 async changeDeck ( cards : number [ ] , deck : string ) : Promise < AnkiActionResponse < null > > {
105128 return this . send < null > ( "changeDeck" , { cards, deck } ) ;
106129 }
130+
131+ async multi ( actions : AnkiMultiAction [ ] ) : Promise < AnkiActionResponse < AnkiActionResponse < unknown > [ ] > > {
132+ return this . send < AnkiActionResponse < unknown > [ ] > ( "multi" , { actions } ) ;
133+ }
107134}
108135
109136export const defaultAnkiConnectClient = new AnkiConnectClient ( ) ;
0 commit comments