const ANKI_PORT = 8765 ; import { AnkiConnectNote } from './interfaces/note-interface' ; export interface AnkiConnectRequest { action : string ; version : 6 ; params : any ; } export function invoke ( action , params = { } ) { return new Promise ( ( resolve , reject ) => { const xhr = new XMLHttpRequest ( ) ; xhr . addEventListener ( 'error' , ( ) => reject ( 'failed to issue request' ) ) ; xhr . addEventListener ( 'load' , ( ) => { try { const response = JSON . parse ( xhr . responseText ) ; if ( Object . getOwnPropertyNames ( response ) . length != 2 ) { throw 'response has an unexpected number of fields' ; } if ( ! response . hasOwnProperty ( 'error' ) ) { throw 'response is missing required error field' ; } if ( ! response . hasOwnProperty ( 'result' ) ) { throw 'response is missing required result field' ; } if ( response . error ) { throw response . error ; } resolve ( response . result ) ; } catch ( e ) { reject ( e ) ; } } ) ; xhr . open ( 'POST' , 'http://127.0.0.1:' + ANKI_PORT . toString ( ) ) ; xhr . send ( JSON . stringify ( { action , version : 6 , params } ) ) ; } ) ; } export function parse < T > ( response ) { if ( Object . getOwnPropertyNames ( response ) . length != 2 ) { throw 'response has an unexpected number of fields' ; } if ( ! ( response . hasOwnProperty ( 'error' ) ) ) { throw 'response is missing required error field' ; } if ( ! ( response . hasOwnProperty ( 'result' ) ) ) { throw 'response is missing required result field' ; } if ( response . error ) { throw response . error ; } return response . result ; } function request ( action , params = { } ) { return { action , version : 6 , params } ; } export function multi ( actions ) { return request ( 'multi' , { actions : actions } ) ; } export function addNote ( note ) { return request ( 'addNote' , { note : note } ) ; } export function deleteNotes ( note_ids ) { return request ( 'deleteNotes' , { notes : note_ids } ) ; } export function updateNoteFields ( id , fields ) { return request ( 'updateNoteFields' , { note : { id : id , fields : fields } } ) ; } export function notesInfo ( note_ids ) { return request ( 'notesInfo' , { notes : note_ids } ) ; } export function changeDeck ( card_ids , deck ) { return request ( 'changeDeck' , { cards : card_ids , deck : deck } ) ; } export function removeTags ( note_ids , tags ) { return request ( 'removeTags' , { notes : note_ids , tags : tags } ) ; } export function addTags ( note_ids , tags ) { return request ( 'addTags' , { notes : note_ids , tags : tags } ) ; } export function getTags ( ) { return request ( 'getTags' ) ; } export function storeMediaFile ( filename , data ) { return request ( 'storeMediaFile' , { filename : filename , data : data } ) ; } export function storeMediaFileByPath ( filename , path ) { return request ( 'storeMediaFile' , { filename : filename , path : path } ) ; }