Skip to content

Commit 4d8b25d

Browse files
authored
feat: fetch objects on the client side (#43)
1 parent d72e1b6 commit 4d8b25d

File tree

7 files changed

+51
-2
lines changed

7 files changed

+51
-2
lines changed

bundlesize.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"files": [
33
{
44
"path": "packages/generative-experiences-api-client/dist/index.js",
5-
"maxSize": "27.46 kB"
5+
"maxSize": "27.57 kB"
66
},
77
{
88
"path": "packages/generative-experiences-api-client/dist/index.umd.js",
9-
"maxSize": "11.43 kB"
9+
"maxSize": "11.53 kB"
1010
},
1111
{
1212
"path": "packages/generative-experiences-vdom/dist/index.js",

packages/generative-experiences-api-client/src/client.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { algoliasearch } from 'algoliasearch';
22
import type { FacetFilters } from 'algoliasearch';
33
import type { PlainSearchParameters } from 'algoliasearch-helper';
44

5+
import { getObjectIDs, getObjects } from './helpers/records';
56
import {
67
GenerationSource,
78
ProductsComparisonOptions,
@@ -277,9 +278,30 @@ export function createClient(opts: CreateClientOptions) {
277278
});
278279

279280
const record = res?.hits?.at(0);
281+
280282
if (record?.content) {
283+
/**
284+
* For backward compatibility, we need to ensure that the objectIDs array is filled.
285+
* This can be dropped at the next major release.
286+
*/
287+
if (Boolean(record.objects?.length) && !record.objectIDs?.length) {
288+
record.objectIDs = getObjectIDs(record.objects);
289+
}
290+
291+
/**
292+
* Fetch records from objectIDs, this used to be done on the API side. But the records could get too large, so this logic is now done on the client side.
293+
*/
294+
if (Boolean(record.objectIDs?.length) && !record.objects?.length) {
295+
record.objects = await getObjects(
296+
record.objectIDs,
297+
this.options.indexName,
298+
searchClient
299+
);
300+
}
301+
281302
return record;
282303
}
304+
283305
return null;
284306
},
285307

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Hit } from '@algolia/client-search';
2+
import type { Algoliasearch } from 'algoliasearch';
3+
4+
export const getObjects = async (
5+
objectIDs: Array<Hit['objectID']>,
6+
indexName: string,
7+
searchClient: Algoliasearch
8+
): Promise<Hit[]> => {
9+
const response = await searchClient.getObjects<Hit>({
10+
requests: objectIDs.map((objectID) => ({
11+
indexName,
12+
objectID,
13+
attributesToRetrieve: ['*'],
14+
})),
15+
});
16+
17+
return response.results;
18+
};
19+
20+
export const getObjectIDs = (objects: Hit[]): Array<Hit['objectID']> =>
21+
objects.map((object) => object.objectID);

packages/generative-experiences-api-client/src/types/shopping-guide.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type CategoryGuide = BaseShoppingGuide & {
1313
description: string;
1414
category: string;
1515
objects: Hit[];
16+
objectIDs: string[];
1617
content: Array<{
1718
type: 'conclusion' | 'factor' | 'introduction';
1819
title: string;
@@ -26,13 +27,15 @@ export type ShoppingGuideType = BaseShoppingGuide & {
2627
description: string;
2728
category: string;
2829
objects: Hit[];
30+
objectIDs: string[];
2931
content: Array<{ title: string; content: string }>;
3032
score_headline: number;
3133
};
3234

3335
export type ComparisonGuide = BaseShoppingGuide & {
3436
type: 'comparison';
3537
objects: Hit[];
38+
objectIDs: string[];
3639
content: Array<{
3740
title: string;
3841
type: 'conclusion' | 'introduction' | 'product';

packages/generative-experiences-js/src/hooks/useShoppingGuidesContent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const defaultState: ShoppingGuideType = {
1717
description: '',
1818
category: '',
1919
objects: [],
20+
objectIDs: [],
2021
content: [
2122
{
2223
title: '',

packages/generative-experiences-react/src/useShoppingGuidesContent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const defaultState: ShoppingGuideType = {
1717
description: '',
1818
category: '',
1919
objects: [],
20+
objectIDs: [],
2021
content: [
2122
{
2223
title: '',

packages/generative-experiences-vdom/src/types/ShoppingGuideContentProps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const defaultState: ShoppingGuideType = {
1414
description: '',
1515
category: '',
1616
objects: [],
17+
objectIDs: [],
1718
content: [
1819
{
1920
title: '',

0 commit comments

Comments
 (0)