mediawiki-skins-Citizen/resources/skins.citizen.search/gateway/mwRestApi.js
alistair3149 a5c52e48b7
feat(core): indicate when search results are from redirects
For REST API, search results will now indicate if they are from redirects.
The current implementation is that it will always show up, the logic to
trim redundant info will come later

Related: T303013
2022-05-20 19:10:36 -04:00

51 lines
1 KiB
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
};
// Redirect title
// Since 1.38
if ( data[ i ].matched_title ) {
results[ i ].matchedTitle = data[ i ].matched_title;
}
if ( data[ i ].thumbnail && data[ i ].thumbnail.url ) {
results[ i ].thumbnail = data[ i ].thumbnail.url;
}
}
return results;
}
module.exports = {
getUrl: getUrl,
convertDataToResults: convertDataToResults
};