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): prevent potential errors #5487

Merged
merged 1 commit into from
Feb 21, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { wait } from '@instantsearch/testutils/wait';
import type { JSDOM } from 'jsdom';
import type { PlainSearchParameters } from 'algoliasearch-helper';
import { fireEvent } from '@testing-library/dom';
import { createInstantSearch } from '../../../test/createInstantSearch';

declare const jsdom: JSDOM;

Expand Down Expand Up @@ -227,15 +228,36 @@ describe('insights', () => {
});

it('throws when search client does not have credentials', () => {
const { insightsClient, instantSearchInstance } = createTestEnvironment({
searchClient: createSearchClient(),
const { insightsClient } = createInsights();
const instantSearchInstance = createInstantSearch({
// @ts-expect-error fake client
client: { search: () => {} },
});
expect(() =>
createInsightsMiddleware({
insightsClient,
})({ instantSearchInstance })
).toThrowErrorMatchingInlineSnapshot(
`"[insights middleware]: could not extract Algolia credentials from searchClient"`
`"apiKey is missing, please provide it so we can authenticate the application"`
);
});

it('warns when search client does not have credentials', () => {
const { insightsClient } = createInsights();
const instantSearchInstance = createInstantSearch({
// @ts-expect-error fake client
client: { search: () => {} },
});
expect(() => {
try {
createInsightsMiddleware({
insightsClient,
})({ instantSearchInstance });
} catch (e) {
// insights error
}
}).toWarnDev(
'[InstantSearch.js]: could not extract Algolia credentials from searchClient in insights middleware.'
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ export function createInsightsMiddleware<
const [appId, apiKey] = getAppIdAndApiKey(instantSearchInstance.client);

// search-insights.js also throws an error so dev-only clarification is sufficient
if (__DEV__ && !(appId && apiKey)) {
throw new Error(
'[insights middleware]: could not extract Algolia credentials from searchClient'
);
}
warning(
Boolean(appId && apiKey),
'could not extract Algolia credentials from searchClient in insights middleware.'
);

let queuedUserToken: string | undefined = undefined;
let userTokenBeforeInit: string | undefined = undefined;
Expand Down Expand Up @@ -122,7 +121,12 @@ export function createInsightsMiddleware<
// Otherwise, the `init` call might override it with anonymous user token.
userTokenBeforeInit = userToken;
});
insightsClient('init', { appId, apiKey, ...insightsInitParams });
insightsClient('init', {
appId,
apiKey,
useCookie: true,
...insightsInitParams,
});

let initialParameters: PlainSearchParameters;
let helper: AlgoliaSearchHelper;
Expand Down Expand Up @@ -196,21 +200,16 @@ export function createInsightsMiddleware<
if (onEvent) {
onEvent(event, _insightsClient as TInsightsClient);
} else if (event.insightsMethod) {
const hasUserToken = Boolean(
(helper.state as PlainSearchParameters).userToken
);
if (hasUserToken) {
insightsClient(event.insightsMethod, event.payload);
} else {
warning(
false,
`
insightsClient(event.insightsMethod, event.payload);

warning(
Boolean((helper.state as PlainSearchParameters).userToken),
`
Cannot send event to Algolia Insights because \`userToken\` is not set.

See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-further/send-insights-events/js/#setting-the-usertoken
`
);
}
);
dhayab marked this conversation as resolved.
Show resolved Hide resolved
} else {
warning(
false,
Expand Down