-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgetRecommendedForYou.ts
More file actions
54 lines (49 loc) · 1.57 KB
/
getRecommendedForYou.ts
File metadata and controls
54 lines (49 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Hit } from '@algolia/client-search';
import { RecommendClient, RecommendedForYouParams } from '@algolia/recommend';
import { addAbsolutePosition } from './utils/addAbsolutePosition';
import { addIndexName } from './utils/addIndexName';
import { addQueryID } from './utils/addQueryID';
import { version } from './version';
export type GetRecommendedForYouProps<TObject> = {
/**
* The initialized Algolia recommend client.
*/
recommendClient: RecommendClient;
/**
* A function to transform the retrieved items before passing them to the component.
* It’s useful to add or remove items, change them, or reorder them.
*/
transformItems?: (items: Array<Hit<TObject>>) => Array<Hit<TObject>>;
} & RecommendedForYouParams;
export function getRecommendedForYou<TObject>({
indexName,
threshold,
queryParameters,
recommendClient,
maxRecommendations,
transformItems = (x) => x,
}: GetRecommendedForYouProps<TObject>) {
recommendClient.addAlgoliaAgent('recommend-core', version);
const queries: RecommendedForYouParams[] = [
{
indexName,
threshold,
queryParameters,
maxRecommendations,
},
];
return recommendClient
.getRecommendedForYou<TObject>(queries)
.then((response) => ({
recommendations: transformItems(
response.results
.map((result) =>
result.hits
.map((hit) => addIndexName(hit, indexName))
.map((hit) => addQueryID(hit, result.queryID))
)
.flat()
.map((hit, idx) => addAbsolutePosition(hit, idx))
),
}));
}