import { request } from 'obsidian' ; import { DefinitionProvider , DictionaryWord , Meaning , PartOfSpeech , Synonym , SynonymProvider } from "src/integrations/types" ; abstract class Base { API_END_POINT = "https://api.dictionaryapi.dev/api/v2/entries/" ; public name = "Free Dictionary API" ; public url = "https://dictionaryapi.dev/" ; offline = false ; languageCodes = { "en_US" : "en_US" , "hi" : "hi" , "es" : "es" , "fr" : "fr" , "ja" : "ja" , "ru" : "ru" , "en_GB" : "en_GB" , "de" : "de" , "it" : "it" , "ko" : "ko" , "pt_BR" : "pt-BR" , "ar" : "ar" , "tr" : "tr" , } ; param query returns protected constructRequest ( query , lang ) { return this . API_END_POINT + lang + '/' + query ; } } export class FreeDictionaryDefinitionProvider extends Base implements DefinitionProvider { public supportedLanguages : string [ ] = [ "en_US" , "hi" , "es" , "fr" , "ja" , "ru" , "en_GB" , "de" , "it" , "ko" , "pt_BR" , "ar" , "tr" , ] ; param query param lang param _ returns async requestDefinitions ( query , lang , _ = true ) { let result ; try { const url = this . constructRequest ( encodeURIComponent ( query ) , this . languageCodes [ lang ] ) ; result = await request ( { url } ) ; } catch ( error ) { return Promise . reject ( error ) ; } const json = ( await JSON . parse ( result ) as DictionaryWord [ ] ) ; if ( ! json || json [ "title" ] ) { return Promise . reject ( json [ "title" ] ) ; } return json . first ( ) ; } } export class FreeDictionarySynonymProvider extends Base implements SynonymProvider { public supportedLanguages : string [ ] = [ "en_US" , ] ; param meaning param pos returns getDoesPosMatch ( meaning , pos ) { switch ( pos ) { case PartOfSpeech . Noun : return meaning . partOfSpeech . toLowerCase ( ) . contains ( 'noun' ) ; case PartOfSpeech . Verb : return meaning . partOfSpeech . toLowerCase ( ) . contains ( 'verb' ) ; case PartOfSpeech . Adjective : return meaning . partOfSpeech . toLowerCase ( ) . contains ( 'adjective' ) ; case PartOfSpeech . Adverb : return meaning . partOfSpeech . toLowerCase ( ) . contains ( 'adverb' ) ; } } param query param lang param pos returns async requestSynonyms ( query , lang , pos ? ) { let result ; try { result = await request ( { url : this . constructRequest ( query , this . languageCodes [ lang ] ) } ) ; } catch ( error ) { return Promise . reject ( error ) ; } if ( ! result ) { return Promise . reject ( "Word doesnt exist in this Dictionary" ) ; } const meanings = ( await JSON . parse ( result ) as DictionaryWord [ ] ) . first ( ) . meanings ; const synonyms = [ ] ; const nonPOSMatch = [ ] ; meanings . forEach ( meaning => { if ( Number . isNumber ( pos ) && ! this . getDoesPosMatch ( meaning , pos ) ) { meaning . definitions . forEach ( def => { if ( def . synonyms ) { def . synonyms . forEach ( synonym => { nonPOSMatch . push ( { word : synonym , } ) ; } ) ; } } ) ; return ; } meaning . definitions . forEach ( def => { if ( def . synonyms ) { def . synonyms . forEach ( synonym => { synonyms . push ( { word : synonym , } ) ; } ) ; } } ) ; } ) ; return synonyms . concat ( nonPOSMatch ) ; } }