UNPKG

wtf_wikipedia

Version:
92 lines (80 loc) 2.56 kB
const { isArray } = require('../_lib/helpers') const isInterWiki = /(wikibooks|wikidata|wikimedia|wikinews|wikipedia|wikiquote|wikisource|wikispecies|wikiversity|wikivoyage|wiktionary|foundation|meta)\.org/ const defaults = { action: 'query', prop: 'revisions|pageprops', // we use the 'revisions' api here, instead of the Raw api, for its CORS-rules.. rvprop: 'content', maxlag: 5, rvslots: 'main', origin: '*', format: 'json', redirects: 'true', } /** * turns a object into a query string * * @private * @param {Object<string, string | number | boolean>} obj * @returns {string} QueryString */ const toQueryString = function (obj) { return Object.entries(obj) .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`) .join('&') } /** * cleans and prepares the tile by replacing the spaces with underscores (_) and trimming the white spaces of the ends * * @private * @param {string} page the title that needs cleaning * @returns {string} the cleaned title */ const cleanTitle = (page) => { return page.replace(/ /g, '_') .trim() } /** * generates the url for fetching the pages * * @private * @param {import('.').fetchDefaults} options * @returns {string} the url that can be used to make the fetch */ const makeUrl = function (options) { let params = Object.assign({}, defaults) //default url let apiPath = '' //add support for third party apis if (options.domain) { //wikimedia is the only api that uses `/w/api` as its path. other wikis use other paths let path = isInterWiki.test(options.domain) ? 'w/api.php' : options.path apiPath = `https://${options.domain}/${path}?` } else if (options.lang && options.wiki) { apiPath = `https://${options.lang}.${options.wiki}.org/w/api.php?` } else { return '' } if (!options.follow_redirects) { delete params.redirects } //support numerical ids let title = options.title if (typeof title === 'number') { //single pageId params.pageids = title } else if (typeof title === 'string') { //single page title params.titles = cleanTitle(title) } else if (title !== undefined && isArray(title) && typeof title[0] === 'number') { //pageid array params.pageids = title.join('|') } else if (title !== undefined && isArray(title) === true && typeof title[0] === 'string') { //title array params.titles = title.map(cleanTitle).join('|') } else { return '' } //make it! return `${apiPath}${toQueryString(params)}` } module.exports = makeUrl