Skip to content

Commit 888e01e

Browse files
fix(core): support Insights in requesters
## Description In #540, we introduced the Requester API to batch Algolia queries, but we [left out Insights plugin support](https://github.com/algolia/autocomplete/blob/c62f1d2757c5824c5a2f456c20cf55ca3d962cdb/packages/autocomplete-preset-algolia/src/search/getAlgoliaHits.ts#L18-L19). These metadata fields are required for the Insights plugin to send events to the API. This PR brings support for the Insights plugin from requesters by adding back the necessary metadata in the hits returned by the Algolia API. ## Related - #559 (reply in thread)
1 parent ca134e7 commit 888e01e

3 files changed

Lines changed: 151 additions & 2 deletions

File tree

bundlesize.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"files": [
33
{
44
"path": "packages/autocomplete-core/dist/umd/index.production.js",
5-
"maxSize": "5 kB"
5+
"maxSize": "5.25 kB"
66
},
77
{
88
"path": "packages/autocomplete-js/dist/umd/index.production.js",

packages/autocomplete-core/src/utils/__tests__/mapToAlgoliaResponse.test.ts

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,138 @@ describe('mapToAlgoliaResponse', () => {
4040
expect(facetHits).toEqual([]);
4141
});
4242

43+
test('returns formatted results', () => {
44+
const { results } = mapToAlgoliaResponse([
45+
createSingleSearchResponse({
46+
index: 'indexName',
47+
queryID: 'queryID',
48+
hits: [
49+
{
50+
objectID: '1',
51+
label: 'Label 1',
52+
},
53+
{
54+
objectID: '2',
55+
label: 'Label 2',
56+
},
57+
],
58+
}),
59+
createSingleSearchResponse({
60+
index: 'indexName',
61+
queryID: 'queryID',
62+
hits: [
63+
{
64+
objectID: '3',
65+
label: 'Label 3',
66+
},
67+
{
68+
objectID: '4',
69+
label: 'Label 4',
70+
},
71+
],
72+
}),
73+
]);
74+
75+
expect(results).toEqual([
76+
expect.objectContaining({
77+
hits: [
78+
{
79+
__autocomplete_indexName: 'indexName',
80+
__autocomplete_queryID: 'queryID',
81+
label: 'Label 1',
82+
objectID: '1',
83+
},
84+
{
85+
__autocomplete_indexName: 'indexName',
86+
__autocomplete_queryID: 'queryID',
87+
label: 'Label 2',
88+
objectID: '2',
89+
},
90+
],
91+
}),
92+
expect.objectContaining({
93+
hits: [
94+
{
95+
__autocomplete_indexName: 'indexName',
96+
__autocomplete_queryID: 'queryID',
97+
label: 'Label 3',
98+
objectID: '3',
99+
},
100+
{
101+
__autocomplete_indexName: 'indexName',
102+
__autocomplete_queryID: 'queryID',
103+
label: 'Label 4',
104+
objectID: '4',
105+
},
106+
],
107+
}),
108+
]);
109+
});
110+
111+
test('returns formatted hits', () => {
112+
const { hits } = mapToAlgoliaResponse([
113+
createSingleSearchResponse({
114+
index: 'indexName',
115+
queryID: 'queryID',
116+
hits: [
117+
{
118+
objectID: '1',
119+
label: 'Label 1',
120+
},
121+
{
122+
objectID: '2',
123+
label: 'Label 2',
124+
},
125+
],
126+
}),
127+
createSingleSearchResponse({
128+
index: 'indexName',
129+
queryID: 'queryID',
130+
hits: [
131+
{
132+
objectID: '3',
133+
label: 'Label 3',
134+
},
135+
{
136+
objectID: '4',
137+
label: 'Label 4',
138+
},
139+
],
140+
}),
141+
]);
142+
143+
expect(hits).toEqual([
144+
[
145+
{
146+
__autocomplete_indexName: 'indexName',
147+
__autocomplete_queryID: 'queryID',
148+
label: 'Label 1',
149+
objectID: '1',
150+
},
151+
{
152+
__autocomplete_indexName: 'indexName',
153+
__autocomplete_queryID: 'queryID',
154+
label: 'Label 2',
155+
objectID: '2',
156+
},
157+
],
158+
[
159+
{
160+
__autocomplete_indexName: 'indexName',
161+
__autocomplete_queryID: 'queryID',
162+
label: 'Label 3',
163+
objectID: '3',
164+
},
165+
{
166+
__autocomplete_indexName: 'indexName',
167+
__autocomplete_queryID: 'queryID',
168+
label: 'Label 4',
169+
objectID: '4',
170+
},
171+
],
172+
]);
173+
});
174+
43175
test('returns formatted facet hits', () => {
44176
const { facetHits } = mapToAlgoliaResponse([
45177
createSFFVResponse({

packages/autocomplete-core/src/utils/mapToAlgoliaResponse.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,24 @@ import type {
44
} from '@algolia/client-search';
55

66
export function mapToAlgoliaResponse<THit>(
7-
results: Array<SearchResponse<THit> | SearchForFacetValuesResponse>
7+
rawResults: Array<SearchResponse<THit> | SearchForFacetValuesResponse>
88
) {
9+
const results: Array<
10+
SearchResponse<THit> | SearchForFacetValuesResponse
11+
> = rawResults.map((result) => {
12+
return {
13+
...result,
14+
hits: (result as SearchResponse<THit>).hits?.map((hit) => {
15+
// Bring support for the Insights plugin.
16+
return {
17+
...hit,
18+
__autocomplete_indexName: (result as SearchResponse<THit>).index,
19+
__autocomplete_queryID: (result as SearchResponse<THit>).queryID,
20+
};
21+
}),
22+
};
23+
});
24+
925
return {
1026
results,
1127
hits: results
@@ -14,6 +30,7 @@ export function mapToAlgoliaResponse<THit>(
1430
facetHits: results
1531
.map((result) =>
1632
(result as SearchForFacetValuesResponse).facetHits?.map((facetHit) => {
33+
// Bring support for the highlighting components.
1734
return {
1835
label: facetHit.value,
1936
count: facetHit.count,

0 commit comments

Comments
 (0)