From 888e01e31b18a92401a8595bc0dfc4b2558562e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chalifour?= Date: Mon, 3 May 2021 11:13:36 +0200 Subject: [PATCH] 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 - https://github.com/algolia/autocomplete/discussions/559#discussioncomment-686775 --- bundlesize.config.json | 2 +- .../__tests__/mapToAlgoliaResponse.test.ts | 132 ++++++++++++++++++ .../src/utils/mapToAlgoliaResponse.ts | 19 ++- 3 files changed, 151 insertions(+), 2 deletions(-) diff --git a/bundlesize.config.json b/bundlesize.config.json index 684c570cd..b7660d914 100644 --- a/bundlesize.config.json +++ b/bundlesize.config.json @@ -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", diff --git a/packages/autocomplete-core/src/utils/__tests__/mapToAlgoliaResponse.test.ts b/packages/autocomplete-core/src/utils/__tests__/mapToAlgoliaResponse.test.ts index a638703ec..9b4d492c8 100644 --- a/packages/autocomplete-core/src/utils/__tests__/mapToAlgoliaResponse.test.ts +++ b/packages/autocomplete-core/src/utils/__tests__/mapToAlgoliaResponse.test.ts @@ -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({ diff --git a/packages/autocomplete-core/src/utils/mapToAlgoliaResponse.ts b/packages/autocomplete-core/src/utils/mapToAlgoliaResponse.ts index fc5ad6817..e8892f2e4 100644 --- a/packages/autocomplete-core/src/utils/mapToAlgoliaResponse.ts +++ b/packages/autocomplete-core/src/utils/mapToAlgoliaResponse.ts @@ -4,8 +4,24 @@ import type { } from '@algolia/client-search'; export function mapToAlgoliaResponse( - results: Array | SearchForFacetValuesResponse> + rawResults: Array | SearchForFacetValuesResponse> ) { + const results: Array< + SearchResponse | SearchForFacetValuesResponse + > = rawResults.map((result) => { + return { + ...result, + hits: (result as SearchResponse).hits?.map((hit) => { + // Bring support for the Insights plugin. + return { + ...hit, + __autocomplete_indexName: (result as SearchResponse).index, + __autocomplete_queryID: (result as SearchResponse).queryID, + }; + }), + }; + }); + return { results, hits: results @@ -14,6 +30,7 @@ export function mapToAlgoliaResponse( facetHits: results .map((result) => (result as SearchForFacetValuesResponse).facetHits?.map((facetHit) => { + // Bring support for the highlighting components. return { label: facetHit.value, count: facetHit.count,