Skip to content

Commit cce4f2e

Browse files
olancevvo
authored andcommitted
feat(custom client): allows to provide a custom JS client instance (#1948)
By passing createAlgoliaClient option to instantsearch() you can now provide your own client instance instead of relying on the one instantiated internally. Usage: ```js createAlgoliaClient: function(algoliasearch, appId, apiKey) { // return an algolia client here }
1 parent cb77dde commit cce4f2e

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/lib/InstantSearch.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import version from './version.js';
1313
import createHelpers from './createHelpers.js';
1414

1515
function defaultCreateURL() { return '#'; }
16+
const defaultCreateAlgoliaClient = (defaultAlgoliasearch, appId, apiKey) => defaultAlgoliasearch(appId, apiKey);
1617

1718
/**
1819
* @function instantsearch
@@ -24,6 +25,12 @@ function defaultCreateURL() { return '#'; }
2425
* @param {function} [options.searchFunction] A hook that will be called each time a search needs to be done, with the
2526
* helper as a parameter. It's your responsibility to call helper.search(). This option allows you to avoid doing
2627
* searches at page load for example.
28+
* @param {function} [options.createAlgoliaClient] Allows you to provide your own algolia client instead of
29+
* the one instantiated internally by instantsearch.js. Useful in situations where you need
30+
* to setup complex options on the client or if you need to share it easily.
31+
* Usage:
32+
* `createAlgoliaClient: function(algoliasearch, appId, apiKey) { return anyCustomClient; }`
33+
* We forward `algoliasearch` which is the original algoliasearch module imported inside instantsearch.js
2734
* @param {Object} [options.searchParameters] Additional parameters to pass to
2835
* the Algolia API.
2936
* [Full documentation](https://community.algolia.com/algoliasearch-helper-js/reference.html#searchparameters)
@@ -60,6 +67,7 @@ class InstantSearch extends EventEmitter {
6067
searchParameters = {},
6168
urlSync = null,
6269
searchFunction,
70+
createAlgoliaClient = defaultCreateAlgoliaClient,
6371
}) {
6472
super();
6573
if (appId === null || apiKey === null || indexName === null) {
@@ -72,7 +80,7 @@ Usage: instantsearch({
7280
throw new Error(usage);
7381
}
7482

75-
const client = algoliasearch(appId, apiKey);
83+
const client = createAlgoliaClient(algoliasearch, appId, apiKey);
7684
client.addAlgoliaAgent(`instantsearch.js ${version}`);
7785

7886
this.client = client;

src/lib/__tests__/InstantSearch-test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,42 @@ describe('InstantSearch lifecycle', () => {
8585
expect(algoliasearchHelper.notCalled).toBe(true, 'algoliasearchHelper not yet called');
8686
});
8787

88+
context('when providing a custom client module', () => {
89+
let createAlgoliaClient;
90+
let customAppID;
91+
let customApiKey;
92+
93+
beforeEach(() => {
94+
// InstantSearch is being called once at the top-level context, so reset the `algoliasearch` spy
95+
algoliasearch.reset();
96+
97+
// Create a spy to act as a clientInstanceFunction that returns a custom client
98+
createAlgoliaClient = sinon.stub().returns(client);
99+
customAppID = 'customAppID';
100+
customApiKey = 'customAPIKey';
101+
102+
// Create a new InstantSearch instance with custom client function
103+
search = new InstantSearch({
104+
appId: customAppID,
105+
apiKey: customApiKey,
106+
indexName,
107+
searchParameters,
108+
urlSync: {},
109+
createAlgoliaClient,
110+
});
111+
});
112+
113+
it('does not call algoliasearch directly', () => {
114+
expect(algoliasearch.calledOnce).toBe(false, 'algoliasearch not called');
115+
});
116+
117+
it('calls createAlgoliaClient(appId, apiKey)', () => {
118+
expect(createAlgoliaClient.calledOnce).toBe(true, 'clientInstanceFunction called once');
119+
expect(createAlgoliaClient.args[0])
120+
.toEqual([algoliasearch, customAppID, customApiKey]);
121+
});
122+
});
123+
88124
context('when adding a widget without render and init', () => {
89125
let widget;
90126

0 commit comments

Comments
 (0)