Skip to content

Commit

Permalink
feat(insights): allow to pass init params (#1230)
Browse files Browse the repository at this point in the history
* feat(insights): allow to pass init params

* bundlesize

* add link to param
  • Loading branch information
aymeric-giraudet committed Jan 12, 2024
1 parent 7f5ba08 commit 186ff9b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
4 changes: 2 additions & 2 deletions bundlesize.config.json
Expand Up @@ -6,15 +6,15 @@
},
{
"path": "packages/autocomplete-js/dist/umd/index.production.js",
"maxSize": "21 kB"
"maxSize": "21.25 kB"
},
{
"path": "packages/autocomplete-preset-algolia/dist/umd/index.production.js",
"maxSize": "2.5 kB"
},
{
"path": "packages/autocomplete-plugin-algolia-insights/dist/umd/index.production.js",
"maxSize": "3.25 kB"
"maxSize": "3.5 kB"
},
{
"path": "packages/autocomplete-plugin-redirect-url/dist/umd/index.production.js",
Expand Down
Expand Up @@ -228,6 +228,28 @@ describe('createAlgoliaInsightsPlugin', () => {
]);
});

test('does not call `init` if `insightsInitParams` not passed', () => {
const insightsClient = jest.fn();
createAlgoliaInsightsPlugin({
insightsClient,
});

expect(insightsClient).not.toHaveBeenCalled();
});

test('initializes insights with `insightsInitParams` if passed', () => {
const insightsClient = jest.fn();
createAlgoliaInsightsPlugin({
insightsClient,
insightsInitParams: { userToken: 'user' },
});

expect(insightsClient).toHaveBeenCalledWith('init', {
partial: true,
userToken: 'user',
});
});

describe('automatic pulling', () => {
const consoleError = jest
.spyOn(console, 'error')
Expand Down
Expand Up @@ -20,6 +20,7 @@ import {
AlgoliaInsightsHit,
AutocompleteInsightsApi,
InsightsClient,
InsightsMethodMap,
OnActiveParams,
OnItemsChangeParams,
OnSelectParams,
Expand Down Expand Up @@ -57,6 +58,12 @@ export type CreateAlgoliaInsightsPluginParams = {
* @link https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-algolia-insights/createAlgoliaInsightsPlugin/#param-insightsclient
*/
insightsClient?: InsightsClient;
/**
* Insights parameters to forward to the Insights client’s init method.
*
* @link https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-algolia-insights/createAlgoliaInsightsPlugin/#param-insightsinitparams
*/
insightsInitParams?: Partial<InsightsMethodMap['init'][0]>;
/**
* Hook to send an Insights event when the items change.
*
Expand Down Expand Up @@ -94,6 +101,7 @@ export function createAlgoliaInsightsPlugin(
): AutocompletePlugin<any, undefined> {
const {
insightsClient: providedInsightsClient,
insightsInitParams,
onItemsChange,
onSelect: onSelectEvent,
onActive: onActiveEvent,
Expand Down Expand Up @@ -137,6 +145,10 @@ export function createAlgoliaInsightsPlugin(
return {};
}

if (insightsInitParams) {
insightsClient('init', { partial: true, ...insightsInitParams });
}

const insights = createSearchInsightsApi(insightsClient);
const previousItems = createRef<AlgoliaInsightsHit[]>([]);

Expand Down
@@ -1,5 +1,5 @@
import type {
InsightsMethodMap,
InsightsMethodMap as _InsightsMethodMap,
InsightsClient as _InsightsClient,
} from 'search-insights';

Expand All @@ -11,6 +11,7 @@ export type {
OnUserTokenChange as InsightsOnUserTokenChange,
} from 'search-insights';

export type InsightsMethodMap = _InsightsMethodMap;
export type InsightsClientMethod = keyof InsightsMethodMap;

export type InsightsClientPayload = {
Expand Down

0 comments on commit 186ff9b

Please sign in to comment.