mediawiki-skins-Citizen/resources/skins.citizen.search/gateway/mwRestApi.js
alistair3149 48deb87709
fix: add null check when accessing undefined keys
Co-authored-by: Hannes Kruse <hannes@octofox.de>
2021-05-18 12:34:08 -04:00

45 lines
900 B
JavaScript

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 = [];
data = data?.pages ?? [];
for ( let i = 0; i < data.length; i++ ) {
results[ i ] = {
id: data[ i ].id,
title: data[ i ].title,
description: data[ i ].description
};
if ( data[ i ].thumbnail && data[ i ].thumbnail.url ) {
results[ i ].thumbnail = data[ i ].thumbnail.url;
}
}
return results;
}
module.exports = {
getUrl: getUrl,
convertDataToResults: convertDataToResults
};