2022-02-01 20:52:16 +00:00
|
|
|
/* global RestResult, SearchResult */
|
2022-02-02 21:17:56 +00:00
|
|
|
/** @module restSearchClient */
|
|
|
|
|
2022-02-14 20:12:03 +00:00
|
|
|
const fetchJson = require( './fetch.js' );
|
2022-02-01 20:52:16 +00:00
|
|
|
const urlGenerator = require( './urlGenerator.js' );
|
2022-02-02 21:17:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} RestResponse
|
|
|
|
* @property {RestResult[]} pages
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} SearchResponse
|
|
|
|
* @property {string} query
|
|
|
|
* @property {SearchResult[]} results
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nullish coalescing operator (??) helper
|
|
|
|
*
|
|
|
|
* @param {any} a
|
|
|
|
* @param {any} b
|
|
|
|
* @return {any}
|
|
|
|
*/
|
|
|
|
function nullish( a, b ) {
|
|
|
|
return ( a !== null && a !== undefined ) ? a : b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-02-01 20:52:16 +00:00
|
|
|
* @param {MwMap} config
|
2022-02-02 21:17:56 +00:00
|
|
|
* @param {string} query
|
|
|
|
* @param {RestResponse} restResponse
|
2022-02-01 20:52:16 +00:00
|
|
|
* @param {boolean} showDescription
|
2022-02-02 21:17:56 +00:00
|
|
|
* @return {SearchResponse}
|
|
|
|
*/
|
2022-02-01 20:52:16 +00:00
|
|
|
function adaptApiResponse( config, query, restResponse, showDescription ) {
|
|
|
|
const urlGeneratorInstance = urlGenerator( config );
|
2022-02-02 21:17:56 +00:00
|
|
|
return {
|
|
|
|
query,
|
2022-08-26 17:24:12 +00:00
|
|
|
results: restResponse.pages.map( ( page, index ) => {
|
2022-02-02 21:17:56 +00:00
|
|
|
const thumbnail = page.thumbnail;
|
|
|
|
return {
|
|
|
|
id: page.id,
|
2022-08-26 17:24:12 +00:00
|
|
|
value: page.id || -( index + 1 ),
|
2022-02-01 20:52:16 +00:00
|
|
|
label: page.title,
|
2022-02-02 21:17:56 +00:00
|
|
|
key: page.key,
|
|
|
|
title: page.title,
|
2022-02-01 20:52:16 +00:00
|
|
|
description: showDescription ? page.description : undefined,
|
|
|
|
url: urlGeneratorInstance.generateUrl( page ),
|
2022-02-02 21:17:56 +00:00
|
|
|
thumbnail: thumbnail ? {
|
|
|
|
url: thumbnail.url,
|
|
|
|
width: nullish( thumbnail.width, undefined ),
|
|
|
|
height: nullish( thumbnail.height, undefined )
|
|
|
|
} : undefined
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} AbortableSearchFetch
|
|
|
|
* @property {Promise<SearchResponse>} fetch
|
|
|
|
* @property {Function} abort
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback fetchByTitle
|
|
|
|
* @param {string} query The search term.
|
2022-10-06 00:38:38 +00:00
|
|
|
* @param {string} searchApiUrl The URL to rest.php
|
2022-02-02 21:17:56 +00:00
|
|
|
* @param {number} [limit] Maximum number of results.
|
|
|
|
* @return {AbortableSearchFetch}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} SearchClient
|
|
|
|
* @property {fetchByTitle} fetchByTitle
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {MwMap} config
|
|
|
|
* @return {SearchClient}
|
|
|
|
*/
|
|
|
|
function restSearchClient( config ) {
|
2022-09-30 16:25:35 +00:00
|
|
|
return config.get( 'wgVectorSearchClient', {
|
2022-02-02 21:17:56 +00:00
|
|
|
/**
|
|
|
|
* @type {fetchByTitle}
|
|
|
|
*/
|
2022-10-06 00:38:38 +00:00
|
|
|
fetchByTitle: ( q, searchApiUrl, limit = 10, showDescription = true ) => {
|
2022-02-02 21:17:56 +00:00
|
|
|
const params = { q, limit };
|
2022-10-06 00:38:38 +00:00
|
|
|
const url = searchApiUrl + '/v1/search/title?' + $.param( params );
|
2022-02-02 21:17:56 +00:00
|
|
|
const result = fetchJson( url, {
|
|
|
|
headers: {
|
|
|
|
accept: 'application/json'
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
const searchResponsePromise = result.fetch
|
|
|
|
.then( ( /** @type {RestResponse} */ res ) => {
|
2022-02-01 20:52:16 +00:00
|
|
|
return adaptApiResponse( config, q, res, showDescription );
|
2022-02-02 21:17:56 +00:00
|
|
|
} );
|
|
|
|
return {
|
|
|
|
abort: result.abort,
|
|
|
|
fetch: searchResponsePromise
|
|
|
|
};
|
|
|
|
}
|
2022-09-30 16:25:35 +00:00
|
|
|
} );
|
2022-02-02 21:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = restSearchClient;
|