Skip to content

Commit 004f033

Browse files
authoredDec 7, 2023
feat(recommend): add recommended-for-you model (#1490)
1 parent 94b46b5 commit 004f033

12 files changed

+60
-3
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
},
107107
{
108108
"path": "packages/recommend/dist/recommend.umd.js",
109-
"maxSize": "4.3KB"
109+
"maxSize": "4.4KB"
110110
}
111111
]
112112
}

‎packages/recommend/src/builds/browser.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getFrequentlyBoughtTogether,
1313
getLookingSimilar,
1414
getRecommendations,
15+
getRecommendedForYou,
1516
getRelatedProducts,
1617
getTrendingFacets,
1718
getTrendingItems,
@@ -57,6 +58,7 @@ export default function recommend(
5758
getTrendingFacets,
5859
getTrendingItems,
5960
getLookingSimilar,
61+
getRecommendedForYou,
6062
},
6163
});
6264
}

‎packages/recommend/src/builds/node.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
getFrequentlyBoughtTogether,
1212
getLookingSimilar,
1313
getRecommendations,
14+
getRecommendedForYou,
1415
getRelatedProducts,
1516
getTrendingFacets,
1617
getTrendingItems,
@@ -51,6 +52,7 @@ export default function recommend(
5152
getTrendingFacets,
5253
getTrendingItems,
5354
getLookingSimilar,
55+
getRecommendedForYou,
5456
},
5557
});
5658
}

‎packages/recommend/src/methods/getRecommendations.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { MethodEnum } from '@algolia/requester-common';
22

3-
import { BaseRecommendClient, RecommendationsQuery, WithRecommendMethods } from '../types';
3+
import {
4+
BaseRecommendClient,
5+
RecommendationsQuery,
6+
RecommendedForYouQuery,
7+
WithRecommendMethods,
8+
} from '../types';
49
import { TrendingQuery } from '../types/TrendingQuery';
510

611
type GetRecommendations = (
@@ -9,7 +14,9 @@ type GetRecommendations = (
914

1015
export const getRecommendations: GetRecommendations = base => {
1116
return (queries: ReadonlyArray<RecommendationsQuery | TrendingQuery>, requestOptions) => {
12-
const requests: ReadonlyArray<RecommendationsQuery | TrendingQuery> = queries.map(query => ({
17+
const requests: ReadonlyArray<
18+
RecommendationsQuery | TrendingQuery | RecommendedForYouQuery
19+
> = queries.map(query => ({
1320
...query,
1421
// The `threshold` param is required by the endpoint to make it easier
1522
// to provide a default value later, so we default it in the client
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { MethodEnum } from '@algolia/requester-common';
2+
3+
import { BaseRecommendClient, RecommendedForYouQuery, WithRecommendMethods } from '../types';
4+
5+
type GetRecommendedForYou = (
6+
base: BaseRecommendClient
7+
) => WithRecommendMethods<BaseRecommendClient>['getRecommendedForYou'];
8+
9+
export const getRecommendedForYou: GetRecommendedForYou = base => {
10+
return (queries: readonly RecommendedForYouQuery[], requestOptions) => {
11+
const requests: readonly RecommendedForYouQuery[] = queries.map(query => ({
12+
...query,
13+
model: 'recommended-for-you',
14+
threshold: query.threshold || 0,
15+
}));
16+
17+
return base.transporter.read(
18+
{
19+
method: MethodEnum.Post,
20+
path: '1/indexes/*/recommendations',
21+
data: {
22+
requests,
23+
},
24+
cacheable: true,
25+
},
26+
requestOptions
27+
);
28+
};
29+
};

‎packages/recommend/src/methods/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from './getRelatedProducts';
88
export * from './getTrendingFacets';
99
export * from './getTrendingItems';
1010
export * from './getLookingSimilar';
11+
export * from './getRecommendedForYou';

‎packages/recommend/src/types/RecommendModel.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export type RecommendModel =
33
| 'related-products'
44
| 'bought-together'
55
| 'looking-similar'
6+
| 'recommended-for-you'
67
| TrendingModel;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { RecommendationsQuery } from './RecommendationsQuery';
2+
3+
export type RecommendedForYouQuery = Omit<RecommendationsQuery, 'model' | 'objectID'>;

‎packages/recommend/src/types/WithRecommendMethods.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { SearchOptions, SearchResponse } from '@algolia/client-search';
22
import { RequestOptions } from '@algolia/transporter';
33

4+
import { RecommendedForYouQuery } from '../builds/node';
45
import { FrequentlyBoughtTogetherQuery } from './FrequentlyBoughtTogetherQuery';
56
import { LookingSimilarQuery } from './LookingSimilarQuery';
67
import { RecommendationsQuery } from './RecommendationsQuery';
@@ -72,4 +73,12 @@ export type WithRecommendMethods<TType> = TType & {
7273
queries: readonly LookingSimilarQuery[],
7374
requestOptions?: RequestOptions & SearchOptions
7475
) => Readonly<Promise<RecommendQueriesResponse<TObject>>>;
76+
77+
/**
78+
* Returns Recommended for you
79+
*/
80+
readonly getRecommendedForYou: <TObject>(
81+
queries: readonly RecommendedForYouQuery[],
82+
requestOptions?: RequestOptions & SearchOptions
83+
) => Readonly<Promise<RecommendQueriesResponse<TObject>>>;
7584
};

‎packages/recommend/src/types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ export * from './TrendingItemsQuery';
1515
export * from './TrendingQuery';
1616
export * from './WithRecommendMethods';
1717
export * from './LookingSimilarQuery';
18+
export * from './RecommendedForYouQuery';
1819
export * from './TrendingFacetHit';
1920
export * from './TrendingFacetsResponse';

‎playground/browser/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<html>
33
<head>
44
<script src="/playground/browser/algoliasearch.umd.js"></script>
5+
<script src="/playground/browser/recommend.umd.js"></script>
56
</head>
67
<body></body>
78
</html>

‎scripts/prepare-playground.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
cp -a packages/algoliasearch/dist/algoliasearch-lite.umd* playground/browser-lite/.
22
cp -a packages/algoliasearch/dist/algoliasearch.umd* playground/browser/.
3+
cp -a packages/recommend/dist/recommend.umd* playground/browser/.

0 commit comments

Comments
 (0)
Please sign in to comment.