2021-05-17 20:34:14 +00:00
|
|
|
const config = require( '../config.json' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build URL used for fetch request
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @return {string} url
|
|
|
|
*/
|
|
|
|
function getUrl( input ) {
|
|
|
|
const endpoint = config.wgScriptPath + '/rest.php/v1/search/title?q=',
|
|
|
|
query = '&limit=' + config.wgCitizenMaxSearchResults;
|
|
|
|
|
|
|
|
return endpoint + input + query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map raw response to Results object
|
|
|
|
*
|
|
|
|
* @param {Object} data
|
|
|
|
* @return {Object} Results
|
|
|
|
*/
|
|
|
|
function convertDataToResults( data ) {
|
|
|
|
const results = [];
|
|
|
|
|
2021-05-18 16:34:08 +00:00
|
|
|
data = data?.pages ?? [];
|
2021-05-17 20:34:14 +00:00
|
|
|
|
|
|
|
for ( let i = 0; i < data.length; i++ ) {
|
|
|
|
results[ i ] = {
|
|
|
|
id: data[ i ].id,
|
|
|
|
title: data[ i ].title,
|
|
|
|
description: data[ i ].description
|
|
|
|
};
|
2022-05-20 23:10:36 +00:00
|
|
|
// Redirect title
|
|
|
|
// Since 1.38
|
|
|
|
if ( data[ i ].matched_title ) {
|
|
|
|
results[ i ].matchedTitle = data[ i ].matched_title;
|
|
|
|
}
|
|
|
|
|
2021-05-17 20:34:14 +00:00
|
|
|
if ( data[ i ].thumbnail && data[ i ].thumbnail.url ) {
|
|
|
|
results[ i ].thumbnail = data[ i ].thumbnail.url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getUrl: getUrl,
|
|
|
|
convertDataToResults: convertDataToResults
|
|
|
|
};
|