import request , { makeRestRequest , setAPIUrl } from './request' ; import { pageOptions , searchOptions , geoOptions , listOptions , eventOptions , randomFormats , pdfOptions , citationFormat } from './optionTypes' ; import Page , { intro , images , html , content , categories , links , coordinates , langLinks , references , infobox , tables , summary , related , media , mobileHtml , pdf , citation } from './page' ; import { coordinatesResult , eventResult , geoSearchResult , imageResult , langLinksResult , languageResult , mobileSections , relatedResult , title , wikiMediaResult , wikiSearchResult , wikiSummary , notFound } from './resultTypes' ; import { categoriesError , contentError , coordinatesError , eventsError , geoSearchError , htmlError , imageError , infoboxError , introError , linksError , mediaError , pageError , relatedError , searchError , summaryError , wikiError , pdfError , citationError } from './errors' ; import { MSGS } from './messages' ; import { getCurrentDay , getCurrentMonth , setPageId , setPageIdOrTitleParam , setTitleForPage } from './utils' ; remarks const wiki = async ( title , pageOptions ? ) : Promise < Page > => { return wiki . page ( title , pageOptions ) ; } ; remarks param query param searchOptions - The number of results and if suggestion needed  searchOptions returns an array of  wikiSearchResult wiki . search = async ( query , searchOptions ? ) : Promise < wikiSearchResult > => { try { const searchParams = { 'list' : 'search' , 'srprop' : '' , 'srlimit' : searchOptions ?. limit || 10 , 'srsearch' : query } ; searchOptions ?. suggestion ? searchParams [ 'srinfo' ] = 'suggestion' : null ; const response = await request ( searchParams ) ; const result = { results : response . query . search , suggestion : response . query . searchinfo ? response . query . searchinfo . suggestion : null } ; return result ; } catch ( error ) { throw new searchError ( error ) ; } } ; remarks param title param pageOptions - Whether to redirect, autoSuggest or preload any fields  pageOptions returns wiki . page = async ( title , pageOptions ? ) : Promise < Page > => { try { if ( pageOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } let pageParams = { prop : 'info|pageprops' , inprop : 'url' , ppprop : 'disambiguation' , } ; pageParams = setPageIdOrTitleParam ( pageParams , title ) ; const response = await request ( pageParams ) ; let pageInfo = response . query . pages ; const pageId = setPageId ( pageParams , response ) ; pageInfo = pageInfo [ pageId ] ; if ( pageInfo . missing == '' ) { throw new pageError ( `${ MSGS . PAGE_NOT_EXIST }${ title }` ) ; } const page = new Page ( pageInfo ) ; if ( pageOptions ?. preload ) { if ( ! pageOptions ?. fields ) { pageOptions . fields = [ 'summary' , 'images' ] ; } for ( const field of pageOptions . fields ) { await page . runMethod ( field ) ; } } return page ; } catch ( error ) { throw new pageError ( error ) ; } } ; remarks param title param pageOptions returns wiki . intro = async ( title , pageOptions ? ) : Promise < string > => { try { if ( pageOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const result = await intro ( title , pageOptions ?. redirect ) ; return result ; } catch ( error ) { throw new introError ( error ) ; } } ; remarks param title param listOptions -  listOptions returns an array of imageResult  imageResult wiki . images = async ( title , listOptions ? ) : Promise < Array < imageResult > > => { try { if ( listOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const result = await images ( title , listOptions ) ; return result ; } catch ( error ) { throw new imageError ( error ) ; } } ; remarks param title param pageOptions returns The summary of the page as  wikiSummary wiki . summary = async ( title , pageOptions ? ) : Promise < wikiSummary > => { try { if ( pageOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const result = await summary ( title , pageOptions ?. redirect ) ; return result ; } catch ( error ) { throw new summaryError ( error ) ; } } ; remarks param title param pageOptions returns beta wiki . html = async ( title , pageOptions ? ) : Promise < string > => { try { if ( pageOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const result = await html ( title , pageOptions ?. redirect ) ; return result ; } catch ( error ) { throw new htmlError ( error ) ; } } ; remarks param title param pageOptions returns wiki . content = async ( title , pageOptions ? ) : Promise < string > => { try { if ( pageOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const response = await content ( title , pageOptions ?. redirect ) ; return response . result ; } catch ( error ) { throw new contentError ( error ) ; } } ; remarks param title param listOptions -  listOptions returns wiki . categories = async ( title , listOptions ? ) : Promise < Array < string > > => { try { if ( listOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const response = await categories ( title , listOptions ) ; return response ; } catch ( error ) { throw new categoriesError ( error ) ; } } ; remarks param title param pageOptions returns The related pages and summary as an array of  wikiSummary experimental wiki . related = async ( title , pageOptions ? ) : Promise < relatedResult > => { try { if ( pageOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const response = await related ( title , pageOptions ?. redirect ) ; return response ; } catch ( error ) { throw new relatedError ( error ) ; } } ; remarks param title param redirect returns The related pages and summary as an array of  wikiMediaResult experimental wiki . media = async ( title , pageOptions ? ) : Promise < wikiMediaResult > => { try { if ( pageOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const response = await media ( title , pageOptions ?. redirect ) ; return response ; } catch ( error ) { throw new mediaError ( error ) ; } } ; remarks param title param listOptions -  listOptions returns wiki . links = async ( title , listOptions ? ) : Promise < Array < string > > => { try { if ( listOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const response = await links ( title , listOptions ) ; return response ; } catch ( error ) { throw new linksError ( error ) ; } } ; remarks param title param listOptions -  listOptions returns wiki . references = async ( title , listOptions ? ) : Promise < Array < string > > => { try { if ( listOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const response = await references ( title , listOptions ) ; return response ; } catch ( error ) { throw new linksError ( error ) ; } } ; remarks param title param pageOptions returns The coordinates as  coordinatesResult wiki . coordinates = async ( title , pageOptions ? ) : Promise < coordinatesResult | null > => { try { if ( pageOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const response = await coordinates ( title , pageOptions ?. redirect ) ; return response ; } catch ( error ) { throw new coordinatesError ( error ) ; } } ; remarks param title param listOptions -  listOptions returns The links as an array of  langLinksResult wiki . langLinks = async ( title , listOptions ? ) : Promise < Array < langLinksResult > > => { try { if ( listOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const response = await langLinks ( title , listOptions ) ; return response ; } catch ( error ) { throw new linksError ( error ) ; } } ; remarks param title param pageOptions returns wiki . infobox = async ( title , pageOptions ? ) : Promise < any > => { try { if ( pageOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const response = await infobox ( title , pageOptions ?. redirect ) ; return response ; } catch ( error ) { throw new infoboxError ( error ) ; } } ; remarks param title param pageOptions returns wiki . tables = async ( title , pageOptions ? ) : Promise < Array < any > > => { try { if ( pageOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const response = await tables ( title , pageOptions ?. redirect ) ; return response ; } catch ( error ) { throw new infoboxError ( error ) ; } } ; remarks returns The languages an array of  languageResult wiki . languages = async ( ) : Promise < Array < languageResult > > => { try { const langParams = { 'meta' : 'siteinfo' , 'siprop' : 'languages' } ; const response = await request ( langParams ) ; const languages = [ ] ; for ( const lang of response . query . languages ) { languages . push ( { [ lang . code ] : lang [ '*' ] } ) ; } return languages ; } catch ( error ) { throw new wikiError ( error ) ; } } ; remarks returns wiki . setLang = ( language ) : string => { const apiUrl = setAPIUrl ( language ) ; return apiUrl ; } ; remarks param latitude param longitude param geoOptions - The number of results and the search radius  geoOptions returns The results as an array of  geoSearchResult wiki . geoSearch = async ( latitude , longitude , geoOptions ? ) : Promise < Array < geoSearchResult > > => { try { const geoSearchParams = { 'list' : 'geosearch' , 'gsradius' : geoOptions ?. radius || 1000 , 'gscoord' : `${ latitude }|${ longitude }` , 'gslimit' : geoOptions ?. limit || 10 , 'gsprop' : 'type' } ; const results = await request ( geoSearchParams ) ; const searchPages = results . query . geosearch ; return searchPages ; } catch ( error ) { throw new geoSearchError ( error ) ; } } ; remarks param query returns wiki . suggest = async ( query ) : Promise < string | null > => { try { const suggestParams = { 'list' : 'search' , 'srinfo' : 'suggestion' , 'srprop' : '' , 'srsearch' : query } ; const result = await request ( suggestParams ) ; return result . query ?. searchinfo ?. suggestion ? result . query ?. searchinfo ?. suggestion : null ; } catch ( error ) { throw new searchError ( error ) ; } } ; remarks param eventOptions - the event types, and the month and day  eventOptions returns Returns the results as array of  eventResult wiki . onThisDay = async ( eventOptions = { } ) : Promise < eventResult > => { try { const type = eventOptions . type || 'all' ; const mm = ( eventOptions . month || getCurrentMonth ( ) ) . toString ( ) . padStart ( 2 , "0" ) ; const dd = ( eventOptions . day || getCurrentDay ( ) ) . toString ( ) . padStart ( 2 , "0" ) ; const path = `feed/onthisday/${ type }/${ mm }/${ dd }` ; const result = await makeRestRequest ( path , true ) ; return result ; } catch ( error ) { throw new eventsError ( error ) ; } } ; param format returns wiki . random = async ( format ? ) : Promise < wikiSummary | title | relatedResult | mobileSections | string > => { try { if ( ! format ) { format = 'summary' ; } const path = `page/random/${ format }` ; const result = await makeRestRequest ( path ) ; return result ; } catch ( error ) { throw new wikiError ( error ) ; } } ; param title param pageOptions returns wiki . mobileHtml = async ( title , pageOptions ? ) : Promise < notFound | string > => { try { if ( pageOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const result = await mobileHtml ( title , pageOptions ?. redirect ) ; return result ; } catch ( error ) { throw new htmlError ( error ) ; } } ; param title param pdfOptions -  pdfOptions returns wiki . pdf = async ( title , pdfOptions ? ) : Promise < any > => { try { if ( pdfOptions ?. autoSuggest ) { title = await setTitleForPage ( title ) ; } const result = await pdf ( title , pdfOptions ) ; return result ; } catch ( error ) { throw new pdfError ( error ) ; } } ; param format param query param language returns wiki . citation = async ( query , format ? , language ? ) : Promise < any > => { try { const result = await citation ( query , format , language ) ; return result ; } catch ( error ) { throw new citationError ( error ) ; } } ; export default wiki ; module . exports = wiki ; module . exports . default = wiki ; export * from './errors' ; export * from './resultTypes' ; export * from './optionTypes' ; export * from './page' ;