Skip to content

Commit

Permalink
fix(algolia): throw error if searchClient is missing (#1122)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Haroenv authored Apr 17, 2023
1 parent 30af95a commit 8144cf3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { invariant } from '@algolia/autocomplete-shared';

import type { MultipleQueriesQuery } from '../types';

import { createAlgoliaRequester } from './createAlgoliaRequester';
Expand All @@ -7,6 +9,11 @@ import { RequestParams } from './createRequester';
* Retrieves Algolia facet hits from multiple indices.
*/
export function getAlgoliaFacets<TTHit>(requestParams: RequestParams<TTHit>) {
invariant(
typeof requestParams.searchClient === 'object',
'The `searchClient` parameter is required for getAlgoliaFacets({ searchClient }).'
);

const requester = createAlgoliaRequester({
transformResponse: (response) => response.facetHits,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TTHit>(requestParams: RequestParams<TTHit>) {
invariant(
typeof requestParams.searchClient === 'object',
'The `searchClient` parameter is required for getAlgoliaResults({ searchClient }).'
);

const requester = createAlgoliaRequester({
transformResponse: (response) => response.hits,
});

return requester(requestParams);
}

0 comments on commit 8144cf3

Please sign in to comment.