From 8144cf3bf37222e07f1e0b068596483e994d5ed8 Mon Sep 17 00:00:00 2001 From: Haroen Viaene Date: Mon, 17 Apr 2023 15:19:49 +0200 Subject: [PATCH] fix(algolia): throw error if searchClient is missing (#1122) * fix(algolia): throw error if searchClient is missing It's a synchronous error, but as this is only for debugging, i don't think it makes a big difference how the error is thrown. * move to invariant / clearer message * no test * not an option --- .../__tests__/getAlgoliaFacets.test.ts | 12 ++++++++++++ .../__tests__/getAlgoliaResults.test.ts | 12 ++++++++++++ .../src/requester/getAlgoliaFacets.ts | 7 +++++++ .../src/requester/getAlgoliaResults.ts | 18 +++++++++++++++--- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/packages/autocomplete-preset-algolia/src/requester/__tests__/getAlgoliaFacets.test.ts b/packages/autocomplete-preset-algolia/src/requester/__tests__/getAlgoliaFacets.test.ts index 50e3d4800..a81cc4a37 100644 --- a/packages/autocomplete-preset-algolia/src/requester/__tests__/getAlgoliaFacets.test.ts +++ b/packages/autocomplete-preset-algolia/src/requester/__tests__/getAlgoliaFacets.test.ts @@ -2,6 +2,18 @@ import { createSearchClient } from '../../../../../test/utils'; import { getAlgoliaFacets } from '../getAlgoliaFacets'; describe('getAlgoliaFacets', () => { + test('throws without search client', () => { + expect(() => + getAlgoliaFacets({ + // @ts-expect-error + searchClient: undefined, + queries: [], + }) + ).toThrowErrorMatchingInlineSnapshot( + `"[Autocomplete] The \`searchClient\` parameter is required for getAlgoliaFacets({ searchClient })."` + ); + }); + test('returns the description', () => { const searchClient = createSearchClient({ search: jest.fn(), diff --git a/packages/autocomplete-preset-algolia/src/requester/__tests__/getAlgoliaResults.test.ts b/packages/autocomplete-preset-algolia/src/requester/__tests__/getAlgoliaResults.test.ts index 501e62f62..4182d70c2 100644 --- a/packages/autocomplete-preset-algolia/src/requester/__tests__/getAlgoliaResults.test.ts +++ b/packages/autocomplete-preset-algolia/src/requester/__tests__/getAlgoliaResults.test.ts @@ -2,6 +2,18 @@ import { createSearchClient } from '../../../../../test/utils'; import { getAlgoliaResults } from '../getAlgoliaResults'; describe('getAlgoliaResults', () => { + test('throws without search client', () => { + expect(() => + getAlgoliaResults({ + // @ts-expect-error + searchClient: undefined, + queries: [], + }) + ).toThrowErrorMatchingInlineSnapshot( + `"[Autocomplete] The \`searchClient\` parameter is required for getAlgoliaResults({ searchClient })."` + ); + }); + test('returns the description', () => { const searchClient = createSearchClient({ search: jest.fn(), diff --git a/packages/autocomplete-preset-algolia/src/requester/getAlgoliaFacets.ts b/packages/autocomplete-preset-algolia/src/requester/getAlgoliaFacets.ts index 9860bf714..2699d20e4 100644 --- a/packages/autocomplete-preset-algolia/src/requester/getAlgoliaFacets.ts +++ b/packages/autocomplete-preset-algolia/src/requester/getAlgoliaFacets.ts @@ -1,3 +1,5 @@ +import { invariant } from '@algolia/autocomplete-shared'; + import type { MultipleQueriesQuery } from '../types'; import { createAlgoliaRequester } from './createAlgoliaRequester'; @@ -7,6 +9,11 @@ import { RequestParams } from './createRequester'; * Retrieves Algolia facet hits from multiple indices. */ export function getAlgoliaFacets(requestParams: RequestParams) { + invariant( + typeof requestParams.searchClient === 'object', + 'The `searchClient` parameter is required for getAlgoliaFacets({ searchClient }).' + ); + const requester = createAlgoliaRequester({ transformResponse: (response) => response.facetHits, }); diff --git a/packages/autocomplete-preset-algolia/src/requester/getAlgoliaResults.ts b/packages/autocomplete-preset-algolia/src/requester/getAlgoliaResults.ts index 7bda56e50..64e2df422 100644 --- a/packages/autocomplete-preset-algolia/src/requester/getAlgoliaResults.ts +++ b/packages/autocomplete-preset-algolia/src/requester/getAlgoliaResults.ts @@ -1,8 +1,20 @@ +import { invariant } from '@algolia/autocomplete-shared'; + import { createAlgoliaRequester } from './createAlgoliaRequester'; +import { RequestParams } from './createRequester'; /** * Retrieves Algolia results from multiple indices. */ -export const getAlgoliaResults = createAlgoliaRequester({ - transformResponse: (response) => response.hits, -}); +export function getAlgoliaResults(requestParams: RequestParams) { + invariant( + typeof requestParams.searchClient === 'object', + 'The `searchClient` parameter is required for getAlgoliaResults({ searchClient }).' + ); + + const requester = createAlgoliaRequester({ + transformResponse: (response) => response.hits, + }); + + return requester(requestParams); +}