Skip to content

Commit

Permalink
feat(preset-algolia): attach algolia credentials on hits (#1117)
Browse files Browse the repository at this point in the history
Co-authored-by: Haroen Viaene <hello@haroen.me>
  • Loading branch information
dhayab and Haroenv committed Apr 24, 2023
1 parent 63dd995 commit 8bcd680
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 16 deletions.
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.75 kB"
"maxSize": "19 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.25 kB"
"maxSize": "3.5 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_algoliaCredentials\\":{\\"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_algoliaCredentials\\":{\\"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_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":5}",
"{\\"objectID\\":\\"4\\",\\"label\\":\\"Hit 4\\",\\"__autocomplete_algoliaCredentials\\":{\\"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_algoliaCredentials\\":{\\"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_algoliaCredentials\\":{\\"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_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"}},\\"results\\":[{\\"page\\":0,\\"hitsPerPage\\":20,\\"nbHits\\":1,\\"nbPages\\":1,\\"processingTimeMS\\":0,\\"hits\\":[{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_algoliaCredentials\\":{\\"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_algoliaCredentials: {
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_algoliaCredentials: {
appId: 'algoliaAppId',
apiKey: 'algoliaApiKey',
},
},
],
}),
expect.objectContaining({
hits: [
{
objectID: '2',
label: 'Hit 2',
__autocomplete_algoliaCredentials: {
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_algoliaCredentials: {
appId: 'algoliaAppId',
apiKey: 'algoliaApiKey',
},
},
],
}),
expect.objectContaining({
hits: [
{
objectID: '2',
label: 'Hit 2',
__autocomplete_algoliaCredentials: {
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_algoliaCredentials: {
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,13 @@
import type { SearchClient } from '../types';

export function getAppIdAndApiKey(searchClient: SearchClient): {
appId: string;
apiKey: string;
} {
const { headers, queryParameters } = searchClient.transporter;
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

0 comments on commit 8bcd680

Please sign in to comment.