Skip to content

Commit 5cf64dd

Browse files
feat(recommend): support multiple queries (#1283)
1 parent 97ebde6 commit 5cf64dd

File tree

7 files changed

+109
-55
lines changed

7 files changed

+109
-55
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ describe('getFrequentlyBoughtTogether', () => {
1414
const client = createMockedClient();
1515

1616
await client.getFrequentlyBoughtTogether(
17-
{
18-
indexName: 'products',
19-
objectID: 'B018APC4LE',
20-
},
17+
[
18+
{
19+
indexName: 'products',
20+
objectID: 'B018APC4LE',
21+
},
22+
],
2123
{}
2224
);
2325

@@ -46,15 +48,17 @@ describe('getFrequentlyBoughtTogether', () => {
4648
test('ignores `fallbackParameters`', async () => {
4749
const client = createMockedClient();
4850

49-
await client.getFrequentlyBoughtTogether({
50-
// @ts-ignore `fallbackParameters` are not supposed to be passed
51-
// according to the types
52-
fallbackParameters: {
53-
facetFilters: [],
51+
await client.getFrequentlyBoughtTogether([
52+
{
53+
// @ts-ignore `fallbackParameters` are not supposed to be passed
54+
// according to the types
55+
fallbackParameters: {
56+
facetFilters: [],
57+
},
58+
indexName: 'products',
59+
objectID: 'B018APC4LE',
5460
},
55-
indexName: 'products',
56-
objectID: 'B018APC4LE',
57-
});
61+
]);
5862

5963
expect(client.transporter.read).toHaveBeenCalledTimes(1);
6064
expect(client.transporter.read).toHaveBeenCalledWith(

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

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ describe('getRecommendations', () => {
1414
const client = createMockedClient();
1515

1616
await client.getRecommendations(
17-
{
18-
model: 'bought-together',
19-
indexName: 'products',
20-
objectID: 'B018APC4LE',
21-
},
17+
[
18+
{
19+
model: 'bought-together',
20+
indexName: 'products',
21+
objectID: 'B018APC4LE',
22+
},
23+
],
2224
{}
2325
);
2426

@@ -47,13 +49,55 @@ describe('getRecommendations', () => {
4749
const client = createMockedClient();
4850

4951
await client.getRecommendations(
52+
[
53+
{
54+
model: 'related-products',
55+
indexName: 'products',
56+
objectID: 'B018APC4LE',
57+
},
58+
],
59+
{}
60+
);
61+
62+
expect(client.transporter.read).toHaveBeenCalledTimes(1);
63+
expect(client.transporter.read).toHaveBeenCalledWith(
5064
{
51-
model: 'related-products',
52-
indexName: 'products',
53-
objectID: 'B018APC4LE',
65+
cacheable: true,
66+
data: {
67+
requests: [
68+
{
69+
indexName: 'products',
70+
model: 'related-products',
71+
objectID: 'B018APC4LE',
72+
threshold: 0,
73+
},
74+
],
75+
},
76+
method: 'POST',
77+
path: '1/indexes/*/recommendations',
5478
},
5579
{}
5680
);
81+
});
82+
83+
test('builds multiple requests', async () => {
84+
const client = createMockedClient();
85+
86+
await client.getRecommendations(
87+
[
88+
{
89+
model: 'related-products',
90+
indexName: 'products',
91+
objectID: 'B018APC4LE-1',
92+
},
93+
{
94+
model: 'related-products',
95+
indexName: 'products',
96+
objectID: 'B018APC4LE-2',
97+
},
98+
],
99+
{}
100+
);
57101

58102
expect(client.transporter.read).toHaveBeenCalledTimes(1);
59103
expect(client.transporter.read).toHaveBeenCalledWith(
@@ -64,7 +108,13 @@ describe('getRecommendations', () => {
64108
{
65109
indexName: 'products',
66110
model: 'related-products',
67-
objectID: 'B018APC4LE',
111+
objectID: 'B018APC4LE-1',
112+
threshold: 0,
113+
},
114+
{
115+
indexName: 'products',
116+
model: 'related-products',
117+
objectID: 'B018APC4LE-2',
68118
threshold: 0,
69119
},
70120
],

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ describe('getRelatedProducts', () => {
1414
const client = createMockedClient();
1515

1616
await client.getRelatedProducts(
17-
{
18-
indexName: 'products',
19-
objectID: 'B018APC4LE',
20-
},
17+
[
18+
{
19+
indexName: 'products',
20+
objectID: 'B018APC4LE',
21+
},
22+
],
2123
{}
2224
);
2325

packages/recommend/src/methods/getFrequentlyBoughtTogether.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { RecommendClient, WithRecommendMethods } from '../types';
2-
import { getRecommendations, GetRecommendationsOptions } from './getRecommendations';
2+
import { getRecommendations, GetRecommendationsQuery } from './getRecommendations';
33

4-
export type GetFrequentlyBoughtTogetherOptions = Omit<
5-
GetRecommendationsOptions,
4+
export type GetFrequentlyBoughtTogetherQuery = Omit<
5+
GetRecommendationsQuery,
66
'model' | 'fallbackParameters'
77
>;
88

@@ -11,13 +11,13 @@ type GetFrequentlyBoughtTogether = (
1111
) => WithRecommendMethods<RecommendClient>['getFrequentlyBoughtTogether'];
1212

1313
export const getFrequentlyBoughtTogether: GetFrequentlyBoughtTogether = base => {
14-
return (options, requestOptions) => {
14+
return (queries, requestOptions) => {
1515
return getRecommendations(base)(
16-
{
17-
...options,
16+
queries.map(query => ({
17+
...query,
1818
fallbackParameters: {},
1919
model: 'bought-together',
20-
},
20+
})),
2121
requestOptions
2222
);
2323
};

packages/recommend/src/methods/getRecommendations.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
WithRecommendMethods,
88
} from '../types';
99

10-
export type GetRecommendationsOptions = {
10+
export type GetRecommendationsQuery = {
1111
readonly indexName: string;
1212
readonly model: RecommendModel;
1313
readonly objectID: string;
@@ -22,16 +22,14 @@ type GetRecommendations = (
2222
) => WithRecommendMethods<RecommendClient>['getRecommendations'];
2323

2424
export const getRecommendations: GetRecommendations = base => {
25-
return (options, requestOptions) => {
26-
const requests: readonly GetRecommendationsOptions[] = [
27-
{
28-
// The `threshold` param is required by the endpoint to make it easier
29-
// to provide a default value later, so we default it in the client
30-
// so that users don't have to provide a value.
31-
threshold: 0,
32-
...options,
33-
},
34-
];
25+
return (queries, requestOptions) => {
26+
const requests: readonly GetRecommendationsQuery[] = queries.map(query => ({
27+
// The `threshold` param is required by the endpoint to make it easier
28+
// to provide a default value later, so we default it in the client
29+
// so that users don't have to provide a value.
30+
threshold: 0,
31+
...query,
32+
}));
3533

3634
return base.transporter.read(
3735
{

packages/recommend/src/methods/getRelatedProducts.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { RecommendClient, WithRecommendMethods } from '../types';
2-
import { getRecommendations, GetRecommendationsOptions } from './getRecommendations';
2+
import { getRecommendations, GetRecommendationsQuery } from './getRecommendations';
33

4-
export type GetRelatedProductsOptions = Omit<GetRecommendationsOptions, 'model'>;
4+
export type GetRelatedProductsQuery = Omit<GetRecommendationsQuery, 'model'>;
55

66
type GetRelatedProducts = (
77
base: RecommendClient
88
) => WithRecommendMethods<RecommendClient>['getRelatedProducts'];
99

1010
export const getRelatedProducts: GetRelatedProducts = base => {
11-
return (options, requestOptions) => {
11+
return (queries, requestOptions) => {
1212
return getRecommendations(base)(
13-
{
14-
...options,
13+
queries.map(query => ({
14+
...query,
1515
model: 'related-products',
16-
},
16+
})),
1717
requestOptions
1818
);
1919
};

packages/recommend/src/types/WithRecommendMethods.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@ import { MultipleQueriesResponse, SearchOptions } from '@algolia/client-search';
22
import { RequestOptions } from '@algolia/transporter';
33

44
import {
5-
GetFrequentlyBoughtTogetherOptions,
6-
GetRecommendationsOptions,
7-
GetRelatedProductsOptions,
5+
GetFrequentlyBoughtTogetherQuery,
6+
GetRecommendationsQuery,
7+
GetRelatedProductsQuery,
88
} from '../methods';
99

1010
export type WithRecommendMethods<TType> = TType & {
1111
/**
1212
* Returns recommendations.
1313
*/
1414
readonly getRecommendations: <TObject>(
15-
options: GetRecommendationsOptions,
15+
queries: readonly GetRecommendationsQuery[],
1616
requestOptions?: RequestOptions & SearchOptions
1717
) => Readonly<Promise<MultipleQueriesResponse<TObject>>>;
1818

1919
/**
2020
* Returns [Related Products](https://algolia.com/doc/guides/algolia-ai/recommend/#related-products).
2121
*/
2222
readonly getRelatedProducts: <TObject>(
23-
options: GetRelatedProductsOptions,
23+
queries: readonly GetRelatedProductsQuery[],
2424
requestOptions?: RequestOptions & SearchOptions
2525
) => Readonly<Promise<MultipleQueriesResponse<TObject>>>;
2626

2727
/**
2828
* Returns [Frequently Bought Together](https://algolia.com/doc/guides/algolia-ai/recommend/#frequently-bought-together) products.
2929
*/
3030
readonly getFrequentlyBoughtTogether: <TObject>(
31-
options: GetFrequentlyBoughtTogetherOptions,
31+
queries: readonly GetFrequentlyBoughtTogetherQuery[],
3232
requestOptions?: RequestOptions & SearchOptions
3333
) => Readonly<Promise<MultipleQueriesResponse<TObject>>>;
3434
};

0 commit comments

Comments
 (0)