From 579ef909fb1aabb460091fc4665d3aeeb1abde99 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Tue, 12 Nov 2024 15:22:48 +0100 Subject: [PATCH 1/2] fix(types): use a minimal type for transformSearchClient --- packages/docsearch-react/src/DocSearch.tsx | 13 +++++++++---- packages/docsearch-react/src/useSearchClient.ts | 6 +++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/docsearch-react/src/DocSearch.tsx b/packages/docsearch-react/src/DocSearch.tsx index 3cc1c59ad..b8aad36a8 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,23 @@ export type DocSearchTranslations = Partial<{ modal: ModalTranslations; }>; +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); From ce960937be39341739bfae95be62a7cbd63b4572 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Tue, 12 Nov 2024 15:55:18 +0100 Subject: [PATCH 2/2] review --- packages/docsearch-react/src/DocSearch.tsx | 1 + packages/website/docs/api.mdx | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/docsearch-react/src/DocSearch.tsx b/packages/docsearch-react/src/DocSearch.tsx index b8aad36a8..bb5d3de88 100644 --- a/packages/docsearch-react/src/DocSearch.tsx +++ b/packages/docsearch-react/src/DocSearch.tsx @@ -15,6 +15,7 @@ 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']; 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]