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

fix(core): support Insights in requesters #562

Merged
merged 1 commit into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"files": [
{
"path": "packages/autocomplete-core/dist/umd/index.production.js",
"maxSize": "5 kB"
"maxSize": "5.25 kB"
},
{
"path": "packages/autocomplete-js/dist/umd/index.production.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,138 @@ describe('mapToAlgoliaResponse', () => {
expect(facetHits).toEqual([]);
});

test('returns formatted results', () => {
const { results } = mapToAlgoliaResponse([
createSingleSearchResponse({
index: 'indexName',
queryID: 'queryID',
hits: [
{
objectID: '1',
label: 'Label 1',
},
{
objectID: '2',
label: 'Label 2',
},
],
}),
createSingleSearchResponse({
index: 'indexName',
queryID: 'queryID',
hits: [
{
objectID: '3',
label: 'Label 3',
},
{
objectID: '4',
label: 'Label 4',
},
],
}),
]);

expect(results).toEqual([
expect.objectContaining({
hits: [
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 1',
objectID: '1',
},
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 2',
objectID: '2',
},
],
}),
expect.objectContaining({
hits: [
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 3',
objectID: '3',
},
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 4',
objectID: '4',
},
],
}),
]);
});

test('returns formatted hits', () => {
const { hits } = mapToAlgoliaResponse([
createSingleSearchResponse({
index: 'indexName',
queryID: 'queryID',
hits: [
{
objectID: '1',
label: 'Label 1',
},
{
objectID: '2',
label: 'Label 2',
},
],
}),
createSingleSearchResponse({
index: 'indexName',
queryID: 'queryID',
hits: [
{
objectID: '3',
label: 'Label 3',
},
{
objectID: '4',
label: 'Label 4',
},
],
}),
]);

expect(hits).toEqual([
[
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 1',
objectID: '1',
},
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 2',
objectID: '2',
},
],
[
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 3',
objectID: '3',
},
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 4',
objectID: '4',
},
],
]);
});

test('returns formatted facet hits', () => {
const { facetHits } = mapToAlgoliaResponse([
createSFFVResponse({
Expand Down
19 changes: 18 additions & 1 deletion packages/autocomplete-core/src/utils/mapToAlgoliaResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,24 @@ import type {
} from '@algolia/client-search';

export function mapToAlgoliaResponse<THit>(
results: Array<SearchResponse<THit> | SearchForFacetValuesResponse>
rawResults: Array<SearchResponse<THit> | SearchForFacetValuesResponse>
) {
const results: Array<
SearchResponse<THit> | SearchForFacetValuesResponse
> = rawResults.map((result) => {
return {
...result,
hits: (result as SearchResponse<THit>).hits?.map((hit) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see a check for sffv response here, are you relying on it being undefined?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes—we don't transform hits if it's not defined.

We don't support Insights for facet hits.

// Bring support for the Insights plugin.
return {
...hit,
__autocomplete_indexName: (result as SearchResponse<THit>).index,
__autocomplete_queryID: (result as SearchResponse<THit>).queryID,
};
}),
};
});

return {
results,
hits: results
Expand All @@ -14,6 +30,7 @@ export function mapToAlgoliaResponse<THit>(
facetHits: results
.map((result) =>
(result as SearchForFacetValuesResponse).facetHits?.map((facetHit) => {
// Bring support for the highlighting components.
return {
label: facetHit.value,
count: facetHit.count,
Expand Down