Skip to content

Commit 8bcd680

Browse files
dhayabHaroenv
andcommitted
feat(preset-algolia): attach algolia credentials on hits (#1117)
Co-authored-by: Haroen Viaene <[email protected]>
1 parent 63dd995 commit 8bcd680

File tree

9 files changed

+110
-16
lines changed

9 files changed

+110
-16
lines changed

bundlesize.config.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
},
77
{
88
"path": "packages/autocomplete-js/dist/umd/index.production.js",
9-
"maxSize": "18.75 kB"
9+
"maxSize": "19 kB"
1010
},
1111
{
1212
"path": "packages/autocomplete-preset-algolia/dist/umd/index.production.js",
13-
"maxSize": "2.25 kB"
13+
"maxSize": "2.5 kB"
1414
},
1515
{
1616
"path": "packages/autocomplete-plugin-algolia-insights/dist/umd/index.production.js",
@@ -26,7 +26,7 @@
2626
},
2727
{
2828
"path": "packages/autocomplete-plugin-query-suggestions/dist/umd/index.production.js",
29-
"maxSize": "3.25 kB"
29+
"maxSize": "3.5 kB"
3030
},
3131
{
3232
"path": "packages/autocomplete-plugin-tags/dist/umd/index.production.js",

packages/autocomplete-js/src/__tests__/requester.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ describe('requester', () => {
265265
.map((node) => node.textContent)
266266
).toMatchInlineSnapshot(`
267267
Array [
268-
"{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_id\\":0}",
268+
"{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":0}",
269269
]
270270
`);
271271

@@ -304,7 +304,7 @@ describe('requester', () => {
304304
.map((node) => node.textContent)
305305
).toMatchInlineSnapshot(`
306306
Array [
307-
"{\\"objectID\\":\\"7\\",\\"label\\":\\"Hit 7\\",\\"__autocomplete_id\\":4}",
307+
"{\\"objectID\\":\\"7\\",\\"label\\":\\"Hit 7\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":4}",
308308
]
309309
`);
310310

@@ -316,8 +316,8 @@ describe('requester', () => {
316316
.map((node) => node.textContent)
317317
).toMatchInlineSnapshot(`
318318
Array [
319-
"{\\"objectID\\":\\"3\\",\\"label\\":\\"Hit 3\\",\\"__autocomplete_id\\":5}",
320-
"{\\"objectID\\":\\"4\\",\\"label\\":\\"Hit 4\\",\\"__autocomplete_id\\":6}",
319+
"{\\"objectID\\":\\"3\\",\\"label\\":\\"Hit 3\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":5}",
320+
"{\\"objectID\\":\\"4\\",\\"label\\":\\"Hit 4\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":6}",
321321
]
322322
`);
323323

@@ -331,7 +331,7 @@ describe('requester', () => {
331331
.map((node) => node.textContent)
332332
).toMatchInlineSnapshot(`
333333
Array [
334-
"{\\"objectID\\":\\"5\\",\\"label\\":\\"Hit 5\\",\\"__autocomplete_id\\":7}",
334+
"{\\"objectID\\":\\"5\\",\\"label\\":\\"Hit 5\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":7}",
335335
]
336336
`);
337337

@@ -561,7 +561,7 @@ describe('requester', () => {
561561
.map((node) => node.textContent)
562562
).toMatchInlineSnapshot(`
563563
Array [
564-
"{\\"0\\":{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\"},\\"hitsPerPage\\":20,\\"__autocomplete_id\\":0}",
564+
"{\\"0\\":{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"}},\\"hitsPerPage\\":20,\\"__autocomplete_id\\":0}",
565565
]
566566
`);
567567

@@ -675,7 +675,7 @@ describe('requester', () => {
675675
.map((node) => node.textContent)
676676
).toMatchInlineSnapshot(`
677677
Array [
678-
"{\\"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}",
678+
"{\\"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}",
679679
]
680680
`);
681681

packages/autocomplete-plugin-algolia-insights/src/types/AlgoliaInsightsHit.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ export type AlgoliaInsightsHit = {
22
objectID: string;
33
__autocomplete_indexName: string;
44
__autocomplete_queryID: string;
5+
__autocomplete_algoliaCredentials: {
6+
appId: string;
7+
apiKey: string;
8+
};
59
};

packages/autocomplete-preset-algolia/src/search/__tests__/fetchAlgoliaResults.test.ts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,30 @@ describe('fetchAlgoliaResults', () => {
6565
},
6666
]);
6767
expect(results).toEqual([
68-
expect.objectContaining({ hits: [{ objectID: '1', label: 'Hit 1' }] }),
69-
expect.objectContaining({ hits: [{ objectID: '2', label: 'Hit 2' }] }),
68+
expect.objectContaining({
69+
hits: [
70+
{
71+
objectID: '1',
72+
label: 'Hit 1',
73+
__autocomplete_algoliaCredentials: {
74+
appId: 'algoliaAppId',
75+
apiKey: 'algoliaApiKey',
76+
},
77+
},
78+
],
79+
}),
80+
expect.objectContaining({
81+
hits: [
82+
{
83+
objectID: '2',
84+
label: 'Hit 2',
85+
__autocomplete_algoliaCredentials: {
86+
appId: 'algoliaAppId',
87+
apiKey: 'algoliaApiKey',
88+
},
89+
},
90+
],
91+
}),
7092
]);
7193
});
7294

@@ -103,8 +125,30 @@ describe('fetchAlgoliaResults', () => {
103125
},
104126
]);
105127
expect(results).toEqual([
106-
expect.objectContaining({ hits: [{ objectID: '1', label: 'Hit 1' }] }),
107-
expect.objectContaining({ hits: [{ objectID: '2', label: 'Hit 2' }] }),
128+
expect.objectContaining({
129+
hits: [
130+
{
131+
objectID: '1',
132+
label: 'Hit 1',
133+
__autocomplete_algoliaCredentials: {
134+
appId: 'algoliaAppId',
135+
apiKey: 'algoliaApiKey',
136+
},
137+
},
138+
],
139+
}),
140+
expect.objectContaining({
141+
hits: [
142+
{
143+
objectID: '2',
144+
label: 'Hit 2',
145+
__autocomplete_algoliaCredentials: {
146+
appId: 'algoliaAppId',
147+
apiKey: 'algoliaApiKey',
148+
},
149+
},
150+
],
151+
}),
108152
]);
109153
});
110154

packages/autocomplete-preset-algolia/src/search/fetchAlgoliaResults.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
SearchResponse,
1111
SearchClient,
1212
} from '../types';
13+
import { getAppIdAndApiKey } from '../utils';
1314

1415
export interface SearchParams {
1516
/**
@@ -43,6 +44,8 @@ export function fetchAlgoliaResults<TRecord>({
4344
});
4445
}
4546

47+
const { appId, apiKey } = getAppIdAndApiKey(searchClient);
48+
4649
return searchClient
4750
.search<TRecord>(
4851
queries.map((searchParameters) => {
@@ -60,6 +63,15 @@ export function fetchAlgoliaResults<TRecord>({
6063
})
6164
)
6265
.then((response) => {
63-
return response.results;
66+
return response.results.map((result) => ({
67+
...result,
68+
hits: result.hits?.map((hit) => ({
69+
...hit,
70+
__autocomplete_algoliaCredentials: {
71+
appId,
72+
apiKey,
73+
},
74+
})),
75+
}));
6476
});
6577
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import algoliasearchV4 from 'algoliasearch';
2+
3+
import { getAppIdAndApiKey } from '../getAppIdAndApiKey';
4+
5+
const APP_ID = 'myAppId';
6+
const API_KEY = 'myApiKey';
7+
8+
describe('getAppIdAndApiKey', () => {
9+
it('gets appId and apiKey from searchClient', () => {
10+
const searchClient = algoliasearchV4(APP_ID, API_KEY);
11+
const { appId, apiKey } = getAppIdAndApiKey(searchClient);
12+
expect(appId).toEqual(APP_ID);
13+
expect(apiKey).toEqual(API_KEY);
14+
});
15+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { SearchClient } from '../types';
2+
3+
export function getAppIdAndApiKey(searchClient: SearchClient): {
4+
appId: string;
5+
apiKey: string;
6+
} {
7+
const { headers, queryParameters } = searchClient.transporter;
8+
const APP_ID = 'x-algolia-application-id';
9+
const API_KEY = 'x-algolia-api-key';
10+
const appId = headers[APP_ID] || queryParameters[APP_ID];
11+
const apiKey = headers[API_KEY] || queryParameters[API_KEY];
12+
return { appId, apiKey };
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './getAppIdAndApiKey';

test/utils/createSearchClient.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ export function createSearchClient(
1515
addAlgoliaAgent: jest.fn(),
1616
clearCache: jest.fn(),
1717
initIndex: jest.fn(),
18-
transporter: {} as any,
18+
transporter: {
19+
headers: {
20+
'x-algolia-application-id': 'algoliaAppId',
21+
'x-algolia-api-key': 'algoliaApiKey',
22+
},
23+
} as any,
1924
search: jest.fn((requests) =>
2025
Promise.resolve(
2126
createMultiSearchResponse(

0 commit comments

Comments
 (0)