From 8bcd6806901fdb2a86382b59f3ee23ec3292ef63 Mon Sep 17 00:00:00 2001 From: Dhaya <154633+dhayab@users.noreply.github.com> Date: Thu, 6 Apr 2023 11:04:46 +0200 Subject: [PATCH] feat(preset-algolia): attach algolia credentials on hits (#1117) Co-authored-by: Haroen Viaene --- bundlesize.config.json | 6 +-- .../src/__tests__/requester.test.ts | 14 ++--- .../src/types/AlgoliaInsightsHit.ts | 4 ++ .../__tests__/fetchAlgoliaResults.test.ts | 52 +++++++++++++++++-- .../src/search/fetchAlgoliaResults.ts | 14 ++++- .../utils/__tests__/getAppIdAndApiKey.test.ts | 15 ++++++ .../src/utils/getAppIdAndApiKey.ts | 13 +++++ .../src/utils/index.ts | 1 + test/utils/createSearchClient.ts | 7 ++- 9 files changed, 110 insertions(+), 16 deletions(-) create mode 100644 packages/autocomplete-preset-algolia/src/utils/__tests__/getAppIdAndApiKey.test.ts create mode 100644 packages/autocomplete-preset-algolia/src/utils/getAppIdAndApiKey.ts create mode 100644 packages/autocomplete-preset-algolia/src/utils/index.ts diff --git a/bundlesize.config.json b/bundlesize.config.json index 5faa961ee..a32b04c62 100644 --- a/bundlesize.config.json +++ b/bundlesize.config.json @@ -6,11 +6,11 @@ }, { "path": "packages/autocomplete-js/dist/umd/index.production.js", - "maxSize": "18.75 kB" + "maxSize": "19 kB" }, { "path": "packages/autocomplete-preset-algolia/dist/umd/index.production.js", - "maxSize": "2.25 kB" + "maxSize": "2.5 kB" }, { "path": "packages/autocomplete-plugin-algolia-insights/dist/umd/index.production.js", @@ -26,7 +26,7 @@ }, { "path": "packages/autocomplete-plugin-query-suggestions/dist/umd/index.production.js", - "maxSize": "3.25 kB" + "maxSize": "3.5 kB" }, { "path": "packages/autocomplete-plugin-tags/dist/umd/index.production.js", diff --git a/packages/autocomplete-js/src/__tests__/requester.test.ts b/packages/autocomplete-js/src/__tests__/requester.test.ts index 9670340f2..7ff99be8d 100644 --- a/packages/autocomplete-js/src/__tests__/requester.test.ts +++ b/packages/autocomplete-js/src/__tests__/requester.test.ts @@ -265,7 +265,7 @@ describe('requester', () => { .map((node) => node.textContent) ).toMatchInlineSnapshot(` Array [ - "{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_id\\":0}", + "{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":0}", ] `); @@ -304,7 +304,7 @@ describe('requester', () => { .map((node) => node.textContent) ).toMatchInlineSnapshot(` Array [ - "{\\"objectID\\":\\"7\\",\\"label\\":\\"Hit 7\\",\\"__autocomplete_id\\":4}", + "{\\"objectID\\":\\"7\\",\\"label\\":\\"Hit 7\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":4}", ] `); @@ -316,8 +316,8 @@ describe('requester', () => { .map((node) => node.textContent) ).toMatchInlineSnapshot(` Array [ - "{\\"objectID\\":\\"3\\",\\"label\\":\\"Hit 3\\",\\"__autocomplete_id\\":5}", - "{\\"objectID\\":\\"4\\",\\"label\\":\\"Hit 4\\",\\"__autocomplete_id\\":6}", + "{\\"objectID\\":\\"3\\",\\"label\\":\\"Hit 3\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":5}", + "{\\"objectID\\":\\"4\\",\\"label\\":\\"Hit 4\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":6}", ] `); @@ -331,7 +331,7 @@ describe('requester', () => { .map((node) => node.textContent) ).toMatchInlineSnapshot(` Array [ - "{\\"objectID\\":\\"5\\",\\"label\\":\\"Hit 5\\",\\"__autocomplete_id\\":7}", + "{\\"objectID\\":\\"5\\",\\"label\\":\\"Hit 5\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":7}", ] `); @@ -561,7 +561,7 @@ describe('requester', () => { .map((node) => node.textContent) ).toMatchInlineSnapshot(` Array [ - "{\\"0\\":{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\"},\\"hitsPerPage\\":20,\\"__autocomplete_id\\":0}", + "{\\"0\\":{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"}},\\"hitsPerPage\\":20,\\"__autocomplete_id\\":0}", ] `); @@ -675,7 +675,7 @@ describe('requester', () => { .map((node) => node.textContent) ).toMatchInlineSnapshot(` Array [ - "{\\"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}", + "{\\"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}", ] `); diff --git a/packages/autocomplete-plugin-algolia-insights/src/types/AlgoliaInsightsHit.ts b/packages/autocomplete-plugin-algolia-insights/src/types/AlgoliaInsightsHit.ts index 6b1a4f8e4..fc5fc245b 100644 --- a/packages/autocomplete-plugin-algolia-insights/src/types/AlgoliaInsightsHit.ts +++ b/packages/autocomplete-plugin-algolia-insights/src/types/AlgoliaInsightsHit.ts @@ -2,4 +2,8 @@ export type AlgoliaInsightsHit = { objectID: string; __autocomplete_indexName: string; __autocomplete_queryID: string; + __autocomplete_algoliaCredentials: { + appId: string; + apiKey: string; + }; }; diff --git a/packages/autocomplete-preset-algolia/src/search/__tests__/fetchAlgoliaResults.test.ts b/packages/autocomplete-preset-algolia/src/search/__tests__/fetchAlgoliaResults.test.ts index d1a9d8b3b..7c7316598 100644 --- a/packages/autocomplete-preset-algolia/src/search/__tests__/fetchAlgoliaResults.test.ts +++ b/packages/autocomplete-preset-algolia/src/search/__tests__/fetchAlgoliaResults.test.ts @@ -65,8 +65,30 @@ describe('fetchAlgoliaResults', () => { }, ]); expect(results).toEqual([ - expect.objectContaining({ hits: [{ objectID: '1', label: 'Hit 1' }] }), - expect.objectContaining({ hits: [{ objectID: '2', label: 'Hit 2' }] }), + expect.objectContaining({ + hits: [ + { + objectID: '1', + label: 'Hit 1', + __autocomplete_algoliaCredentials: { + appId: 'algoliaAppId', + apiKey: 'algoliaApiKey', + }, + }, + ], + }), + expect.objectContaining({ + hits: [ + { + objectID: '2', + label: 'Hit 2', + __autocomplete_algoliaCredentials: { + appId: 'algoliaAppId', + apiKey: 'algoliaApiKey', + }, + }, + ], + }), ]); }); @@ -103,8 +125,30 @@ describe('fetchAlgoliaResults', () => { }, ]); expect(results).toEqual([ - expect.objectContaining({ hits: [{ objectID: '1', label: 'Hit 1' }] }), - expect.objectContaining({ hits: [{ objectID: '2', label: 'Hit 2' }] }), + expect.objectContaining({ + hits: [ + { + objectID: '1', + label: 'Hit 1', + __autocomplete_algoliaCredentials: { + appId: 'algoliaAppId', + apiKey: 'algoliaApiKey', + }, + }, + ], + }), + expect.objectContaining({ + hits: [ + { + objectID: '2', + label: 'Hit 2', + __autocomplete_algoliaCredentials: { + appId: 'algoliaAppId', + apiKey: 'algoliaApiKey', + }, + }, + ], + }), ]); }); diff --git a/packages/autocomplete-preset-algolia/src/search/fetchAlgoliaResults.ts b/packages/autocomplete-preset-algolia/src/search/fetchAlgoliaResults.ts index 9cdadd06a..d87cd7028 100644 --- a/packages/autocomplete-preset-algolia/src/search/fetchAlgoliaResults.ts +++ b/packages/autocomplete-preset-algolia/src/search/fetchAlgoliaResults.ts @@ -10,6 +10,7 @@ import type { SearchResponse, SearchClient, } from '../types'; +import { getAppIdAndApiKey } from '../utils'; export interface SearchParams { /** @@ -43,6 +44,8 @@ export function fetchAlgoliaResults({ }); } + const { appId, apiKey } = getAppIdAndApiKey(searchClient); + return searchClient .search( queries.map((searchParameters) => { @@ -60,6 +63,15 @@ export function fetchAlgoliaResults({ }) ) .then((response) => { - return response.results; + return response.results.map((result) => ({ + ...result, + hits: result.hits?.map((hit) => ({ + ...hit, + __autocomplete_algoliaCredentials: { + appId, + apiKey, + }, + })), + })); }); } diff --git a/packages/autocomplete-preset-algolia/src/utils/__tests__/getAppIdAndApiKey.test.ts b/packages/autocomplete-preset-algolia/src/utils/__tests__/getAppIdAndApiKey.test.ts new file mode 100644 index 000000000..d7606a08e --- /dev/null +++ b/packages/autocomplete-preset-algolia/src/utils/__tests__/getAppIdAndApiKey.test.ts @@ -0,0 +1,15 @@ +import algoliasearchV4 from 'algoliasearch'; + +import { getAppIdAndApiKey } from '../getAppIdAndApiKey'; + +const APP_ID = 'myAppId'; +const API_KEY = 'myApiKey'; + +describe('getAppIdAndApiKey', () => { + it('gets appId and apiKey from searchClient', () => { + const searchClient = algoliasearchV4(APP_ID, API_KEY); + const { appId, apiKey } = getAppIdAndApiKey(searchClient); + expect(appId).toEqual(APP_ID); + expect(apiKey).toEqual(API_KEY); + }); +}); diff --git a/packages/autocomplete-preset-algolia/src/utils/getAppIdAndApiKey.ts b/packages/autocomplete-preset-algolia/src/utils/getAppIdAndApiKey.ts new file mode 100644 index 000000000..f7add01f0 --- /dev/null +++ b/packages/autocomplete-preset-algolia/src/utils/getAppIdAndApiKey.ts @@ -0,0 +1,13 @@ +import type { SearchClient } from '../types'; + +export function getAppIdAndApiKey(searchClient: SearchClient): { + appId: string; + apiKey: string; +} { + const { headers, queryParameters } = searchClient.transporter; + const APP_ID = 'x-algolia-application-id'; + const API_KEY = 'x-algolia-api-key'; + const appId = headers[APP_ID] || queryParameters[APP_ID]; + const apiKey = headers[API_KEY] || queryParameters[API_KEY]; + return { appId, apiKey }; +} diff --git a/packages/autocomplete-preset-algolia/src/utils/index.ts b/packages/autocomplete-preset-algolia/src/utils/index.ts new file mode 100644 index 000000000..4ae8e1aac --- /dev/null +++ b/packages/autocomplete-preset-algolia/src/utils/index.ts @@ -0,0 +1 @@ +export * from './getAppIdAndApiKey'; diff --git a/test/utils/createSearchClient.ts b/test/utils/createSearchClient.ts index d2938d464..83f47cbbc 100644 --- a/test/utils/createSearchClient.ts +++ b/test/utils/createSearchClient.ts @@ -15,7 +15,12 @@ export function createSearchClient( addAlgoliaAgent: jest.fn(), clearCache: jest.fn(), initIndex: jest.fn(), - transporter: {} as any, + transporter: { + headers: { + 'x-algolia-application-id': 'algoliaAppId', + 'x-algolia-api-key': 'algoliaApiKey', + }, + } as any, search: jest.fn((requests) => Promise.resolve( createMultiSearchResponse(