Skip to content

Commit

Permalink
feat(types): support algoliasearch v5 (#5066)
Browse files Browse the repository at this point in the history
* feat(types): support algoliasearch v5

simplifies the types on top of algolia/algoliasearch-helper-js#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
  • Loading branch information
Haroenv authored and sarahdayan committed Jul 25, 2022
1 parent 9d7bb31 commit 45e35d3
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 101 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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",
"htm": "^3.1.1",
Expand Down Expand Up @@ -149,7 +149,7 @@
"webpack": "4.41.5"
},
"peerDependencies": {
"algoliasearch": ">= 3.1 < 5"
"algoliasearch": ">= 3.1 < 6"
},
"resolutions": {
"places.js/algoliasearch": "3.35.0"
Expand Down
10 changes: 7 additions & 3 deletions src/connectors/answers/connectAnswers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import type {
Hits,
Hit,
FindAnswersOptions,
FindAnswersParameters,
FindAnswersResponse,
WidgetRenderState,
SearchClient,
} from '../../types';

type IndexWithAnswers = {
Expand Down Expand Up @@ -138,7 +138,11 @@ const connectAnswers: AnswersConnector = function connectAnswers(

// this does not directly use DebouncedFunction<findAnswers>, since then the generic will disappear
let debouncedRefine: DebouncedFunction<
(...params: FindAnswersParameters) => Promise<FindAnswersResponse<Hit>>
ReturnType<NonNullable<SearchClient['initIndex']>> extends {
findAnswers: infer FindAnswers;
}
? FindAnswers
: any
>;

return {
Expand Down Expand Up @@ -199,7 +203,7 @@ const connectAnswers: AnswersConnector = function connectAnswers(
...extraParameters,
nbHits,
attributesForPrediction,
})
}) as unknown as Promise<FindAnswersResponse<Hit>>
).then((result) => {
if (!result) {
// It's undefined when it's debounced.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -839,9 +839,13 @@ search.addWidgets([
];

const searchClient = createSearchClient({
search() {
search<T>() {
return Promise.resolve(
createMultiSearchResponse(createSingleSearchResponse({ hits }))
createMultiSearchResponse(
createSingleSearchResponse({
hits: hits as unknown as SearchResponse<T>['hits'],
})
)
);
},
});
Expand Down
9 changes: 7 additions & 2 deletions src/connectors/geo-search/__tests__/connectGeoSearch-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -1731,9 +1732,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/geo-search/
];

const searchClient = createSearchClient({
search() {
search<T>() {
return Promise.resolve(
createMultiSearchResponse(createSingleSearchResponse({ hits }))
createMultiSearchResponse(
createSingleSearchResponse({
hits: hits as unknown as SearchResponse<T>['hits'],
})
)
);
},
});
Expand Down
9 changes: 7 additions & 2 deletions src/connectors/hits/__tests__/connectHits-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
EscapedHits,
Hit,
HitAttributeHighlightResult,
SearchResponse,
} from '../../../types';
import { createInstantSearch } from '../../../../test/mock/createInstantSearch';
import { wait } from '../../../../test/utils/wait';
Expand Down Expand Up @@ -851,9 +852,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
];

const searchClient = createSearchClient({
search() {
search<T>() {
return Promise.resolve(
createMultiSearchResponse(createSingleSearchResponse({ hits }))
createMultiSearchResponse(
createSingleSearchResponse({
hits: hits as unknown as SearchResponse<T>['hits'],
})
)
);
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
HitAttributeHighlightResult,
Hit,
EscapedHits,
SearchResponse,
} from '../../../types';
import { createInstantSearch } from '../../../../test/mock/createInstantSearch';
import {
Expand Down Expand Up @@ -1441,9 +1442,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
];

const searchClient = createSearchClient({
search() {
search<T>() {
return Promise.resolve(
createMultiSearchResponse(createSingleSearchResponse({ hits }))
createMultiSearchResponse(
createSingleSearchResponse({
hits: hits as unknown as SearchResponse<T>['hits'],
})
)
);
},
});
Expand Down
73 changes: 2 additions & 71 deletions src/types/algoliasearch.ts
Original file line number Diff line number Diff line change
@@ -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<TObject> = AlgoliaSearch.Response<TObject>;
/** @ts-ignore */
type SearchResponseV4<TObject> = ClientSearch.SearchResponse<TObject>;

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<typeof algoliasearch>;

type SearchIndex = ReturnType<DefaultSearchClient['initIndex']>;

export type SearchClient = {
search: DefaultSearchClient['search'];
searchForFacetValues: DefaultSearchClient['searchForFacetValues'];
addAlgoliaAgent?: DefaultSearchClient['addAlgoliaAgent'];
initIndex?: (
indexName: string
) => SearchIndex extends { findAnswers: any }
? Partial<Pick<SearchIndex, 'findAnswers'>>
: SearchIndex;
};

export type MultiResponse<THit = any> = {
results: Array<SearchResponse<THit>>;
};

export type SearchResponse<THit> =
DefaultSearchClient extends DummySearchClientV4
? SearchResponseV4<THit>
: SearchResponseV3<THit> & 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<TObject> =
DefaultSearchClient extends DummySearchClientV4
? ClientSearch.FindAnswersResponse<TObject>
: any;
export {};
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export * from './utils';

// Algolia-related
// eslint-disable-next-line import/export
export * from './algoliasearch';
export * from './results';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
},
},
]);
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/hits/__tests__/hits-integration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const createInstantSearch = ({
} = {}) => {
const search = instantsearch({
indexName: 'instant_search',
searchClient: createSearchClient({ hitsPerPage }) as any,
searchClient: createSearchClient({ hitsPerPage }),
});

search.addWidgets([
Expand Down Expand Up @@ -223,7 +223,7 @@ describe('hits', () => {
searchClient: createSearchClient({
hitsPerPage,
includeQueryID: true,
}) as any,
}),
insightsClient: aa,
});

Expand Down
4 changes: 2 additions & 2 deletions test/mock/createAPIResponse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {
MultiResponse,
SearchResponse,
SearchResponses,
SearchForFacetValuesResponse,
} from '../../src/types';

Expand Down Expand Up @@ -38,7 +38,7 @@ export const createSingleSearchResponse = <THit = any>(

export const createMultiSearchResponse = <THit = any>(
...args: Array<Partial<SearchResponse<THit>>>
): MultiResponse => {
): SearchResponses<THit> => {
if (!args.length) {
return {
results: [createSingleSearchResponse()],
Expand Down
6 changes: 3 additions & 3 deletions test/mock/createSearchClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SearchClient, MultiResponse } from '../../src/types';
import type { SearchClient, SearchResponses } from '../../src/types';

import {
createSingleSearchResponse,
Expand All @@ -23,7 +23,7 @@ export const createSearchClient = (
type ControlledClient = {
searchClient: SearchClient;
searches: Array<{
promise: Promise<MultiResponse>;
promise: Promise<SearchResponses<any>>;
resolver: () => void;
}>;
};
Expand All @@ -35,7 +35,7 @@ export const createControlledSearchClient = (
const searchClient = createSearchClient({
search: jest.fn(() => {
let resolver: () => void;
const promise: Promise<MultiResponse> = new Promise((resolve) => {
const promise: Promise<SearchResponses<any>> = new Promise((resolve) => {
resolver = () => resolve(createMultiSearchResponse());
});

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3168,10 +3168,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"

Expand Down

0 comments on commit 45e35d3

Please sign in to comment.