import { request } from 'obsidian' ; import type { Synonym , SynonymProvider } from "src/integrations/types" ; export class OpenThesaurusSynonymAPI implements SynonymProvider { API_END_POINT = "https://www.openthesaurus.de/synonyme/search?q=" ; public name = "OpenThesaurus" ; public url = "https://www.openthesaurus.de/" ; public supportedLanguages : string [ ] = [ "de" ] ; offline = false ; param query returns constructRequest ( query ) { return this . API_END_POINT + query + "&format=application/json" ; } async requestSynonyms ( query ) { let result ; try { result = await request ( { url : this . constructRequest ( query ) } ) ; } catch ( error ) { return Promise . reject ( error ) ; } if ( ! result ) { return Promise . reject ( "Word doesnt exist in this Dictionary" ) ; } const response = await JSON . parse ( result ) ; if ( response . synsets . length <= 0 ) { return Promise . reject ( "No Synonym found" ) ; } const synonymList = response . synsets [ 0 ] . terms ; const synonyms = [ ] ; synonymList . forEach ( ( synonym ) => { const word = synonym [ "term" ] ; if ( query != word ) { synonyms . push ( { word : word } ) ; } } ) ; return synonyms ; } }