-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(recommend): Add trending types and methods (#1396)
Co-authored-by: Clément Vannicatte <20689156+shortcuts@users.noreply.github.com>
- Loading branch information
1 parent
ede7a4b
commit 6882ec8
Showing
15 changed files
with
258 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,7 +105,7 @@ | |
}, | ||
{ | ||
"path": "packages/recommend/dist/recommend.umd.js", | ||
"maxSize": "4.1KB" | ||
"maxSize": "4.2KB" | ||
} | ||
] | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/recommend/src/__tests__/getTrendingFacets.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { TestSuite } from '../../../client-common/src/__tests__/TestSuite'; | ||
|
||
const recommend = new TestSuite('recommend').recommend; | ||
|
||
function createMockedClient() { | ||
const client = recommend('appId', 'apiKey'); | ||
jest.spyOn(client.transporter, 'read').mockImplementation(() => Promise.resolve()); | ||
|
||
return client; | ||
} | ||
|
||
describe('getTrendingFacets', () => { | ||
test('builds the request', async () => { | ||
const client = createMockedClient(); | ||
|
||
await client.getTrendingFacets( | ||
[ | ||
{ | ||
indexName: 'products', | ||
facetName: 'company', | ||
}, | ||
], | ||
{} | ||
); | ||
|
||
expect(client.transporter.read).toHaveBeenCalledTimes(1); | ||
expect(client.transporter.read).toHaveBeenCalledWith( | ||
{ | ||
cacheable: true, | ||
data: { | ||
requests: [ | ||
{ | ||
indexName: 'products', | ||
model: 'trending-facets', | ||
facetName: 'company', | ||
threshold: 0, | ||
}, | ||
], | ||
}, | ||
method: 'POST', | ||
path: '1/indexes/*/recommendations', | ||
}, | ||
{} | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { TestSuite } from '../../../client-common/src/__tests__/TestSuite'; | ||
|
||
const recommend = new TestSuite('recommend').recommend; | ||
|
||
function createMockedClient() { | ||
const client = recommend('appId', 'apiKey'); | ||
jest.spyOn(client.transporter, 'read').mockImplementation(() => Promise.resolve()); | ||
|
||
return client; | ||
} | ||
|
||
describe('getTrendingItems', () => { | ||
test('builds the request', async () => { | ||
const client = createMockedClient(); | ||
|
||
await client.getTrendingItems( | ||
[ | ||
{ | ||
indexName: 'products', | ||
}, | ||
], | ||
{} | ||
); | ||
|
||
expect(client.transporter.read).toHaveBeenCalledTimes(1); | ||
expect(client.transporter.read).toHaveBeenCalledWith( | ||
{ | ||
cacheable: true, | ||
data: { | ||
requests: [ | ||
{ | ||
indexName: 'products', | ||
model: 'trending-items', | ||
threshold: 0, | ||
}, | ||
], | ||
}, | ||
method: 'POST', | ||
path: '1/indexes/*/recommendations', | ||
}, | ||
{} | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { MethodEnum } from '@algolia/requester-common'; | ||
|
||
import { BaseRecommendClient, TrendingFacetsQuery, WithRecommendMethods } from '../types'; | ||
|
||
type GetTrendingFacets = ( | ||
base: BaseRecommendClient | ||
) => WithRecommendMethods<BaseRecommendClient>['getTrendingFacets']; | ||
|
||
export const getTrendingFacets: GetTrendingFacets = base => { | ||
return (queries: readonly TrendingFacetsQuery[], requestOptions) => { | ||
const requests: readonly TrendingFacetsQuery[] = queries.map(query => ({ | ||
...query, | ||
model: 'trending-facets', | ||
// 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 | ||
); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { MethodEnum } from '@algolia/requester-common'; | ||
|
||
import { BaseRecommendClient, TrendingItemsQuery, WithRecommendMethods } from '../types'; | ||
|
||
type GetTrendingItems = ( | ||
base: BaseRecommendClient | ||
) => WithRecommendMethods<BaseRecommendClient>['getTrendingItems']; | ||
|
||
export const getTrendingItems: GetTrendingItems = base => { | ||
return (queries: readonly TrendingItemsQuery[], requestOptions) => { | ||
const requests: readonly TrendingItemsQuery[] = queries.map(query => ({ | ||
...query, | ||
model: 'trending-items', | ||
// 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 | ||
); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export type RecommendModel = 'related-products' | 'bought-together'; | ||
export type RecommendModel = 'related-products' | 'bought-together' | TrendingModel; | ||
export type TrendingModel = 'trending-items' | 'trending-facets'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { TrendingQuery } from './TrendingQuery'; | ||
|
||
export type TrendingFacetsQuery = Omit<TrendingQuery, 'model' | 'facetValue'>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { TrendingQuery } from './TrendingQuery'; | ||
|
||
export type TrendingItemsQuery = Omit<TrendingQuery, 'model'>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { TrendingModel } from './RecommendModel'; | ||
import { RecommendSearchOptions } from './RecommendSearchOptions'; | ||
|
||
export type TrendingQuery = { | ||
/** | ||
* The name of the target index. | ||
*/ | ||
readonly indexName: string; | ||
|
||
/** | ||
* The name of the Recommendation model to use. | ||
*/ | ||
readonly model: TrendingModel; | ||
|
||
/** | ||
* Threshold for the recommendations confidence score (between 0 and 100). Only recommendations with a greater score are returned. | ||
*/ | ||
readonly threshold?: number; | ||
|
||
/** | ||
* How many recommendations to retrieve. | ||
*/ | ||
readonly maxRecommendations?: number; | ||
|
||
/** | ||
* List of [search parameters](https://www.algolia.com/doc/api-reference/search-api-parameters/) to send. | ||
*/ | ||
readonly queryParameters?: RecommendSearchOptions; | ||
|
||
/** | ||
* List of [search parameters](https://www.algolia.com/doc/api-reference/search-api-parameters/) to send. | ||
* | ||
* Additional filters to use as fallback when there aren’t enough recommendations. | ||
*/ | ||
readonly fallbackParameters?: RecommendSearchOptions; | ||
|
||
/** | ||
* Used for trending model | ||
*/ | ||
readonly facetName?: string; | ||
|
||
/** | ||
* Used for trending model | ||
*/ | ||
readonly facetValue?: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters