Skip to content

Commit c305ab4

Browse files
fix(core): support Insights in requesters (#562)
1 parent a16bea2 commit c305ab4

File tree

3 files changed

+151
-2
lines changed

3 files changed

+151
-2
lines changed

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)