From 5f69d42677d21b0e00cc7a479ba3ec8722752627 Mon Sep 17 00:00:00 2001 From: Maxime Date: Tue, 8 Aug 2017 10:15:39 -0700 Subject: [PATCH] add queryHook argument --- README.md | 15 +++++++++++++++ src/lib/DocSearch.js | 11 +++++++++-- src/lib/__tests__/DocSearch-test.js | 22 ++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 72bee06a..b7c806b5 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,21 @@ var search = docsearch({ }); ``` +#### queryHook + +If you want modify the query before it is send to Algolia you can pass the `queryHook` option. + +```javascript +var search = docsearch({ + apiKey: '', + indexName: '', + inputSelector: '', + queryHook: function (query) { + return query + "_modified"; + } +}); +``` + #### transformData If you want to modify the hits before displaying them you can make use of the diff --git a/src/lib/DocSearch.js b/src/lib/DocSearch.js index 15951aa0..a3a142bd 100644 --- a/src/lib/DocSearch.js +++ b/src/lib/DocSearch.js @@ -43,6 +43,7 @@ class DocSearch { autoselect: true, }, transformData = false, + queryHook = false, handleSelected = false, enhancedSearchInput = false, layout = 'collumns', @@ -56,6 +57,7 @@ class DocSearch { algoliaOptions, autocompleteOptions, transformData, + queryHook, handleSelected, enhancedSearchInput, layout, @@ -89,7 +91,7 @@ class DocSearch { this.autocomplete = autocomplete(this.input, autocompleteOptions, [ { - source: this.getAutocompleteSource(transformData), + source: this.getAutocompleteSource(transformData, queryHook), templates: { suggestion: DocSearch.getSuggestionTemplate(this.isSimpleLayout), footer: templates.footer, @@ -170,11 +172,16 @@ class DocSearch { * the Algolia index and call the callbacks with the formatted hits. * @function getAutocompleteSource * @param {function} transformData An optional function to transform the hits + * @param {function} queryHook An optional function to transform the query * @returns {function} Method to be passed as the `source` option of * autocomplete */ - getAutocompleteSource(transformData) { + getAutocompleteSource(transformData, queryHook) { return (query, callback) => { + if (queryHook) { + query = queryHook(query) || query; + } + this.client .search([ { diff --git a/src/lib/__tests__/DocSearch-test.js b/src/lib/__tests__/DocSearch-test.js index b0396147..1682e391 100644 --- a/src/lib/__tests__/DocSearch-test.js +++ b/src/lib/__tests__/DocSearch-test.js @@ -367,6 +367,28 @@ describe('DocSearch', () => { expect(client.search.calledWith([expectedArguments])).toBe(true); }); }); + + describe('when queryHook is used', () => { + it('calls the Agolia client with the correct parameters', () => { + // Given + const actual = docsearch.getAutocompleteSource(false, function(query) { + return query + ' modified'; + }); + + // When + actual('query'); + + // Then + expect(client.search.calledOnce).toBe(true); + // expect(resolvedQuery.calledOnce).toBe(true); + const expectedArguments = { + indexName: 'indexName', + query: 'query modified', + params: { hitsPerPage: 5 }, + }; + expect(client.search.calledWith([expectedArguments])).toBe(true); + }); + }); }); describe('handleSelected', () => {