Skip to content

Commit

Permalink
fix(insightsMiddleware): throw an error when credentials can't be ext…
Browse files Browse the repository at this point in the history
…racted (#4901)

* fix(insightsMiddleware): throw an error when credentials can't be extracted

When api or appId is missing, an error gets thrown by search insights, but that doesn't explain *why* that error gets thrown.

Ideally this would also clarify what the next step is (usually making sure to spread the previous search client, or leaving all properties in place, but i'm not too sure how to word that.

This doesn't add a new error where there wasn't already one, as the search-insights library threw an error in this case already.

* only dev mode, avoid bundle size bloat

* can't be bothered with the ts
  • Loading branch information
Haroenv authored Sep 10, 2021
1 parent 47cdd81 commit 55313e4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/middlewares/__tests__/createInsightsMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ import { createSearchClient } from '../../../test/mock/createSearchClient';
import { warning } from '../../lib/utils';

describe('insights', () => {
const createTestEnvironment = () => {
const searchClientWithCredentials = createSearchClient({
// @ts-expect-error only available in search client v4
transporter: {
headers: {
'x-algolia-application-id': 'myAppId',
'x-algolia-api-key': 'myApiKey',
},
},
});
const createTestEnvironment = ({
searchClient = searchClientWithCredentials,
} = {}) => {
const { analytics, insightsClient } = createInsights();
const indexName = 'my-index';
const instantSearchInstance = instantsearch({
searchClient: createSearchClient({
// @ts-expect-error only available in search client v4
transporter: {
headers: {
'x-algolia-application-id': 'myAppId',
'x-algolia-api-key': 'myApiKey',
},
},
}),
searchClient,
indexName,
});
instantSearchInstance.start();
Expand Down Expand Up @@ -114,6 +117,19 @@ describe('insights', () => {
});
});

it('throws when search client does not have credentials', () => {
const { insightsClient, instantSearchInstance } = createTestEnvironment({
searchClient: createSearchClient(),
});
expect(() =>
createInsightsMiddleware({
insightsClient,
})({ instantSearchInstance })
).toThrowErrorMatchingInlineSnapshot(
`"[insights middleware]: could not extract Algolia credentials from searchClient"`
);
});

it('does not throw without userToken in UMD with the library loaded after the event', () => {
const {
insightsClient,
Expand Down
8 changes: 8 additions & 0 deletions src/middlewares/createInsightsMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ export const createInsightsMiddleware: CreateInsightsMiddleware = (props) => {

return ({ instantSearchInstance }) => {
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'
);
}

let queuedUserToken: string | undefined = undefined;
let userTokenBeforeInit: string | undefined = undefined;

Expand Down
2 changes: 2 additions & 0 deletions src/widgets/hits/__tests__/hits-integration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const createSearchClient = ({
),
})
),
applicationID: 'latency',
apiKey: '123',
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ describe('infiniteHits', () => {
),
}) as any
),
// credentials are stored like this in client v3, but not part of the SearchClient type
...({ applicationID: 'latency', apiKey: '123' } as any),
});
const search = instantsearch({
indexName: 'instant_search',
Expand Down

0 comments on commit 55313e4

Please sign in to comment.