Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

algolia.ts

38 lines967 Bytes

1import algoliasearch from "algoliasearch";
2import { Hit } from "@algolia/client-search";
3​
4// From Algolia example
5// https://github.com/algolia/react-instantsearch
6const ALGOLIA_APP_ID = "latency";
7const ALGOLIA_SEARCH_API_KEY = "6be0576ff61c053d5f9a3225e2a90f76";
8​
9type SearchOptions = {
10 indexName: string;
11 query: string;
12 pageParam: number;
13 hitsPerPage: number;
14};
15​
16export async function search<TData>({
17 indexName,
18 query,
19 pageParam = 0,
20 hitsPerPage = 10,
21}: SearchOptions): Promise<{
22 hits: Hit<TData>[];
23 nextPage: number | undefined;
24}> {
25 const client = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_SEARCH_API_KEY);
26 const index = client.initIndex(indexName);
27​
28 console.log("alogolia:search", { indexName, query, pageParam, hitsPerPage });
29​
30 const { hits, page, nbPages } = await index.search<TData>(query, {
31 page: pageParam,
32 hitsPerPage,
33 });
34​
35 const nextPage = page + 1 < nbPages ? page + 1 : undefined;
36​
37 return { hits, nextPage };