diff --git a/resources/skins.vector.search/App.vue b/resources/skins.vector.search/App.vue index e34ebad84..825741f0a 100644 --- a/resources/skins.vector.search/App.vue +++ b/resources/skins.vector.search/App.vue @@ -162,10 +162,7 @@ module.exports = exports = defineComponent( { * @param {string} value */ onInput: function ( value ) { - const searchApiUrl = mw.config.get( 'wgVectorSearchApiUrl', - mw.config.get( 'wgScriptPath' ) + '/rest.php' - ), - query = value.trim(); + const query = value.trim(); this.currentSearchQuery = query; @@ -177,7 +174,7 @@ module.exports = exports = defineComponent( { instrumentation.listeners.onFetchStart(); - restClient.fetchByTitle( query, searchApiUrl, 10, this.showDescription ).fetch + restClient.fetchByTitle( query, 10, this.showDescription ).fetch .then( ( data ) => { // Only use these results if they're still relevant // If currentSearchQuery !== query, these results are for a previous search diff --git a/resources/skins.vector.search/restSearchClient.js b/resources/skins.vector.search/restSearchClient.js index 6e5e03b02..a944a929a 100644 --- a/resources/skins.vector.search/restSearchClient.js +++ b/resources/skins.vector.search/restSearchClient.js @@ -66,8 +66,8 @@ function adaptApiResponse( config, query, restResponse, showDescription ) { /** * @callback fetchByTitle * @param {string} query The search term. - * @param {string} searchApiUrl The URL to rest.php * @param {number} [limit] Maximum number of results. + * @param {boolean} [showDescription] Whether descriptions should be added to the results. * @return {AbortableSearchFetch} */ @@ -85,7 +85,10 @@ function restSearchClient( config ) { /** * @type {fetchByTitle} */ - fetchByTitle: ( q, searchApiUrl, limit = 10, showDescription = true ) => { + fetchByTitle: ( q, limit = 10, showDescription = true ) => { + const searchApiUrl = config.get( 'wgVectorSearchApiUrl', + config.get( 'wgScriptPath' ) + '/rest.php' + ); const params = { q, limit }; const url = searchApiUrl + '/v1/search/title?' + $.param( params ); const result = fetchJson( url, { diff --git a/tests/jest/restSearchClient.test.js b/tests/jest/restSearchClient.test.js index 295e72dcb..816e88ea3 100644 --- a/tests/jest/restSearchClient.test.js +++ b/tests/jest/restSearchClient.test.js @@ -11,6 +11,9 @@ const configMock = { if ( key === 'wgScript' ) { return '/w/index.php'; } + if ( key === 'wgVectorSearchApiUrl' ) { + return 'https://en.wikipedia.org/w/rest.php'; + } return fallback; } ), set: jest.fn() @@ -66,7 +69,6 @@ describe( 'restApiSearchClient', () => { const searchResult = await restSearchClient( configMock ).fetchByTitle( 'media', - 'https://en.wikipedia.org/w/rest.php', 2 ).fetch; @@ -98,8 +100,7 @@ describe( 'restApiSearchClient', () => { fetchMock.mockOnce( JSON.stringify( restResponse ) ); const searchResult = await restSearchClient( configMock ).fetchByTitle( - 'thereIsNothingLikeThis', - 'https://en.wikipedia.org/w/rest.php' + 'thereIsNothingLikeThis' ).fetch; /* eslint-disable-next-line compat/compat */ @@ -122,8 +123,7 @@ describe( 'restApiSearchClient', () => { fetchMock.mockRejectOnce( new Error( 'failed' ) ); await expect( restSearchClient( configMock ).fetchByTitle( - 'anything', - 'https://en.wikipedia.org/w/rest.php' + 'anything' ).fetch ).rejects.toThrow( 'failed' ); } ); }