import { categoriesError , contentError , coordinatesError , htmlError , imageError , wikiError , pdfError , infoboxError , introError , linksError , mediaError , preloadError , relatedError , summaryError , citationError } from './errors' ; import request , { makeRestRequest , returnRestUrl } from './request' ; import { coordinatesResult , imageResult , langLinksResult , notFound , pageResult , relatedResult , wikiMediaResult , wikiSummary } from './resultTypes' ; import { setPageId , setPageIdOrTitleParam } from './utils' ; import { citationFormat , listOptions , pageOptions , pdfOptions } from './optionTypes' ; import { MSGS } from './messages' ; const infoboxParser = require ( 'infobox-parser' ) ; export class Page { pageid ! : number ; ns ! : number ; title ! : string ; contentmodel ! : string ; pagelanguage ! : string ; pagelanguagehtmlcode ! : string ; pagelanguagedir ! : string ; touched ! : string ; lastrevid ! : number ; length ! : number ; fullurl ! : string ; editurl ! : string ; canonicalurl ! : string ; revid ! : number ; parentid ! : number ; _summary ! : wikiSummary ; _images ! : Array < imageResult > ; _content ! : string ; _html ! : string ; _categories ! : Array < string > ; _references ! : Array < string > ; _links ! : Array < string > ; _coordinates ! : coordinatesResult ; _langLinks ! : Array < langLinksResult > ; _infobox ! : any ; _tables ! : Array < any > ; _intro ! : string ; _related ! : relatedResult ; _media ! : wikiMediaResult ; _mobileHtml ! : string | notFound ; constructor ( response ) { this . pageid = response . pageid ; this . ns = response . ns ; this . title = response . title ; this . contentmodel = response . contentmodel ; this . pagelanguage = response . pagelanguage ; this . pagelanguagedir = response . pagelanguagedir ; this . touched = response . touched ; this . lastrevid = response . lastrevid ; this . length = response . length ; this . fullurl = response . fullurl ; this . editurl = response . editurl ; this . canonicalurl = response . canonicalurl ; } remarks This method is part of the  Page .
     *
     *  param title param redirect returns public intro = async ( pageOptions ? ) : Promise < string > => { try { if ( ! this . _intro ) { const response = await intro ( this . pageid . toString ( ) , pageOptions ?. redirect ) ; this . _intro = response ; } return this . _intro ; } catch ( error ) { throw new introError ( error ) ; } } ; remarks This method is part of the  Page .
     *
     *  param title param listOptions -  listOptions returns an array of imageResult  imageResult public images = async ( listOptions ? ) : Promise < Array < imageResult > > => { try { if ( ! this . _images ) { const result = await images ( this . pageid . toString ( ) , listOptions ) ; this . _images = result ; } return this . _images ; } catch ( error ) { throw new imageError ( error ) ; } } ; remarks This method is part of the  Page .
     *
     *  param title param redirect returns The summary of the page as  wikiSummary public summary = async ( pageOptions ? ) : Promise < wikiSummary > => { try { if ( ! this . _summary ) { const result = await summary ( this . title , pageOptions ?. redirect ) ; this . _summary = result ; } return this . _summary ; } catch ( error ) { throw new summaryError ( error ) ; } } ; remarks This method is part of the  Page .
     *
     *  param title param redirect returns beta public html = async ( pageOptions ? ) : Promise < string > => { try { if ( ! this . _html ) { const result = await html ( this . pageid . toString ( ) , pageOptions ?. redirect ) ; this . _html = result ; } return this . _html ; } catch ( error ) { throw new htmlError ( error ) ; } } ; remarks This method is part of the  Page .
     *
     *  param title param redirect returns public content = async ( pageOptions ? ) : Promise < string > => { try { if ( ! this . _content ) { const result = await content ( this . pageid . toString ( ) , pageOptions ?. redirect ) ; this . parentid = result . ids . parentId ; this . revid = result . ids . revId ; this . _content = result . result ; } return this . _content ; } catch ( error ) { throw new contentError ( error ) ; } } ; remarks This method is part of the  Page .
     *
     *  param title param listOptions -  listOptions returns public categories = async ( listOptions ? ) : Promise < Array < string > > => { try { if ( ! this . _categories ) { const result = await categories ( this . pageid . toString ( ) , listOptions ) ; this . _categories = result ; } return this . _categories ; } catch ( error ) { throw new categoriesError ( error ) ; } } ; remarks This method is part of the  Page .
     *
     *  param title param listOptions -  listOptions returns public links = async ( listOptions ? ) : Promise < Array < string > > => { try { if ( ! this . _links ) { const result = await links ( this . pageid . toString ( ) , listOptions ) ; this . _links = result ; } return this . _links ; } catch ( error ) { throw new linksError ( error ) ; } } ; remarks This method is part of the  Page .
     *
     *  param title param listOptions -  listOptions returns public references = async ( listOptions ? ) : Promise < Array < string > > => { try { if ( ! this . _references ) { const result = await references ( this . pageid . toString ( ) , listOptions ) ; this . _references = result ; } return this . _references ; } catch ( error ) { throw new linksError ( error ) ; } } ; remarks This method is part of the  Page .
     *
     *  param title param redirect returns The coordinates as  coordinatesResult public coordinates = async ( pageOptions ? ) : Promise < coordinatesResult > => { try { if ( ! this . _coordinates ) { const result = await coordinates ( this . pageid . toString ( ) , pageOptions ?. redirect ) ; this . _coordinates = result ; } return this . _coordinates ; } catch ( error ) { throw new coordinatesError ( error ) ; } } ; remarks This method is part of the  Page .
     *
     *  param title param listOptions -  listOptions returns The links as an array of  langLinksResult public langLinks = async ( listOptions ? ) : Promise < Array < langLinksResult > > => { try { if ( ! this . _langLinks ) { const result = await langLinks ( this . pageid . toString ( ) , listOptions ) ; this . _langLinks = result ; } return this . _langLinks ; } catch ( error ) { throw new linksError ( error ) ; } } ; remarks This method is part of the  Page .
     *
     *  param title param redirect returns public infobox = async ( pageOptions ? ) : Promise < any > => { try { if ( ! this . _infobox ) { const result = await infobox ( this . pageid . toString ( ) , pageOptions ?. redirect ) ; this . _infobox = result ; } return this . _infobox ; } catch ( error ) { throw new infoboxError ( error ) ; } } ; remarks This method is part of the  Page .
     *
     *  param title param redirect returns public tables = async ( pageOptions ? ) : Promise < Array < any > > => { try { if ( ! this . _tables ) { const result = await tables ( this . pageid . toString ( ) , pageOptions ?. redirect ) ; this . _tables = result ; } return this . _tables ; } catch ( error ) { throw new infoboxError ( error ) ; } } ; remarks This method is part of the  Page .
     *
     *  param title param redirect returns The related pages and summary as an array of  wikiSummary experimental public related = async ( pageOptions ? ) : Promise < relatedResult > => { try { if ( ! this . _related ) { const result = await related ( this . title , pageOptions ?. redirect ) ; this . _related = result ; } return this . _related ; } catch ( error ) { throw new relatedError ( error ) ; } } ; remarks param title param redirect returns The related pages and summary as an array of  wikiMediaResult experimental public media = async ( pageOptions ? ) : Promise < wikiMediaResult > => { try { if ( ! this . _media ) { const result = await media ( this . title , pageOptions ?. redirect ) ; this . _media = result ; } return this . _media ; } catch ( error ) { throw new mediaError ( error ) ; } } ; param title param redirect returns public mobileHtml = async ( pageOptions ? ) : Promise < notFound | string > => { try { if ( ! this . _mobileHtml ) { const result = await mobileHtml ( this . title , pageOptions ?. redirect ) ; this . _mobileHtml = result ; } return this . _mobileHtml ; } catch ( error ) { throw new htmlError ( error ) ; } } ; param pdfOptions -  pdfOptions returns public pdf = async ( pdfOptions ? ) : Promise < string > => { try { const result = await pdf ( this . title , pdfOptions ) ; return result ; } catch ( error ) { throw new pdfError ( error ) ; } } ; public async runMethod ( functionName ) { try { const result = await eval ( `this.${ functionName }()` ) ; return result ; } catch ( error ) { throw new preloadError ( error ) ; } } } remarks param title param listOptions -  listOptions returns an array of imageResult  imageResult export const images = async ( title , listOptions ? ) : Promise < Array < imageResult > > => { try { let imageOptions = { generator : 'images' , gimlimit : listOptions ?. limit || 5 , prop : 'imageinfo' , iiprop : 'url' } ; imageOptions = setPageIdOrTitleParam ( imageOptions , title ) ; const response = await request ( imageOptions , listOptions ?. redirect ) ; const images = [ ] ; const imageKeys = Object . keys ( response . query . pages ) ; for ( const image of imageKeys ) { const imageInfo = response . query . pages [ image ] ; imageInfo . url = imageInfo . imageinfo [ 0 ] . url ; images . push ( imageInfo ) ; } return images ; } catch ( error ) { throw new imageError ( error ) ; } } ; remarks param title param redirect returns export const intro = async ( title , redirect = true ) : Promise < string > => { try { let introOptions = { prop : 'extracts' , explaintext : '' , exintro : '' , } ; introOptions = setPageIdOrTitleParam ( introOptions , title ) ; const response = await request ( introOptions , redirect ) ; const pageId = setPageId ( introOptions , response ) ; return response ?. query ?. pages [ pageId ] . extract ; } catch ( error ) { throw new introError ( error ) ; } } ; remarks param title param redirect returns beta export const html = async ( title , redirect = true ) : Promise < string > => { try { let htmlOptions = { 'prop' : 'revisions' , 'rvprop' : 'content' , 'rvlimit' : 1 , 'rvparse' : '' } ; htmlOptions = setPageIdOrTitleParam ( htmlOptions , title ) ; const response = await request ( htmlOptions , redirect ) ; const pageId = setPageId ( htmlOptions , response ) ; return response . query . pages [ pageId ] . revisions [ 0 ] [ '*' ] ; } catch ( error ) { throw new htmlError ( error ) ; } } ; remarks param title param redirect returns export const content = async ( title , redirect = true ) : Promise < any > => { try { let contentOptions = { 'prop' : 'extracts|revisions' , 'explaintext' : '' , 'rvprop' : 'ids' } ; contentOptions = setPageIdOrTitleParam ( contentOptions , title ) ; const response = await request ( contentOptions , redirect ) ; const pageId = setPageId ( contentOptions , response ) ; const result = response [ 'query' ] [ 'pages' ] [ pageId ] [ 'extract' ] ; const ids = { revisionId : response [ 'query' ] [ 'pages' ] [ pageId ] [ 'revisions' ] [ 0 ] [ 'revid' ] , parentId : response [ 'query' ] [ 'pages' ] [ pageId ] [ 'revisions' ] [ 0 ] [ 'parentid' ] } ; return { result , ids } ; } catch ( error ) { throw new contentError ( error ) ; } } ; remarks param title param listOptions -  listOptions returns export const categories = async ( title , listOptions ? ) : Promise < Array < string > > => { try { let categoryOptions = { prop : 'categories' , pllimit : listOptions ?. limit , } ; categoryOptions = setPageIdOrTitleParam ( categoryOptions , title ) ; const response = await request ( categoryOptions , listOptions ?. redirect ) ; const pageId = setPageId ( categoryOptions , response ) ; return response . query . pages [ pageId ] . categories . map ( ( category ) => category . title ) ; } catch ( error ) { throw new categoriesError ( error ) ; } } ; remarks param title param listOptions -  listOptions returns export const links = async ( title , listOptions ? ) : Promise < Array < string > > => { try { let linksOptions = { prop : 'links' , plnamespace : 0 , pllimit : listOptions ?. limit || 'max' , } ; linksOptions = setPageIdOrTitleParam ( linksOptions , title ) ; const response = await request ( linksOptions , listOptions ?. redirect ) ; const pageId = setPageId ( linksOptions , response ) ; const result = response . query . pages [ pageId ] . links . map ( ( link ) => link . title ) ; return result ; } catch ( error ) { throw new linksError ( error ) ; } } ; remarks param title param listOptions -  listOptions returns export const references = async ( title , listOptions ? ) : Promise < Array < string > > => { try { let extLinksOptions = { prop : 'extlinks' , ellimit : listOptions ?. limit || 'max' , } ; extLinksOptions = setPageIdOrTitleParam ( extLinksOptions , title ) ; const response = await request ( extLinksOptions , listOptions ?. redirect ) ; const pageId = setPageId ( extLinksOptions , response ) ; const result = response . query . pages [ pageId ] . extlinks . map ( ( link ) => link [ '*' ] ) ; return result ; } catch ( error ) { throw new linksError ( error ) ; } } ; remarks param title param redirect returns The coordinates as  coordinatesResult export const coordinates = async ( title , redirect = true ) : Promise < coordinatesResult > => { try { let coordinatesOptions = { prop : 'coordinates' , } ; coordinatesOptions = setPageIdOrTitleParam ( coordinatesOptions , title ) ; const response = await request ( coordinatesOptions , redirect ) ; const pageId = setPageId ( coordinatesOptions , response ) ; const coordinates = response . query . pages [ pageId ] . coordinates ; return coordinates ? coordinates [ 0 ] : null ; } catch ( error ) { throw new coordinatesError ( error ) ; } } ; remarks param title param listOptions -  listOptions returns The links as an array of  langLinksResult export const langLinks = async ( title , listOptions ? ) : Promise < Array < langLinksResult > > => { try { let languageOptions = { prop : 'langlinks' , lllimit : listOptions ?. limit || 'max' , llprop : 'url' } ; languageOptions = setPageIdOrTitleParam ( languageOptions , title ) ; const response = await request ( languageOptions , listOptions ?. redirect ) ; const pageId = setPageId ( languageOptions , response ) ; const result = response . query . pages [ pageId ] . langlinks . map ( ( link ) => { return { lang : link . lang , title : link [ '*' ] , url : link . url } ; } ) ; return result ; } catch ( error ) { throw new linksError ( error ) ; } } ; remarks param title param redirect returns export const infobox = async ( title , redirect = true ) : Promise < any > => { try { const infoboxOptions = { prop : 'revisions' , rvprop : 'content' , rvsection : 0 } ; const fullInfo = await rawInfo ( title , infoboxOptions , redirect ) ; const info = infoboxParser ( fullInfo ) . general ; return info ; } catch ( error ) { throw new infoboxError ( error ) ; } } ; remarks param title param redirect returns export const tables = async ( title , redirect = true ) : Promise < Array < any > > => { try { const tableOptions = { prop : 'revisions' , rvprop : 'content' , } ; const fullInfo = await rawInfo ( title , tableOptions , redirect ) ; const info = infoboxParser ( fullInfo ) . tables ; return info ; } catch ( error ) { throw new infoboxError ( error ) ; } } ; remarks param title param redirect returns export const rawInfo = async ( title , options , redirect = true ) : Promise < any > => { try { options = setPageIdOrTitleParam ( options , title ) ; const response = await request ( options , redirect ) ; if ( ! ( response . query ?. pages ) ) { throw new wikiError ( MSGS . INFOBOX_NOT_EXIST ) ; } const pageId = setPageId ( options , response ) ; const data = response . query . pages [ pageId ] [ 'revisions' ] [ 0 ] ; return data ? data [ '*' ] : '' ; } catch ( error ) { throw new infoboxError ( error ) ; } } ; remarks param title param redirect returns The summary of the page as  wikiSummary export const summary = async ( title , redirect = true ) : Promise < wikiSummary > => { try { const path = 'page/summary/' + title . replace ( " " , "_" ) ; const response = await makeRestRequest ( path , redirect ) ; return response ; } catch ( error ) { throw new summaryError ( error ) ; } } ; remarks param title param redirect returns The related pages and summary as an array of  wikiSummary experimental export const related = async ( title , redirect = true ) : Promise < relatedResult > => { try { const path = 'page/related/' + title . replace ( " " , "_" ) ; const response = await makeRestRequest ( path , 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 export const media = async ( title , redirect = true ) : Promise < wikiMediaResult > => { try { const path = 'page/media-list/' + title . replace ( " " , "_" ) ; const response = await makeRestRequest ( path , redirect ) ; return response ; } catch ( error ) { throw new mediaError ( error ) ; } } ; param title param redirect returns export const mobileHtml = async ( title , redirect = true ) : Promise < notFound | string > => { try { const path = `page/mobile-html/${ title }` ; const result = await makeRestRequest ( path , redirect ) ; return result ; } catch ( error ) { throw new htmlError ( error ) ; } } ; param title param pdfOptions -  pdfOptions returns export const pdf = async ( title , pdfOptions ? ) : Promise < string > => { try { let path = `page/pdf/${ title }` ; pdfOptions ?. format ? path += `/${ pdfOptions . format }` : null ; pdfOptions ?. type ? path += `/${ pdfOptions . type }` : null ; const result = returnRestUrl ( path ) ; return result ; } catch ( error ) { throw new pdfError ( error ) ; } } ; param format param query param language returns export const citation = async ( query , format ? , language ? ) : Promise < any > => { try { let path = `data/citation` ; path += format ? `/${ format }` : `/mediawiki` ; path += `/${ query }` ; language ? path += `/${ language }` : null ; const result = await makeRestRequest ( path ) ; return result ; } catch ( error ) { throw new citationError ( error ) ; } } ; export default Page ;