Skip to content

Commit 36285b2

Browse files
authored
fix(recommend): Add Type RecommendQueriesResponse from old MultipleQueriesResponse for recommend (#1472)
1 parent a38a628 commit 36285b2

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

packages/recommend/src/__tests__/getRecommendations.test.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@ import { TestSuite } from '../../../client-common/src/__tests__/TestSuite';
22

33
const recommend = new TestSuite('recommend').recommend;
44

5-
function createMockedClient() {
5+
function createMockedClient<TObject>() {
66
const client = recommend('appId', 'apiKey');
7-
jest.spyOn(client.transporter, 'read').mockImplementation(() => Promise.resolve());
7+
jest.spyOn(client.transporter, 'read').mockImplementation(() =>
8+
Promise.resolve({
9+
results: [
10+
{
11+
hits: [
12+
{
13+
objectID: '1',
14+
},
15+
],
16+
},
17+
],
18+
})
19+
);
820

921
return client;
1022
}
@@ -197,4 +209,25 @@ describe('getRecommendations', () => {
197209
{}
198210
);
199211
});
212+
213+
test('returns recommendations results', async () => {
214+
const client = createMockedClient();
215+
216+
const recommendations = await client.getRecommendations<any>(
217+
[
218+
{
219+
model: 'bought-together',
220+
indexName: 'products',
221+
objectID: 'B018APC4LE',
222+
},
223+
],
224+
{}
225+
);
226+
227+
expect(recommendations.results[0].hits).toEqual([
228+
{
229+
objectID: '1',
230+
},
231+
]);
232+
});
200233
});
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MultipleQueriesResponse, SearchOptions } from '@algolia/client-search';
1+
import { SearchOptions, SearchResponse } from '@algolia/client-search';
22
import { RequestOptions } from '@algolia/transporter';
33

44
import { FrequentlyBoughtTogetherQuery } from './FrequentlyBoughtTogetherQuery';
@@ -9,52 +9,59 @@ import { TrendingFacetsQuery } from './TrendingFacetsQuery';
99
import { TrendingItemsQuery } from './TrendingItemsQuery';
1010
import { TrendingQuery } from './TrendingQuery';
1111

12+
export type RecommendQueriesResponse<TObject> = {
13+
/**
14+
* The list of results.
15+
*/
16+
readonly results: ReadonlyArray<SearchResponse<TObject>>;
17+
};
18+
1219
export type WithRecommendMethods<TType> = TType & {
1320
/**
1421
* Returns recommendations.
1522
*/
1623
readonly getRecommendations: <TObject>(
1724
queries: ReadonlyArray<RecommendationsQuery | TrendingQuery>,
1825
requestOptions?: RequestOptions & SearchOptions
19-
) => Readonly<Promise<MultipleQueriesResponse<TObject>>>;
26+
) => Readonly<Promise<RecommendQueriesResponse<TObject>>>;
2027

2128
/**
2229
* Returns [Related Products](https://algolia.com/doc/guides/algolia-ai/recommend/#related-products).
2330
*/
2431
readonly getRelatedProducts: <TObject>(
2532
queries: readonly RelatedProductsQuery[],
2633
requestOptions?: RequestOptions & SearchOptions
27-
) => Readonly<Promise<MultipleQueriesResponse<TObject>>>;
34+
) => Readonly<Promise<RecommendQueriesResponse<TObject>>>;
2835

2936
/**
3037
* Returns [Frequently Bought Together](https://algolia.com/doc/guides/algolia-ai/recommend/#frequently-bought-together) products.
3138
*/
3239
readonly getFrequentlyBoughtTogether: <TObject>(
3340
queries: readonly FrequentlyBoughtTogetherQuery[],
3441
requestOptions?: RequestOptions & SearchOptions
35-
) => Readonly<Promise<MultipleQueriesResponse<TObject>>>;
42+
) => Readonly<Promise<RecommendQueriesResponse<TObject>>>;
3643

3744
/**
3845
* Returns trending items
3946
*/
4047
readonly getTrendingItems: <TObject>(
4148
queries: readonly TrendingItemsQuery[],
4249
requestOptions?: RequestOptions & SearchOptions
43-
) => Readonly<Promise<MultipleQueriesResponse<TObject>>>;
50+
) => Readonly<Promise<RecommendQueriesResponse<TObject>>>;
4451

4552
/**
4653
* Returns trending items per facet
4754
*/
4855
readonly getTrendingFacets: <TObject>(
4956
queries: readonly TrendingFacetsQuery[],
5057
requestOptions?: RequestOptions & SearchOptions
51-
) => Readonly<Promise<MultipleQueriesResponse<TObject>>>;
58+
) => Readonly<Promise<RecommendQueriesResponse<TObject>>>;
5259

5360
/**
5461
* Returns Looking Similar
5562
*/
5663
readonly getLookingSimilar: <TObject>(
5764
queries: readonly LookingSimilarQuery[],
5865
requestOptions?: RequestOptions & SearchOptions
59-
) => Readonly<Promise<MultipleQueriesResponse<TObject>>>;
66+
) => Readonly<Promise<RecommendQueriesResponse<TObject>>>;
6067
};

0 commit comments

Comments
 (0)