File tree 15 files changed +258
-7
lines changed
15 files changed +258
-7
lines changed Original file line number Diff line number Diff line change 105
105
},
106
106
{
107
107
"path" : " packages/recommend/dist/recommend.umd.js" ,
108
- "maxSize" : " 4.1KB "
108
+ "maxSize" : " 4.2KB "
109
109
}
110
110
]
111
111
}
Original file line number Diff line number Diff line change
1
+ import { TestSuite } from '../../../client-common/src/__tests__/TestSuite' ;
2
+
3
+ const recommend = new TestSuite ( 'recommend' ) . recommend ;
4
+
5
+ function createMockedClient ( ) {
6
+ const client = recommend ( 'appId' , 'apiKey' ) ;
7
+ jest . spyOn ( client . transporter , 'read' ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
8
+
9
+ return client ;
10
+ }
11
+
12
+ describe ( 'getTrendingFacets' , ( ) => {
13
+ test ( 'builds the request' , async ( ) => {
14
+ const client = createMockedClient ( ) ;
15
+
16
+ await client . getTrendingFacets (
17
+ [
18
+ {
19
+ indexName : 'products' ,
20
+ facetName : 'company' ,
21
+ } ,
22
+ ] ,
23
+ { }
24
+ ) ;
25
+
26
+ expect ( client . transporter . read ) . toHaveBeenCalledTimes ( 1 ) ;
27
+ expect ( client . transporter . read ) . toHaveBeenCalledWith (
28
+ {
29
+ cacheable : true ,
30
+ data : {
31
+ requests : [
32
+ {
33
+ indexName : 'products' ,
34
+ model : 'trending-facets' ,
35
+ facetName : 'company' ,
36
+ threshold : 0 ,
37
+ } ,
38
+ ] ,
39
+ } ,
40
+ method : 'POST' ,
41
+ path : '1/indexes/*/recommendations' ,
42
+ } ,
43
+ { }
44
+ ) ;
45
+ } ) ;
46
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import { TestSuite } from '../../../client-common/src/__tests__/TestSuite' ;
2
+
3
+ const recommend = new TestSuite ( 'recommend' ) . recommend ;
4
+
5
+ function createMockedClient ( ) {
6
+ const client = recommend ( 'appId' , 'apiKey' ) ;
7
+ jest . spyOn ( client . transporter , 'read' ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
8
+
9
+ return client ;
10
+ }
11
+
12
+ describe ( 'getTrendingItems' , ( ) => {
13
+ test ( 'builds the request' , async ( ) => {
14
+ const client = createMockedClient ( ) ;
15
+
16
+ await client . getTrendingItems (
17
+ [
18
+ {
19
+ indexName : 'products' ,
20
+ } ,
21
+ ] ,
22
+ { }
23
+ ) ;
24
+
25
+ expect ( client . transporter . read ) . toHaveBeenCalledTimes ( 1 ) ;
26
+ expect ( client . transporter . read ) . toHaveBeenCalledWith (
27
+ {
28
+ cacheable : true ,
29
+ data : {
30
+ requests : [
31
+ {
32
+ indexName : 'products' ,
33
+ model : 'trending-items' ,
34
+ threshold : 0 ,
35
+ } ,
36
+ ] ,
37
+ } ,
38
+ method : 'POST' ,
39
+ path : '1/indexes/*/recommendations' ,
40
+ } ,
41
+ { }
42
+ ) ;
43
+ } ) ;
44
+ } ) ;
Original file line number Diff line number Diff line change @@ -8,7 +8,13 @@ import { createBrowserXhrRequester } from '@algolia/requester-browser-xhr';
8
8
import { createUserAgent } from '@algolia/transporter' ;
9
9
10
10
import { createRecommendClient } from '../createRecommendClient' ;
11
- import { getFrequentlyBoughtTogether , getRecommendations , getRelatedProducts } from '../methods' ;
11
+ import {
12
+ getFrequentlyBoughtTogether ,
13
+ getRecommendations ,
14
+ getRelatedProducts ,
15
+ getTrendingFacets ,
16
+ getTrendingItems ,
17
+ } from '../methods' ;
12
18
import { BaseRecommendClient , RecommendOptions , WithRecommendMethods } from '../types' ;
13
19
14
20
export default function recommend (
@@ -47,6 +53,8 @@ export default function recommend(
47
53
getFrequentlyBoughtTogether,
48
54
getRecommendations,
49
55
getRelatedProducts,
56
+ getTrendingFacets,
57
+ getTrendingItems,
50
58
} ,
51
59
} ) ;
52
60
}
Original file line number Diff line number Diff line change @@ -7,7 +7,13 @@ import { createNodeHttpRequester } from '@algolia/requester-node-http';
7
7
import { createUserAgent } from '@algolia/transporter' ;
8
8
9
9
import { createRecommendClient } from '../createRecommendClient' ;
10
- import { getFrequentlyBoughtTogether , getRecommendations , getRelatedProducts } from '../methods' ;
10
+ import {
11
+ getFrequentlyBoughtTogether ,
12
+ getRecommendations ,
13
+ getRelatedProducts ,
14
+ getTrendingFacets ,
15
+ getTrendingItems ,
16
+ } from '../methods' ;
11
17
import { BaseRecommendClient , RecommendOptions , WithRecommendMethods } from '../types' ;
12
18
13
19
export default function recommend (
@@ -41,6 +47,8 @@ export default function recommend(
41
47
getFrequentlyBoughtTogether,
42
48
getRecommendations,
43
49
getRelatedProducts,
50
+ getTrendingFacets,
51
+ getTrendingItems,
44
52
} ,
45
53
} ) ;
46
54
}
Original file line number Diff line number Diff line change 1
1
import { MethodEnum } from '@algolia/requester-common' ;
2
2
3
- import { BaseRecommendClient , RecommendationsQuery , WithRecommendMethods } from '../types' ;
3
+ import {
4
+ BaseRecommendClient ,
5
+ RecommendationsQuery ,
6
+ TrendingQuery ,
7
+ WithRecommendMethods ,
8
+ } from '../types' ;
4
9
5
10
type GetRecommendations = (
6
11
base : BaseRecommendClient
7
12
) => WithRecommendMethods < BaseRecommendClient > [ 'getRecommendations' ] ;
8
13
9
14
export const getRecommendations : GetRecommendations = base => {
10
- return ( queries : readonly RecommendationsQuery [ ] , requestOptions ) => {
11
- const requests : readonly RecommendationsQuery [ ] = queries . map ( query => ( {
15
+ return ( queries : ReadonlyArray < RecommendationsQuery | TrendingQuery > , requestOptions ) => {
16
+ const requests : ReadonlyArray < RecommendationsQuery | TrendingQuery > = queries . map ( query => ( {
12
17
...query ,
13
18
// The `threshold` param is required by the endpoint to make it easier
14
19
// to provide a default value later, so we default it in the client
Original file line number Diff line number Diff line change
1
+ import { MethodEnum } from '@algolia/requester-common' ;
2
+
3
+ import { BaseRecommendClient , TrendingFacetsQuery , WithRecommendMethods } from '../types' ;
4
+
5
+ type GetTrendingFacets = (
6
+ base : BaseRecommendClient
7
+ ) => WithRecommendMethods < BaseRecommendClient > [ 'getTrendingFacets' ] ;
8
+
9
+ export const getTrendingFacets : GetTrendingFacets = base => {
10
+ return ( queries : readonly TrendingFacetsQuery [ ] , requestOptions ) => {
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
+ } ,
29
+ requestOptions
30
+ ) ;
31
+ } ;
32
+ } ;
Original file line number Diff line number Diff line change
1
+ import { MethodEnum } from '@algolia/requester-common' ;
2
+
3
+ import { BaseRecommendClient , TrendingItemsQuery , WithRecommendMethods } from '../types' ;
4
+
5
+ type GetTrendingItems = (
6
+ base : BaseRecommendClient
7
+ ) => WithRecommendMethods < BaseRecommendClient > [ 'getTrendingItems' ] ;
8
+
9
+ export const getTrendingItems : GetTrendingItems = base => {
10
+ return ( queries : readonly TrendingItemsQuery [ ] , requestOptions ) => {
11
+ const requests : readonly TrendingItemsQuery [ ] = 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
+ } ,
29
+ requestOptions
30
+ ) ;
31
+ } ;
32
+ } ;
Original file line number Diff line number Diff line change 5
5
export * from './getFrequentlyBoughtTogether' ;
6
6
export * from './getRecommendations' ;
7
7
export * from './getRelatedProducts' ;
8
+ export * from './getTrendingFacets' ;
9
+ export * from './getTrendingItems' ;
Original file line number Diff line number Diff line change 1
- export type RecommendModel = 'related-products' | 'bought-together' ;
1
+ export type RecommendModel = 'related-products' | 'bought-together' | TrendingModel ;
2
+ export type TrendingModel = 'trending-items' | 'trending-facets' ;
Original file line number Diff line number Diff line change
1
+ import { TrendingQuery } from './TrendingQuery' ;
2
+
3
+ export type TrendingFacetsQuery = Omit < TrendingQuery , 'model' | 'facetValue' > ;
Original file line number Diff line number Diff line change
1
+ import { TrendingQuery } from './TrendingQuery' ;
2
+
3
+ export type TrendingItemsQuery = Omit < TrendingQuery , 'model' > ;
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ import { RequestOptions } from '@algolia/transporter';
4
4
import { FrequentlyBoughtTogetherQuery } from './FrequentlyBoughtTogetherQuery' ;
5
5
import { RecommendationsQuery } from './RecommendationsQuery' ;
6
6
import { RelatedProductsQuery } from './RelatedProductsQuery' ;
7
+ import { TrendingFacetsQuery } from './TrendingFacetsQuery' ;
8
+ import { TrendingItemsQuery } from './TrendingItemsQuery' ;
7
9
8
10
export type WithRecommendMethods < TType > = TType & {
9
11
/**
@@ -29,4 +31,20 @@ export type WithRecommendMethods<TType> = TType & {
29
31
queries : readonly FrequentlyBoughtTogetherQuery [ ] ,
30
32
requestOptions ?: RequestOptions & SearchOptions
31
33
) => Readonly < Promise < MultipleQueriesResponse < TObject > > > ;
34
+
35
+ /**
36
+ * Returns trending items
37
+ */
38
+ readonly getTrendingItems : < TObject > (
39
+ queries : readonly TrendingItemsQuery [ ] ,
40
+ requestOptions ?: RequestOptions & SearchOptions
41
+ ) => Readonly < Promise < MultipleQueriesResponse < TObject > > > ;
42
+
43
+ /**
44
+ * Returns trending items per facet
45
+ */
46
+ readonly getTrendingFacets : < TObject > (
47
+ queries : readonly TrendingFacetsQuery [ ] ,
48
+ requestOptions ?: RequestOptions & SearchOptions
49
+ ) => Readonly < Promise < MultipleQueriesResponse < TObject > > > ;
32
50
} ;
Original file line number Diff line number Diff line change @@ -10,4 +10,7 @@ export * from './RecommendOptions';
10
10
export * from './RecommendSearchOptions' ;
11
11
export * from './RecommendationsQuery' ;
12
12
export * from './RelatedProductsQuery' ;
13
+ export * from './TrendingFacetsQuery' ;
14
+ export * from './TrendingQuery' ;
15
+ export * from './TrendingItemsQuery' ;
13
16
export * from './WithRecommendMethods' ;
You can’t perform that action at this time.
0 commit comments