Debian Codegraph @

skins/Vector/resources/skins.vector.search/restSearchClient.js

raw symbols SCIP
1 // / <reference lib="@wikimedia/types" />
2 /** @module restSearchClient */
3
4 const fetchJson = require( './fetch.js' );
5 const urlGenerator = require( './urlGenerator.js' );
6
7 /**
8 * @typedef {Object} RestResponse
9 * @property {RestResult[]} pages
10 */
11
12 /**
13 * @typedef {Object} SearchResponse
14 * @property {string} query
15 * @property {SearchResult[]} results
16 */
17
18 /**
19 * Nullish coalescing operator (??) helper
20 *
21 * @param {any} a
22 * @param {any} b
23 * @return {any}
24 */
25 function nullish( a, b ) {
26 return ( a !== null && a !== undefined ) ? a : b;
27 }
28
29 /**
30 * @param {MwMap} config
31 * @param {string} query
32 * @param {RestResponse} restResponse
33 * @param {boolean} showDescription
34 * @return {SearchResponse}
35 */
36 function adaptApiResponse( config, query, restResponse, showDescription ) {
37 const urlGeneratorInstance = urlGenerator( config );
38 return {
39 query,
40 results: restResponse.pages.map( ( page, index ) => {
41 const thumbnail = page.thumbnail;
42 return {
43 id: page.id,
44 value: page.id || -( index + 1 ),
45 label: page.title,
46 key: page.key,
47 title: page.title,
48 description: showDescription ? page.description : undefined,
49 url: urlGeneratorInstance.generateUrl( page ),
50 thumbnail: thumbnail ? {
51 url: thumbnail.url,
52 width: nullish( thumbnail.width, undefined ),
53 height: nullish( thumbnail.height, undefined )
54 } : undefined
55 };
56 } )
57 };
58 }
59
60 /**
61 * @typedef {Object} AbortableSearchFetch
62 * @property {Promise<SearchResponse>} fetch
63 * @property {Function} abort
64 */
65
66 /**
67 * @callback fetchByTitle
68 * @param {string} query The search term.
69 * @param {number} [limit] Maximum number of results.
70 * @param {boolean} [showDescription] Whether descriptions should be added to the results.
71 * @return {AbortableSearchFetch}
72 */
73
74 /**
75 * @callback loadMore
76 * @param {string} query The search term.
77 * @param {number} offset The number of search results that were already loaded.
78 * @param {number} [limit] How many further search results to load (at most).
79 * @param {boolean} [showDescription] Whether descriptions should be added to the results.
80 * @return {AbortableSearchFetch}
81 */
82
83 /**
84 * @typedef {Object} SearchClient
85 * @property {fetchByTitle} fetchByTitle
86 * @property {loadMore} [loadMore]
87 */
88
89 /**
90 * @param {MwMap} config
91 * @return {SearchClient}
92 */
93 function restSearchClient( config ) {
94 return config.get( 'wgVectorSearchClient', {
95 /**
96 * @type {fetchByTitle}
97 */
98 fetchByTitle: ( q, limit = 10, showDescription = true ) => {
99 const searchApiUrl = config.get( 'wgVectorSearchApiUrl',
100 config.get( 'wgScriptPath' ) + '/rest.php'
101 );
102 const params = { q, limit: limit.toString() };
103 const search = new URLSearchParams( params );
104 const url = `${ searchApiUrl }/v1/search/title?${ search.toString() }`;
105 const result = fetchJson( url, {
106 headers: {
107 accept: 'application/json'
108 }
109 } );
110 const searchResponsePromise = result.fetch
111 .then( ( /** @type {RestResponse} */ res ) => adaptApiResponse(
112 config, q, res, showDescription
113 ) );
114 return {
115 abort: result.abort,
116 fetch: searchResponsePromise
117 };
118 }
119 } );
120 }
121
122 module.exports = restSearchClient;