/
getRecommendations.ts
42 lines (37 loc) · 1.22 KB
/
getRecommendations.ts
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
import { MethodEnum } from '@algolia/requester-common';
import {
BaseRecommendClient,
RecommendationsQuery,
TrendingFacetsQuery,
TrendingItemsQuery,
TrendingModel,
WithRecommendMethods,
} from '../types';
type GetRecommendations = (
base: BaseRecommendClient
) => WithRecommendMethods<BaseRecommendClient>['getRecommendations'];
type TrendingQuery =
| (TrendingItemsQuery & { readonly model: TrendingModel })
| (TrendingFacetsQuery & { readonly model: TrendingModel });
export const getRecommendations: GetRecommendations = base => {
return (queries: ReadonlyArray<RecommendationsQuery | TrendingQuery>, requestOptions) => {
const requests: ReadonlyArray<RecommendationsQuery | TrendingQuery> = queries.map(query => ({
...query,
// The `threshold` param is required by the endpoint to make it easier
// to provide a default value later, so we default it in the client
// so that users don't have to provide a value.
threshold: query.threshold || 0,
}));
return base.transporter.read(
{
method: MethodEnum.Post,
path: '1/indexes/*/recommendations',
data: {
requests,
},
cacheable: true,
},
requestOptions
);
};
};