| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
const fetchJson = require( './fetch.js' ); |
| 5 |
const urlGenerator = require( './urlGenerator.js' ); |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
function nullish( a, b ) { |
| 26 |
return ( a !== null && a !== undefined ) ? a : b; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 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 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
function restSearchClient( config ) { |
| 94 |
return config.get( 'wgVectorSearchClient', { |
| 95 |
|
| 96 |
|
| 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( ( 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; |