Skip to content

Commit

Permalink
fix(types): use a minimal type for transformSearchClient (#2348)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Nov 12, 2024
1 parent 57eace5 commit 08d9a37
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
14 changes: 10 additions & 4 deletions packages/docsearch-react/src/DocSearch.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<LiteClient['transporter'], 'algoliaAgent'>;
};

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<InternalDocSearchHit> }) => JSX.Element | null;
transformSearchClient?: <T extends LiteClient | SearchClient>(searchClient: T) => T;
transformSearchClient?: (searchClient: DocSearchTransformClient) => DocSearchTransformClient;
disableUserPersonalization?: boolean;
initialQuery?: string;
navigator?: AutocompleteOptions<InternalDocSearchHit>['navigator'];
Expand Down
6 changes: 3 additions & 3 deletions packages/docsearch-react/src/useSearchClient.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/website/docs/api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit 08d9a37

Please sign in to comment.