diff --git a/packages/docsearch-react/src/DocSearch.tsx b/packages/docsearch-react/src/DocSearch.tsx index 3cc1c59ad..bb5d3de88 100644 --- a/packages/docsearch-react/src/DocSearch.tsx +++ b/packages/docsearch-react/src/DocSearch.tsx @@ -1,6 +1,5 @@ import type { AutocompleteState, AutocompleteOptions } from '@algolia/autocomplete-core'; -import type { SearchClient } from 'algoliasearch'; -import type { SearchQuery, LiteClient } from 'algoliasearch/lite'; +import type { LiteClient, SearchForHits } from 'algoliasearch/lite'; import React from 'react'; import { createPortal } from 'react-dom'; @@ -16,17 +15,24 @@ export type DocSearchTranslations = Partial<{ modal: ModalTranslations; }>; +// The interface that describes the minimal implementation required for the algoliasearch client, when using the [`transformSearchClient`](https://docsearch.algolia.com/docs/api/#transformsearchclient) option. +export type DocSearchTransformClient = { + search: LiteClient['search']; + addAlgoliaAgent: LiteClient['addAlgoliaAgent']; + transporter: Pick; +}; + export interface DocSearchProps { appId: string; apiKey: string; indexName: string; placeholder?: string; - searchParameters?: SearchQuery; + searchParameters?: SearchForHits; maxResultsPerGroup?: number; transformItems?: (items: DocSearchHit[]) => DocSearchHit[]; hitComponent?: (props: { hit: InternalDocSearchHit | StoredDocSearchHit; children: React.ReactNode }) => JSX.Element; resultsFooterComponent?: (props: { state: AutocompleteState }) => JSX.Element | null; - transformSearchClient?: (searchClient: T) => T; + transformSearchClient?: (searchClient: DocSearchTransformClient) => DocSearchTransformClient; disableUserPersonalization?: boolean; initialQuery?: string; navigator?: AutocompleteOptions['navigator']; diff --git a/packages/docsearch-react/src/useSearchClient.ts b/packages/docsearch-react/src/useSearchClient.ts index b2968dc1f..ae0bbffdf 100644 --- a/packages/docsearch-react/src/useSearchClient.ts +++ b/packages/docsearch-react/src/useSearchClient.ts @@ -1,14 +1,14 @@ import { liteClient } from 'algoliasearch/lite'; -import type { LiteClient } from 'algoliasearch/lite'; import React from 'react'; +import type { DocSearchTransformClient } from './DocSearch'; import { version } from './version'; export function useSearchClient( appId: string, apiKey: string, - transformSearchClient: (searchClient: LiteClient) => LiteClient, -): LiteClient { + transformSearchClient: (searchClient: DocSearchTransformClient) => DocSearchTransformClient, +): DocSearchTransformClient { const searchClient = React.useMemo(() => { const client = liteClient(appId, apiKey); client.addAlgoliaAgent('docsearch', version); diff --git a/packages/website/docs/api.mdx b/packages/website/docs/api.mdx index 05756ef78..09c08fcdc 100644 --- a/packages/website/docs/api.mdx +++ b/packages/website/docs/api.mdx @@ -126,7 +126,7 @@ See the [default implementation][8]. ## `transformSearchClient` -> `type: function` | `default: searchClient => searchClient` | **optional** +> `type: function` | `default: DocSearchTransformClient => DocSearchTransformClient` | **optional** Useful for transforming the [Algolia Search Client][10], for example to [debounce search queries][9]