Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(preset-algolia): support algoliasearch v5 #1002

Merged
merged 2 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/autocomplete-preset-algolia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"algoliasearch": "4.9.1"
},
"peerDependencies": {
"@algolia/client-search": "^4.9.1",
"algoliasearch": "^4.9.1"
"@algolia/client-search": ">= 4.9.1 < 6",
"algoliasearch": ">= 4.9.1 < 6"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HighlightResult } from '@algolia/client-search';
import { HighlightResult } from '../types';

export type HighlightedHit<THit> = THit & {
_highlightResult?: HighlightResult<THit>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SnippetResult } from '@algolia/client-search';
import { SnippetResult } from '../types';

export type SnippetedHit<THit> = THit & {
_snippetResult?: SnippetResult<THit>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {
import { fetchAlgoliaResults } from '../search';
import type {
MultipleQueriesQuery,
SearchForFacetValuesResponse,
SearchResponse,
} from '@algolia/client-search';
import { SearchClient } from 'algoliasearch/lite';

import { fetchAlgoliaResults } from '../search';
SearchClient,
} from '../types';

type Fetcher = typeof fetchAlgoliaResults;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MultipleQueriesQuery } from '@algolia/client-search';
import type { MultipleQueriesQuery } from '../types';

import { createAlgoliaRequester } from './createAlgoliaRequester';
import { RequestParams } from './createRequester';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import {
userAgents as coreUserAgents,
UserAgent,
} from '@algolia/autocomplete-shared';
import {

import { HIGHLIGHT_PRE_TAG, HIGHLIGHT_POST_TAG } from '../constants';
import type {
MultipleQueriesQuery,
SearchForFacetValuesResponse,
SearchResponse,
} from '@algolia/client-search';
import type { SearchClient } from 'algoliasearch/lite';

import { HIGHLIGHT_PRE_TAG, HIGHLIGHT_POST_TAG } from '../constants';
SearchClient,
} from '../types';

export interface SearchParams {
/**
Expand Down
71 changes: 71 additions & 0 deletions packages/autocomplete-preset-algolia/src/types/algoliasearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as ClientSearch from '@algolia/client-search';
import type * as AlgoliaSearch from 'algoliasearch/lite';

// turns any to unknown, so it can be used as a conditional
type AnyToUnknown<TSubject> = (any extends TSubject ? true : false) extends true
? unknown
: TSubject;

type SearchClientShape = {
search: unknown;
};

type ClientLiteV5 = AnyToUnknown<
// @ts-ignore
ReturnType<typeof AlgoliaSearch.liteClient>
>;
type ClientSearchV5 = AnyToUnknown<
// @ts-ignore
ReturnType<typeof ClientSearch.searchClient>
>;
type ClientV5 = ClientLiteV5 extends SearchClientShape
? ClientLiteV5
: ClientSearchV5 extends SearchClientShape
? ClientSearchV5
: unknown;

type PickForClient<
TMapping extends { v4: unknown; v5: unknown }
> = ClientV5 extends SearchClientShape ? TMapping['v5'] : TMapping['v4'];

export type SearchClient = PickForClient<{
// @ts-ignore
v4: AlgoliaSearch.SearchClient;
// @ts-ignore
v5: ClientV5;
}>;

export type MultipleQueriesQuery = PickForClient<{
// @ts-ignore
v4: ClientSearch.MultipleQueriesQuery;
// @ts-ignore
v5: AlgoliaSearch.LegacySearchMethodProps[number];
}>;

export type SearchForFacetValuesResponse = PickForClient<{
// @ts-ignore
v4: ClientSearch.SearchForFacetValuesResponse;
// @ts-ignore
v5: AlgoliaSearch.SearchForFacetValuesResponse;
}>;

export type SearchResponse<THit> = PickForClient<{
// @ts-ignore
v4: ClientSearch.SearchResponse<THit>;
// @ts-ignore
v5: AlgoliaSearch.SearchResponse; // should be generic, but isn't yet in the client
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
}>;

export type HighlightResult<THit> = PickForClient<{
// @ts-ignore
v4: ClientSearch.HighlightResult<THit>;
// @ts-ignore
v5: AlgoliaSearch.HighlightResult; // should be generic, but isn't yet in the client
}>;

export type SnippetResult<THit> = PickForClient<{
// @ts-ignore
v4: ClientSearch.SnippetResult<THit>;
// @ts-ignore
v5: AlgoliaSearch.SnippetResult; // should be generic, but isn't yet in the client
}>;
1 change: 1 addition & 0 deletions packages/autocomplete-preset-algolia/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './algoliasearch';