Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(insights): allow to pass init params #1230

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bundlesize.config.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
*/
aymeric-giraudet marked this conversation as resolved.
Show resolved Hide resolved
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 });
}
Comment on lines +148 to +150
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a condition that checks we can use the partial init here (isModernInsightsClient()) ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as it's additional, and only when people pass the insights init parameters, I'm not sure if it's needed


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

Expand Down
Original file line number Diff line number Diff line change
@@ -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