From 3eb4dc75a5935f2ee4fead8787f39af0150b24c4 Mon Sep 17 00:00:00 2001 From: Haroen Viaene Date: Tue, 28 Jun 2022 11:01:43 +0200 Subject: [PATCH] feat(types): support algoliasearch v5 (#5066) * feat(types): support algoliasearch v5 simplifies the types on top of https://github.com/algolia/algoliasearch-helper-js/pull/910 * update with trial * try with that other file * let's do it this way * cast as any, because the type is too narrow * udpate helper again * answers * tryalong * upd * update helper * update to released helper * fix ^ * fix unit test * move anys * Update src/connectors/geo-search/__tests__/connectGeoSearch-test.ts * remove T * allez, avoidable like this --- package.json | 4 +- src/connectors/answers/connectAnswers.ts | 10 ++- .../__tests__/connectAutocomplete-test.ts | 10 ++- .../__tests__/connectGeoSearch-test.ts | 9 ++- .../hits/__tests__/connectHits-test.ts | 9 ++- .../__tests__/connectInfiniteHits-test.ts | 9 ++- src/types/algoliasearch.ts | 73 +------------------ src/types/index.ts | 1 + .../__tests__/hierarchical-menu-test.ts | 6 +- .../hits/__tests__/hits-integration-test.ts | 4 +- test/mock/createAPIResponse.ts | 4 +- test/mock/createSearchClient.ts | 6 +- yarn.lock | 8 +- 13 files changed, 52 insertions(+), 101 deletions(-) diff --git a/package.json b/package.json index febc44706f..3862cf4d25 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "@types/google.maps": "^3.45.3", "@types/hogan.js": "^3.0.0", "@types/qs": "^6.5.3", - "algoliasearch-helper": "^3.9.0", + "algoliasearch-helper": "^3.10.0", "classnames": "^2.2.5", "hogan.js": "^3.0.2", "preact": "^10.6.0", @@ -146,7 +146,7 @@ "webpack": "4.41.5" }, "peerDependencies": { - "algoliasearch": ">= 3.1 < 5" + "algoliasearch": ">= 3.1 < 6" }, "resolutions": { "places.js/algoliasearch": "3.35.0" diff --git a/src/connectors/answers/connectAnswers.ts b/src/connectors/answers/connectAnswers.ts index fe9178715c..030dd174c5 100644 --- a/src/connectors/answers/connectAnswers.ts +++ b/src/connectors/answers/connectAnswers.ts @@ -14,9 +14,9 @@ import type { Hits, Hit, FindAnswersOptions, - FindAnswersParameters, FindAnswersResponse, WidgetRenderState, + SearchClient, } from '../../types'; type IndexWithAnswers = { @@ -138,7 +138,11 @@ const connectAnswers: AnswersConnector = function connectAnswers( // this does not directly use DebouncedFunction, since then the generic will disappear let debouncedRefine: DebouncedFunction< - (...params: FindAnswersParameters) => Promise> + ReturnType> extends { + findAnswers: infer FindAnswers; + } + ? FindAnswers + : any >; return { @@ -199,7 +203,7 @@ const connectAnswers: AnswersConnector = function connectAnswers( ...extraParameters, nbHits, attributesForPrediction, - }) + }) as unknown as Promise> ).then((result) => { if (!result) { // It's undefined when it's debounced. diff --git a/src/connectors/autocomplete/__tests__/connectAutocomplete-test.ts b/src/connectors/autocomplete/__tests__/connectAutocomplete-test.ts index 146d4a672d..ec6b002a29 100644 --- a/src/connectors/autocomplete/__tests__/connectAutocomplete-test.ts +++ b/src/connectors/autocomplete/__tests__/connectAutocomplete-test.ts @@ -15,7 +15,7 @@ import { import type { AutocompleteRenderState } from '../connectAutocomplete'; import connectAutocomplete from '../connectAutocomplete'; import { TAG_PLACEHOLDER } from '../../../lib/utils'; -import type { SearchClient } from '../../../types'; +import type { SearchClient, SearchResponse } from '../../../types'; import { wait } from '../../../../test/utils/wait'; import instantsearch from '../../../index.es'; @@ -839,9 +839,13 @@ search.addWidgets([ ]; const searchClient = createSearchClient({ - search() { + search() { return Promise.resolve( - createMultiSearchResponse(createSingleSearchResponse({ hits })) + createMultiSearchResponse( + createSingleSearchResponse({ + hits: hits as unknown as SearchResponse['hits'], + }) + ) ); }, }); diff --git a/src/connectors/geo-search/__tests__/connectGeoSearch-test.ts b/src/connectors/geo-search/__tests__/connectGeoSearch-test.ts index 08bb4d12ca..c0e64252bc 100644 --- a/src/connectors/geo-search/__tests__/connectGeoSearch-test.ts +++ b/src/connectors/geo-search/__tests__/connectGeoSearch-test.ts @@ -16,6 +16,7 @@ import { } from '../../../../test/mock/createAPIResponse'; import instantsearch from '../../../index.es'; import { wait } from '../../../../test/utils/wait'; +import type { SearchResponse } from '../../../types'; describe('connectGeoSearch', () => { const createFakeHelper = () => { @@ -1731,9 +1732,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/geo-search/ ]; const searchClient = createSearchClient({ - search() { + search() { return Promise.resolve( - createMultiSearchResponse(createSingleSearchResponse({ hits })) + createMultiSearchResponse( + createSingleSearchResponse({ + hits: hits as unknown as SearchResponse['hits'], + }) + ) ); }, }); diff --git a/src/connectors/hits/__tests__/connectHits-test.ts b/src/connectors/hits/__tests__/connectHits-test.ts index 4be3ca0d8c..8ed564f9b1 100644 --- a/src/connectors/hits/__tests__/connectHits-test.ts +++ b/src/connectors/hits/__tests__/connectHits-test.ts @@ -22,6 +22,7 @@ import type { EscapedHits, Hit, HitAttributeHighlightResult, + SearchResponse, } from '../../../types'; import { createInstantSearch } from '../../../../test/mock/createInstantSearch'; import { wait } from '../../../../test/utils/wait'; @@ -851,9 +852,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co ]; const searchClient = createSearchClient({ - search() { + search() { return Promise.resolve( - createMultiSearchResponse(createSingleSearchResponse({ hits })) + createMultiSearchResponse( + createSingleSearchResponse({ + hits: hits as unknown as SearchResponse['hits'], + }) + ) ); }, }); diff --git a/src/connectors/infinite-hits/__tests__/connectInfiniteHits-test.ts b/src/connectors/infinite-hits/__tests__/connectInfiniteHits-test.ts index 7e6ce95849..7a7205b122 100644 --- a/src/connectors/infinite-hits/__tests__/connectInfiniteHits-test.ts +++ b/src/connectors/infinite-hits/__tests__/connectInfiniteHits-test.ts @@ -9,6 +9,7 @@ import type { HitAttributeHighlightResult, Hit, EscapedHits, + SearchResponse, } from '../../../types'; import { createInstantSearch } from '../../../../test/mock/createInstantSearch'; import { @@ -1441,9 +1442,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi ]; const searchClient = createSearchClient({ - search() { + search() { return Promise.resolve( - createMultiSearchResponse(createSingleSearchResponse({ hits })) + createMultiSearchResponse( + createSingleSearchResponse({ + hits: hits as unknown as SearchResponse['hits'], + }) + ) ); }, }); diff --git a/src/types/algoliasearch.ts b/src/types/algoliasearch.ts index 297988e794..1147accae2 100644 --- a/src/types/algoliasearch.ts +++ b/src/types/algoliasearch.ts @@ -1,72 +1,3 @@ -/* eslint-disable import/no-duplicates */ -// Custom types to support both algoliasearch -// `v3` and algoliasearch `v4` clients. +export * from 'algoliasearch-helper/types/algoliasearch.js'; -import type algoliasearch from 'algoliasearch/lite'; -import type * as AlgoliaSearch from 'algoliasearch/lite'; -/** @ts-ignore */ -import type * as ClientSearch from '@algolia/client-search'; - -/** @ts-ignore */ -type SearchResponseV3 = AlgoliaSearch.Response; -/** @ts-ignore */ -type SearchResponseV4 = ClientSearch.SearchResponse; - -type SearchForFacetValuesResponseV3 = - /** @ts-ignore */ - AlgoliaSearch.SearchForFacetValues.Response; -/** @ts-ignore */ -type SearchForFacetValuesResponseV4 = ClientSearch.SearchForFacetValuesResponse; - -type RelevantSortResponse = { - appliedRelevancyStrictness?: number; - nbSortedHits?: number; -}; - -type DummySearchClientV4 = { - readonly transporter: any; -}; - -type DefaultSearchClient = ReturnType; - -type SearchIndex = ReturnType; - -export type SearchClient = { - search: DefaultSearchClient['search']; - searchForFacetValues: DefaultSearchClient['searchForFacetValues']; - addAlgoliaAgent?: DefaultSearchClient['addAlgoliaAgent']; - initIndex?: ( - indexName: string - ) => SearchIndex extends { findAnswers: any } - ? Partial> - : SearchIndex; -}; - -export type MultiResponse = { - results: Array>; -}; - -export type SearchResponse = - DefaultSearchClient extends DummySearchClientV4 - ? SearchResponseV4 - : SearchResponseV3 & RelevantSortResponse; - -export type SearchForFacetValuesResponse = - DefaultSearchClient extends DummySearchClientV4 - ? SearchForFacetValuesResponseV4 - : SearchForFacetValuesResponseV3; - -export type FindAnswersParameters = SearchIndex extends { - findAnswers: (...params: infer Params) => any; -} - ? Params - : any; - -export type FindAnswersOptions = DefaultSearchClient extends DummySearchClientV4 - ? ClientSearch.FindAnswersOptions - : any; - -export type FindAnswersResponse = - DefaultSearchClient extends DummySearchClientV4 - ? ClientSearch.FindAnswersResponse - : any; +export {}; diff --git a/src/types/index.ts b/src/types/index.ts index 4931b72dd0..46af57f9f5 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -2,6 +2,7 @@ export * from './utils'; // Algolia-related +// eslint-disable-next-line import/export export * from './algoliasearch'; export * from './results'; diff --git a/src/widgets/hierarchical-menu/__tests__/hierarchical-menu-test.ts b/src/widgets/hierarchical-menu/__tests__/hierarchical-menu-test.ts index e674c8f264..f659e29fa0 100644 --- a/src/widgets/hierarchical-menu/__tests__/hierarchical-menu-test.ts +++ b/src/widgets/hierarchical-menu/__tests__/hierarchical-menu-test.ts @@ -375,15 +375,11 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hierarchica indexName: 'test', params: { analytics: false, - attributesToHighlight: [], - attributesToRetrieve: [], - attributesToSnippet: [], clickAnalytics: false, facets: ['hierarchy.1'], - hitsPerPage: 1, + hitsPerPage: 0, maxValuesPerFacet: 10, page: 0, - tagFilters: '', }, }, ]); diff --git a/src/widgets/hits/__tests__/hits-integration-test.ts b/src/widgets/hits/__tests__/hits-integration-test.ts index c57807fd04..2e48095536 100644 --- a/src/widgets/hits/__tests__/hits-integration-test.ts +++ b/src/widgets/hits/__tests__/hits-integration-test.ts @@ -47,7 +47,7 @@ const createInstantSearch = ({ } = {}) => { const search = instantsearch({ indexName: 'instant_search', - searchClient: createSearchClient({ hitsPerPage }) as any, + searchClient: createSearchClient({ hitsPerPage }), }); search.addWidgets([ @@ -223,7 +223,7 @@ describe('hits', () => { searchClient: createSearchClient({ hitsPerPage, includeQueryID: true, - }) as any, + }), insightsClient: aa, }); diff --git a/test/mock/createAPIResponse.ts b/test/mock/createAPIResponse.ts index 0c0a2d525b..e3017c6ea9 100644 --- a/test/mock/createAPIResponse.ts +++ b/test/mock/createAPIResponse.ts @@ -1,6 +1,6 @@ import type { - MultiResponse, SearchResponse, + SearchResponses, SearchForFacetValuesResponse, } from '../../src/types'; @@ -38,7 +38,7 @@ export const createSingleSearchResponse = ( export const createMultiSearchResponse = ( ...args: Array>> -): MultiResponse => { +): SearchResponses => { if (!args.length) { return { results: [createSingleSearchResponse()], diff --git a/test/mock/createSearchClient.ts b/test/mock/createSearchClient.ts index 973b6fdb54..89b921e608 100644 --- a/test/mock/createSearchClient.ts +++ b/test/mock/createSearchClient.ts @@ -1,4 +1,4 @@ -import type { SearchClient, MultiResponse } from '../../src/types'; +import type { SearchClient, SearchResponses } from '../../src/types'; import { createSingleSearchResponse, @@ -23,7 +23,7 @@ export const createSearchClient = ( type ControlledClient = { searchClient: SearchClient; searches: Array<{ - promise: Promise; + promise: Promise>; resolver: () => void; }>; }; @@ -35,7 +35,7 @@ export const createControlledSearchClient = ( const searchClient = createSearchClient({ search: jest.fn(() => { let resolver: () => void; - const promise: Promise = new Promise((resolve) => { + const promise: Promise> = new Promise((resolve) => { resolver = () => resolve(createMultiSearchResponse()); }); diff --git a/yarn.lock b/yarn.lock index f8d206e658..f9d98d2a0d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3156,10 +3156,10 @@ ajv@^6.1.0, ajv@^6.10.0, ajv@^6.10.2, ajv@^6.12.3, ajv@~6.12.6: json-schema-traverse "^0.4.1" uri-js "^4.2.2" -algoliasearch-helper@^3.9.0: - version "3.9.0" - resolved "https://registry.yarnpkg.com/algoliasearch-helper/-/algoliasearch-helper-3.9.0.tgz#1e99d351ecdcff48449644157a8d250c7c592828" - integrity sha512-siWWl8QYJ3sh1yzJf9h/cHHpZC8wuPoPdVx5OtQ8X62ruUembTwvsLYoicrL7pF7fsYxdyvJfV9Yb2/nrVGrfg== +algoliasearch-helper@^3.10.0: + version "3.10.0" + resolved "https://registry.yarnpkg.com/algoliasearch-helper/-/algoliasearch-helper-3.10.0.tgz#59a0f645dd3c7e55cf01faa568d1af50c49d36f6" + integrity sha512-4E4od8qWWDMVvQ3jaRX6Oks/k35ywD011wAA4LbYMMjOtaZV6VWaTjRr4iN2bdaXP2o1BP7SLFMBf3wvnHmd8Q== dependencies: "@algolia/events" "^4.0.1"