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(preset-algolia): attach algolia credentials on hits #1117

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
},
{
"path": "packages/autocomplete-js/dist/umd/index.production.js",
"maxSize": "18 kB"
"maxSize": "18.5 kB"
},
{
"path": "packages/autocomplete-preset-algolia/dist/umd/index.production.js",
"maxSize": "2.25 kB"
"maxSize": "2.5 kB"
},
{
"path": "packages/autocomplete-plugin-algolia-insights/dist/umd/index.production.js",
Expand All @@ -26,7 +26,7 @@
},
{
"path": "packages/autocomplete-plugin-query-suggestions/dist/umd/index.production.js",
"maxSize": "3 kB"
"maxSize": "3.25 kB"
},
{
"path": "packages/autocomplete-plugin-tags/dist/umd/index.production.js",
Expand Down
14 changes: 7 additions & 7 deletions packages/autocomplete-js/src/__tests__/requester.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ describe('requester', () => {
.map((node) => node.textContent)
).toMatchInlineSnapshot(`
Array [
"{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_id\\":0}",
"{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_algoliaResultsMetadata\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":0}",
]
`);

Expand Down Expand Up @@ -304,7 +304,7 @@ describe('requester', () => {
.map((node) => node.textContent)
).toMatchInlineSnapshot(`
Array [
"{\\"objectID\\":\\"7\\",\\"label\\":\\"Hit 7\\",\\"__autocomplete_id\\":4}",
"{\\"objectID\\":\\"7\\",\\"label\\":\\"Hit 7\\",\\"__autocomplete_algoliaResultsMetadata\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":4}",
]
`);

Expand All @@ -316,8 +316,8 @@ describe('requester', () => {
.map((node) => node.textContent)
).toMatchInlineSnapshot(`
Array [
"{\\"objectID\\":\\"3\\",\\"label\\":\\"Hit 3\\",\\"__autocomplete_id\\":5}",
"{\\"objectID\\":\\"4\\",\\"label\\":\\"Hit 4\\",\\"__autocomplete_id\\":6}",
"{\\"objectID\\":\\"3\\",\\"label\\":\\"Hit 3\\",\\"__autocomplete_algoliaResultsMetadata\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":5}",
"{\\"objectID\\":\\"4\\",\\"label\\":\\"Hit 4\\",\\"__autocomplete_algoliaResultsMetadata\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":6}",
]
`);

Expand All @@ -331,7 +331,7 @@ describe('requester', () => {
.map((node) => node.textContent)
).toMatchInlineSnapshot(`
Array [
"{\\"objectID\\":\\"5\\",\\"label\\":\\"Hit 5\\",\\"__autocomplete_id\\":7}",
"{\\"objectID\\":\\"5\\",\\"label\\":\\"Hit 5\\",\\"__autocomplete_algoliaResultsMetadata\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":7}",
]
`);

Expand Down Expand Up @@ -561,7 +561,7 @@ describe('requester', () => {
.map((node) => node.textContent)
).toMatchInlineSnapshot(`
Array [
"{\\"0\\":{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\"},\\"hitsPerPage\\":20,\\"__autocomplete_id\\":0}",
"{\\"0\\":{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_algoliaResultsMetadata\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"}},\\"hitsPerPage\\":20,\\"__autocomplete_id\\":0}",
]
`);

Expand Down Expand Up @@ -675,7 +675,7 @@ describe('requester', () => {
.map((node) => node.textContent)
).toMatchInlineSnapshot(`
Array [
"{\\"0\\":{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\"},\\"results\\":[{\\"page\\":0,\\"hitsPerPage\\":20,\\"nbHits\\":1,\\"nbPages\\":1,\\"processingTimeMS\\":0,\\"hits\\":[{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\"}],\\"query\\":\\"\\",\\"params\\":\\"\\",\\"exhaustiveNbHits\\":true,\\"exhaustiveFacetsCount\\":true}],\\"facetHits\\":[],\\"__autocomplete_id\\":0}",
"{\\"0\\":{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_algoliaResultsMetadata\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"}},\\"results\\":[{\\"page\\":0,\\"hitsPerPage\\":20,\\"nbHits\\":1,\\"nbPages\\":1,\\"processingTimeMS\\":0,\\"hits\\":[{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_algoliaResultsMetadata\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"}}],\\"query\\":\\"\\",\\"params\\":\\"\\",\\"exhaustiveNbHits\\":true,\\"exhaustiveFacetsCount\\":true}],\\"facetHits\\":[],\\"__autocomplete_id\\":0}",
]
`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ export type AlgoliaInsightsHit = {
objectID: string;
__autocomplete_indexName: string;
__autocomplete_queryID: string;
__autocomplete_algoliaResultsMetadata: {
dhayab marked this conversation as resolved.
Show resolved Hide resolved
appId: string;
apiKey: string;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,30 @@ describe('fetchAlgoliaResults', () => {
},
]);
expect(results).toEqual([
expect.objectContaining({ hits: [{ objectID: '1', label: 'Hit 1' }] }),
expect.objectContaining({ hits: [{ objectID: '2', label: 'Hit 2' }] }),
expect.objectContaining({
hits: [
{
objectID: '1',
label: 'Hit 1',
__autocomplete_algoliaResultsMetadata: {
appId: 'algoliaAppId',
apiKey: 'algoliaApiKey',
},
},
],
}),
expect.objectContaining({
hits: [
{
objectID: '2',
label: 'Hit 2',
__autocomplete_algoliaResultsMetadata: {
appId: 'algoliaAppId',
apiKey: 'algoliaApiKey',
},
},
],
}),
]);
});

Expand Down Expand Up @@ -103,8 +125,30 @@ describe('fetchAlgoliaResults', () => {
},
]);
expect(results).toEqual([
expect.objectContaining({ hits: [{ objectID: '1', label: 'Hit 1' }] }),
expect.objectContaining({ hits: [{ objectID: '2', label: 'Hit 2' }] }),
expect.objectContaining({
hits: [
{
objectID: '1',
label: 'Hit 1',
__autocomplete_algoliaResultsMetadata: {
appId: 'algoliaAppId',
apiKey: 'algoliaApiKey',
},
},
],
}),
expect.objectContaining({
hits: [
{
objectID: '2',
label: 'Hit 2',
__autocomplete_algoliaResultsMetadata: {
appId: 'algoliaAppId',
apiKey: 'algoliaApiKey',
},
},
],
}),
]);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
SearchResponse,
SearchClient,
} from '../types';
import { getAppIdAndApiKey } from '../utils';

export interface SearchParams {
/**
Expand Down Expand Up @@ -43,6 +44,8 @@ export function fetchAlgoliaResults<TRecord>({
});
}

const { appId, apiKey } = getAppIdAndApiKey(searchClient);

return searchClient
.search<TRecord>(
queries.map((searchParameters) => {
Expand All @@ -60,6 +63,15 @@ export function fetchAlgoliaResults<TRecord>({
})
)
.then((response) => {
return response.results;
return response.results.map((result) => ({
...result,
hits: result.hits?.map((hit) => ({
...hit,
__autocomplete_algoliaResultsMetadata: {
appId,
apiKey,
},
})),
}));
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import algoliasearchV4 from 'algoliasearch';

import { getAppIdAndApiKey } from '../getAppIdAndApiKey';

const APP_ID = 'myAppId';
const API_KEY = 'myApiKey';

describe('getAppIdAndApiKey', () => {
it('gets appId and apiKey from searchClient', () => {
const searchClient = algoliasearchV4(APP_ID, API_KEY);
const { appId, apiKey } = getAppIdAndApiKey(searchClient);
expect(appId).toEqual(APP_ID);
expect(apiKey).toEqual(API_KEY);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { SearchClient } from '../types';

export function getAppIdAndApiKey(
searchClient: SearchClient
): { appId: string; apiKey: string } {
const { headers, queryParameters } = searchClient.transporter;

Choose a reason for hiding this comment

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

Hi @dhayab, I am not sure if we are using autocomplete incorrectly, but we ran into an issue where the transporter wasn't checked and the JS code was trying to pull headers from undefined. Could you help with this? We have a fix in a place where we are using version 1.8.3 of autocomplete-preset-algolia. We could be using it wrong, I am not sure.

https://github.com/algolia/algoliasearch-shopify/pull/1520

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi Bryan, this is probably because you have the v3 of algoliasearch in your dependencies. Autocomplete relies on the v4, which is what causes the issue right now.

If you're not using the javascript search client directly, you can safely update it to the latest v4 version as InstantSearch and Autocomplete have support for it.

Choose a reason for hiding this comment

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

Ahhh wonderful thank you!

const APP_ID = 'x-algolia-application-id';
const API_KEY = 'x-algolia-api-key';
const appId = headers[APP_ID] || queryParameters[APP_ID];
const apiKey = headers[API_KEY] || queryParameters[API_KEY];
return { appId, apiKey };
}
1 change: 1 addition & 0 deletions packages/autocomplete-preset-algolia/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './getAppIdAndApiKey';
7 changes: 6 additions & 1 deletion test/utils/createSearchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ export function createSearchClient(
addAlgoliaAgent: jest.fn(),
clearCache: jest.fn(),
initIndex: jest.fn(),
transporter: {} as any,
transporter: {
headers: {
'x-algolia-application-id': 'algoliaAppId',
'x-algolia-api-key': 'algoliaApiKey',
},
} as any,
search: jest.fn((requests) =>
Promise.resolve(
createMultiSearchResponse(
Expand Down