Skip to content

Commit 3a38878

Browse files
committed
fix(recommend): prevent undefined threshold
1 parent 8923c4e commit 3a38878

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,40 @@ describe('getRecommendations', () => {
125125
{}
126126
);
127127
});
128+
129+
test('override `undefined` threshold with default value', async () => {
130+
const client = createMockedClient();
131+
132+
await client.getRecommendations(
133+
[
134+
{
135+
model: 'bought-together',
136+
indexName: 'products',
137+
objectID: 'B018APC4LE',
138+
threshold: undefined,
139+
},
140+
],
141+
{}
142+
);
143+
144+
expect(client.transporter.read).toHaveBeenCalledTimes(1);
145+
expect(client.transporter.read).toHaveBeenCalledWith(
146+
{
147+
cacheable: true,
148+
data: {
149+
requests: [
150+
{
151+
indexName: 'products',
152+
model: 'bought-together',
153+
objectID: 'B018APC4LE',
154+
threshold: 0,
155+
},
156+
],
157+
},
158+
method: 'POST',
159+
path: '1/indexes/*/recommendations',
160+
},
161+
{}
162+
);
163+
});
128164
});

packages/recommend/src/methods/getRecommendations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ type GetRecommendations = (
99
export const getRecommendations: GetRecommendations = base => {
1010
return (queries: readonly RecommendationsQuery[], requestOptions) => {
1111
const requests: readonly RecommendationsQuery[] = queries.map(query => ({
12+
...query,
1213
// The `threshold` param is required by the endpoint to make it easier
1314
// to provide a default value later, so we default it in the client
1415
// so that users don't have to provide a value.
15-
threshold: 0,
16-
...query,
16+
threshold: query.threshold || 0,
1717
}));
1818

1919
return base.transporter.read(

0 commit comments

Comments
 (0)