Skip to content

Commit 727c787

Browse files
fix(recommend): Add a trendingQuery type
1 parent 36bbcfc commit 727c787

12 files changed

+100
-42
lines changed

packages/recommend/src/methods/getTrendingFacets.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1+
import { MethodEnum } from '@algolia/requester-common';
2+
13
import { BaseRecommendClient, TrendingFacetsQuery, WithRecommendMethods } from '../types';
2-
import { getRecommendations } from './getRecommendations';
34

45
type GetTrendingFacets = (
56
base: BaseRecommendClient
67
) => WithRecommendMethods<BaseRecommendClient>['getTrendingFacets'];
78

89
export const getTrendingFacets: GetTrendingFacets = base => {
910
return (queries: readonly TrendingFacetsQuery[], requestOptions) => {
10-
return getRecommendations(base)(
11-
queries.map(query => ({
12-
...query,
13-
model: 'trending-facets',
14-
})),
11+
const requests: readonly TrendingFacetsQuery[] = queries.map(query => ({
12+
...query,
13+
model: 'trending-facets',
14+
// The `threshold` param is required by the endpoint to make it easier
15+
// to provide a default value later, so we default it in the client
16+
// so that users don't have to provide a value.
17+
threshold: query.threshold || 0,
18+
}));
19+
20+
return base.transporter.read(
21+
{
22+
method: MethodEnum.Post,
23+
path: '1/indexes/*/recommendations',
24+
data: {
25+
requests,
26+
},
27+
cacheable: true,
28+
},
1529
requestOptions
1630
);
1731
};

packages/recommend/src/methods/getTrendingGlobalItems.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1+
import { MethodEnum } from '@algolia/requester-common';
2+
13
import { BaseRecommendClient, TrendingGlobalItemsQuery, WithRecommendMethods } from '../types';
2-
import { getRecommendations } from './getRecommendations';
34

45
type GetTrendingGlobalItems = (
56
base: BaseRecommendClient
67
) => WithRecommendMethods<BaseRecommendClient>['getTrendingGlobalItems'];
78

89
export const getTrendingGlobalItems: GetTrendingGlobalItems = base => {
910
return (queries: readonly TrendingGlobalItemsQuery[], requestOptions) => {
10-
return getRecommendations(base)(
11-
queries.map(query => ({
12-
...query,
13-
model: 'trending-items',
14-
})),
11+
const requests: readonly TrendingGlobalItemsQuery[] = queries.map(query => ({
12+
...query,
13+
model: 'trending-items',
14+
// The `threshold` param is required by the endpoint to make it easier
15+
// to provide a default value later, so we default it in the client
16+
// so that users don't have to provide a value.
17+
threshold: query.threshold || 0,
18+
}));
19+
20+
return base.transporter.read(
21+
{
22+
method: MethodEnum.Post,
23+
path: '1/indexes/*/recommendations',
24+
data: {
25+
requests,
26+
},
27+
cacheable: true,
28+
},
1529
requestOptions
1630
);
1731
};

packages/recommend/src/methods/getTrendingItemsForFacet.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { BaseRecommendClient, TrendingItemsForFacetQuery, WithRecommendMethods } from '../types';
2-
import { getRecommendations } from './getRecommendations';
2+
import { getTrendingGlobalItems } from '.';
33

44
type GetTrendingItemsForFacet = (
55
base: BaseRecommendClient
66
) => WithRecommendMethods<BaseRecommendClient>['getTrendingItemsForFacet'];
77

88
export const getTrendingItemsForFacet: GetTrendingItemsForFacet = base => {
99
return (queries: readonly TrendingItemsForFacetQuery[], requestOptions) => {
10-
return getRecommendations(base)(
10+
return getTrendingGlobalItems(base)(
1111
queries.map(query => ({
1212
...query,
13-
model: 'trending-items',
1413
})),
1514
requestOptions
1615
);

packages/recommend/src/types/FrequentlyBoughtTogetherQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { RecommendationsQuery } from './RecommendationsQuery';
22

33
export type FrequentlyBoughtTogetherQuery = Omit<
44
RecommendationsQuery,
5-
'model' | 'fallbackParameters' | 'facetName' | 'facetValue'
5+
'model' | 'fallbackParameters'
66
>;
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
export type RecommendModel =
2-
| 'related-products'
3-
| 'bought-together'
4-
| 'trending-items'
5-
| 'trending-facets';
1+
export type RecommendModel = 'related-products' | 'bought-together';
2+
export type TrendingModel = 'trending-items' | 'trending-facets';

packages/recommend/src/types/RecommendationsQuery.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type RecommendationsQuery = {
1515
/**
1616
* The `objectID` of the item to get recommendations for.
1717
*/
18-
readonly objectID?: string;
18+
readonly objectID: string;
1919

2020
/**
2121
* Threshold for the recommendations confidence score (between 0 and 100). Only recommendations with a greater score are returned.
@@ -38,14 +38,4 @@ export type RecommendationsQuery = {
3838
* Additional filters to use as fallback when there aren’t enough recommendations.
3939
*/
4040
readonly fallbackParameters?: RecommendSearchOptions;
41-
42-
/**
43-
* Used for trending model
44-
*/
45-
readonly facetName?: string;
46-
47-
/**
48-
* Used for trending model
49-
*/
50-
readonly facetValue?: string;
5141
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { RecommendationsQuery } from './RecommendationsQuery';
22

3-
export type RelatedProductsQuery = Omit<RecommendationsQuery, 'model' | 'facetName' | 'facetValue'>;
3+
export type RelatedProductsQuery = Omit<RecommendationsQuery, 'model'>;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { RecommendationsQuery } from './RecommendationsQuery';
1+
import { TrendingQuery } from './TrendingQuery';
22

3-
export type TrendingFacetsQuery = Omit<RecommendationsQuery, 'model' | 'objectID' | 'facetValue'>;
3+
export type TrendingFacetsQuery = Omit<TrendingQuery, 'model' | 'facetValue'>;
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { RecommendationsQuery } from './RecommendationsQuery';
1+
import { TrendingQuery } from './TrendingQuery';
22

3-
export type TrendingGlobalItemsQuery = Omit<
4-
RecommendationsQuery,
5-
'model' | 'objectID' | 'facetName' | 'facetValue'
6-
>;
3+
export type TrendingGlobalItemsQuery = Omit<TrendingQuery, 'model' | 'facetName' | 'facetValue'>;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { RecommendationsQuery } from './RecommendationsQuery';
1+
import { TrendingQuery } from './TrendingQuery';
22

3-
export type TrendingItemsForFacetQuery = Omit<RecommendationsQuery, 'model' | 'objectID'>;
3+
export type TrendingItemsForFacetQuery = Omit<TrendingQuery, 'model'>;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { TrendingModel } from './RecommendModel';
2+
import { RecommendSearchOptions } from './RecommendSearchOptions';
3+
4+
export type TrendingQuery = {
5+
/**
6+
* The name of the target index.
7+
*/
8+
readonly indexName: string;
9+
10+
/**
11+
* The name of the Recommendation model to use.
12+
*/
13+
readonly model: TrendingModel;
14+
15+
/**
16+
* Threshold for the recommendations confidence score (between 0 and 100). Only recommendations with a greater score are returned.
17+
*/
18+
readonly threshold?: number;
19+
20+
/**
21+
* How many recommendations to retrieve.
22+
*/
23+
readonly maxRecommendations?: number;
24+
25+
/**
26+
* List of [search parameters](https://www.algolia.com/doc/api-reference/search-api-parameters/) to send.
27+
*/
28+
readonly queryParameters?: RecommendSearchOptions;
29+
30+
/**
31+
* List of [search parameters](https://www.algolia.com/doc/api-reference/search-api-parameters/) to send.
32+
*
33+
* Additional filters to use as fallback when there aren’t enough recommendations.
34+
*/
35+
readonly fallbackParameters?: RecommendSearchOptions;
36+
37+
/**
38+
* Used for trending model
39+
*/
40+
readonly facetName: string;
41+
42+
/**
43+
* Used for trending model
44+
*/
45+
readonly facetValue: string;
46+
};

packages/recommend/src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ export * from './RelatedProductsQuery';
1313
export * from './TrendingFacetsQuery';
1414
export * from './TrendingGlobalItemsQuery';
1515
export * from './TrendingItemsForFacetQuery';
16+
export * from './TrendingQuery';
1617
export * from './WithRecommendMethods';

0 commit comments

Comments
 (0)