Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

feat(types): support algoliasearch v5 #910

Merged
merged 16 commits into from
Jun 24, 2022
67 changes: 20 additions & 47 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,10 @@
// Note: Below, we will be importing both algoliasearch
// `v3` and algoliasearch `v4` types. The goal is being
// able to export the algoliasearch-helper types using
// the developer installed version of the client.

import algoliasearch, {
// @ts-ignore
SearchClient as SearchClientV4,
// @ts-ignore
Client as SearchClientV3,
// @ts-ignore
QueryParameters as SearchOptionsV3,
// @ts-ignore
Response as SearchResponseV3,
} from 'algoliasearch';
import {
SearchOptions as SearchOptionsV4,
SearchResponse as SearchResponseV4,
FindAnswersResponse
// @ts-ignore
} from '@algolia/client-search';
import EventEmitter from '@algolia/events';

type DummySearchClientV4 = {
transporter: any;
};

type Client = ReturnType<typeof algoliasearch> extends DummySearchClientV4
? SearchClientV4
: SearchClientV3;
type SearchOptions = ReturnType<
typeof algoliasearch
> extends DummySearchClientV4
? SearchOptionsV4
: SearchOptionsV3;
type SearchResponse<T> = ReturnType<
typeof algoliasearch
> extends DummySearchClientV4
? SearchResponseV4<T>
: SearchResponseV3<T>;

type SearchClient = Pick<Client, 'search' | 'searchForFacetValues'>;
import {
FindAnswersResponse,
SearchClient,
SearchOptions,
SearchResponse,
} from './types/algoliasearch';

/**
* The algoliasearchHelper module is the function that will let its
Expand Down Expand Up @@ -272,7 +237,11 @@ declare namespace algoliasearchHelper {
*/
addDisjunctiveRefine(facet: string, value: string): this;
addHierarchicalFacetRefinement(facet: string, path: string): this;
addNumericRefinement(facet: string, operator?: SearchParameters.Operator, value?: number | number[]): this;
addNumericRefinement(
facet: string,
operator?: SearchParameters.Operator,
value?: number | number[]
): this;
addFacetRefinement(facet: string, value: string): this;
/**
* @deprecated since version 2.4.0, see {@link AlgoliaSearchHelper#addFacetRefinement}
Expand All @@ -284,7 +253,11 @@ declare namespace algoliasearchHelper {
*/
addExclude: AlgoliaSearchHelper['addFacetExclusion'];
addTag(tag: string): this;
removeNumericRefinement(facet: string, operator?: SearchParameters.Operator, value?: number | number[]): this;
removeNumericRefinement(
facet: string,
operator?: SearchParameters.Operator,
value?: number | number[]
): this;
removeDisjunctiveFacetRefinement(facet: string, value?: string): this;
/**
* @deprecated since version 2.4.0, see {@link AlgoliaSearchHelper#removeDisjunctiveFacetRefinement}
Expand Down Expand Up @@ -530,7 +503,7 @@ declare namespace algoliasearchHelper {
'facetsRefinements',
'hierarchicalFacets',
'facetsExcludes',

'disjunctiveFacetsRefinements',
'numericRefinements',
'tagRefinements',
Expand Down Expand Up @@ -950,7 +923,7 @@ declare namespace algoliasearchHelper {
queryType?: 'prefixAll' | 'prefixLast' | 'prefixNone';
/**
* Search entries inside a given area defined by a set of points
* defauly: ''
* default: ''
* https://www.algolia.com/doc/api-reference/api-parameters/insidePolygon/
*/
insidePolygon?: number[][];
Expand Down Expand Up @@ -1150,7 +1123,7 @@ declare namespace algoliasearchHelper {
* Marker which can be added to search results to identify them as created without a search response.
* This is for internal use, e.g., avoiding caching in infinite hits, or delaying the display of these results.
*/
__isArtificial?: boolean | undefined
__isArtificial?: boolean | undefined;
}

type ISearchResponse<T> = Omit<SearchResponse<T>, 'facets' | 'params'> &
Expand Down Expand Up @@ -1392,7 +1365,7 @@ declare namespace algoliasearchHelper {

/**
* Returns all refinements for all filters + tags. It also provides
* additional information: count and exhausistivity for each filter.
* additional information: count and exhaustiveness for each filter.
*
* See the [refinement type](#Refinement) for an exhaustive view of the available
* data.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"@algolia/events": "^4.0.1"
},
"peerDependencies": {
"algoliasearch": ">= 3.1 < 5"
"algoliasearch": ">= 3.1 < 6"
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
},
"resolutions": {
"node-sass": "4.14.1"
Expand Down
124 changes: 124 additions & 0 deletions types/algoliasearch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Note: Below, we will be importing all algoliasearch v3,4,5 types.
// The goal is being able to export the algoliasearch-helper types using
// the version of the client installed by the developer.

// @ts-ignore
import type algoliasearch from 'algoliasearch/lite';
// @ts-ignore
import type * as AlgoliaSearchLite from 'algoliasearch/lite';
// @ts-ignore
import type * as ClientSearch from '@algolia/client-search';
// @ts-ignore
import type * as AlgoliaSearchLiteV5 from '@experimental-api-clients-automation/algoliasearch-lite';
// @ts-ignore
import type * as AlgoliaSearchFullV5 from '@experimental-api-clients-automation/algoliasearch';
// @ts-ignore
import * as ClientSearchV5 from '@experimental-api-clients-automation/client-search';
Haroenv marked this conversation as resolved.
Show resolved Hide resolved

type DummySearchClientV4 = {
transporter: any;
};

type DummySearchClientV5 = {
search: any;
};

// @ts-ignore
type ClientLiteV5 = ReturnType<
// @ts-ignore
typeof AlgoliaSearchLiteV5.algoliasearchLiteClient
>;
// @ts-ignore
type ClientFullV5 = ReturnType<typeof AlgoliaSearchFullV5.algoliasearch>;

type PickForClient<
T extends {
v3: unknown;
v4: unknown;
v5lite: unknown;
v5full: unknown;
}
> = ClientLiteV5 extends DummySearchClientV5
? T['v5lite']
: ClientFullV5 extends DummySearchClientV5
? T['v5full']
: ReturnType<typeof algoliasearch> extends DummySearchClientV4
? T['v4']
: T['v3'];

type DefaultSearchClient = PickForClient<{
// @ts-ignore
v3: ReturnType<typeof algoliasearch>;
// @ts-ignore
v4: ReturnType<typeof algoliasearch>;
// @ts-ignore
v5lite: ClientLiteV5;
// @ts-ignore
v5full: ClientFullV5;
}>;

export type SearchOptions = PickForClient<{
// @ts-ignore
v3: AlgoliaSearchLite.QueryParameters;
// @ts-ignore
v4: ClientSearch.SearchOptions;
v5lite: NonNullable<
// @ts-ignore
ClientSearchV5.LegacySearchMethodProps[number]['params']
>;
v5full: NonNullable<
// @ts-ignore
ClientSearchV5.LegacySearchMethodProps[number]['params']
>;
}>;

export type SearchResponse<T> = PickForClient<{
// @ts-ignore
v3: AlgoliaSearchLite.Response<T>;
// @ts-ignore
v4: ClientSearch.SearchResponse<T>;
// @ts-ignore
v5lite: ClientSearchV5.SearchResponse; // TODO: should be generic
// @ts-ignore
v5full: ClientSearchV5.SearchResponse;
}>;

export type SearchForFacetValuesResponse = PickForClient<{
// @ts-ignore
v3: AlgoliaSearchLite.SearchForFacetValues.Response;
// @ts-ignore
v4: ClientSearch.SearchForFacetValuesResponse;
// @ts-ignore
v5lite: ClientSearchV5.SearchForFacetValuesResponse;
// @ts-ignore
v5full: ClientSearchV5.SearchForFacetValuesResponse;
}>;

export type FindAnswersOptions = PickForClient<{
v3: any;
// @ts-ignore
v4: ClientSearch.FindAnswersOptions;
v5lite: any;
v5full: any;
}>;

export type FindAnswersResponse<T> = PickForClient<{
v3: any;
// @ts-ignore
v4: ClientSearch.FindAnswersResponse<T>;
v5lite: any;
v5full: any;
}>;

export interface SearchClient {
search: DefaultSearchClient['search'];
searchForFacetValues?: DefaultSearchClient extends {
searchForFacetValues: unknown;
}
? DefaultSearchClient['searchForFacetValues']
: never;
initIndex?: DefaultSearchClient extends { initIndex: unknown }
? DefaultSearchClient['initIndex']
: never;
addAlgoliaAgent?: DefaultSearchClient['addAlgoliaAgent'];
}