Skip to content

Commit 8089dc5

Browse files
fix[gen2][fetchOneEntry]: ENG-9110 make fetchOneEntry() handle options.locale just like builder.get() handles options.locale (BuilderIO#4114)
## Description FetchEntries - includes extra userAttribute: {locale: value} along with the locale payload `builder.getAll()` hits the Content API without the addition of `locale` to `userAttributes`. See here: https://www.loom.com/share/7117e8725b69400f98e02d823a901b7a The change in this [PR](BuilderIO#3719) was added to ensure `locale` is added to `userAttributes` for `builder.get()` and not `builder.getAll()` However, to ensure the same outcome In Gen2 SDK the [change](https://github.com/BuilderIO/builder/pull/3719/files#diff-b4fb78a40f76e70808858993da491c522b4f567feefeb6c01b0ab5defac58569) of adding `locale` to `userAttributes` is added in `packages/sdks/src/functions/get-content/generate-content-url.ts` which is used in **BOTH** `fetchOneEntry()` and `fetchEntries()`. **PR that caused the bug** BuilderIO#3719 **Link to JIRA ticket** https://builder-io.atlassian.net/browse/ENG-9110 **Loom** https://www.loom.com/share/a5d6f20989c24811adc35545e92ea315
1 parent b2b79ee commit 8089dc5

File tree

7 files changed

+476
-12
lines changed

7 files changed

+476
-12
lines changed

.changeset/tough-geckos-argue.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"@builder.io/sdk-angular": patch
3+
"@builder.io/sdk-react-nextjs": patch
4+
"@builder.io/sdk-qwik": patch
5+
"@builder.io/sdk-react": patch
6+
"@builder.io/sdk-react-native": patch
7+
"@builder.io/sdk-solid": patch
8+
"@builder.io/sdk-svelte": patch
9+
"@builder.io/sdk-vue": patch
10+
---
11+
12+
Fix: corrected the implementation of locale handling for fetchEntries()

packages/core/src/builder.class.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,20 @@ describe('get', () => {
11951195
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
11961196
);
11971197
});
1198+
1199+
test('hits content API with locale=en-IN added to the locale query param and the userAttributes.locale param', async () => {
1200+
const expectedModel = 'page';
1201+
const expectedLocale = 'en-IN';
1202+
1203+
builder.apiEndpoint = 'content';
1204+
await builder.get(expectedModel, { locale: expectedLocale });
1205+
1206+
expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
1207+
expect(builder['makeFetchApiCall']).toBeCalledWith(
1208+
`https://cdn.builder.io/api/v3/content/page?omit=meta.componentsUsed&apiKey=${API_KEY}&locale=${expectedLocale}&noTraverse=false&userAttributes=%7B%22locale%22%3A%22${expectedLocale}%22%7D&includeRefs=true&model=%22${expectedModel}%22`,
1209+
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
1210+
);
1211+
});
11981212
});
11991213

12001214
describe('getAll', () => {
@@ -1264,4 +1278,18 @@ describe('getAll', () => {
12641278
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
12651279
);
12661280
});
1281+
1282+
test('hits content API with locale=en-IN added to the locale query param and NOT the userAttributes.locale param', async () => {
1283+
const expectedModel = 'page';
1284+
const expectedLocale = 'en-IN';
1285+
1286+
builder.apiEndpoint = 'content';
1287+
await builder.getAll(expectedModel, { locale: expectedLocale });
1288+
1289+
expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
1290+
expect(builder['makeFetchApiCall']).toBeCalledWith(
1291+
`https://cdn.builder.io/api/v3/content/page?omit=meta.componentsUsed&apiKey=${API_KEY}&locale=${expectedLocale}&noTraverse=true&userAttributes=%7B%22urlPath%22%3A%22%2F%22%2C%22host%22%3A%22localhost%22%2C%22device%22%3A%22desktop%22%7D&includeRefs=true&limit=30&model=%22${expectedModel}%22`,
1292+
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
1293+
);
1294+
});
12671295
});

packages/sdks/src/functions/get-content/__snapshots__/generate-content-url.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
exports[`Generate Content URL > Handles overrides correctly 1`] = `"https://cdn.builder.io/api/v3/content/page?apiKey=YJIGb4i01jvw0SRdL5Bt&limit=30&noTraverse=true&includeRefs=true&omit=meta.componentsUsed&cachebust=true&noCache=true&overrides.037948e52eaf4743afed464f02c70da4=037948e52eaf4743afed464f02c70da4&overrides.page=037948e52eaf4743afed464f02c70da4&overrides.page%3A%2F=037948e52eaf4743afed464f02c70da4&preview=page&query.id=%22c1b81bab59704599b997574eb0736def%22"`;
44

5-
exports[`Generate Content URL > add options.locale in userAttributes when no locale attribute set 1`] = `"https://cdn.builder.io/api/v3/content/page?apiKey=YJIGb4i01jvw0SRdL5Bt&limit=30&noTraverse=true&includeRefs=true&locale=en-US&omit=meta.componentsUsed&userAttributes=%7B%22locale%22%3A%22en-US%22%7D"`;
5+
exports[`Generate Content URL > does not add options.locale in userAttributes when no locale attribute set 1`] = `"https://cdn.builder.io/api/v3/content/page?apiKey=YJIGb4i01jvw0SRdL5Bt&limit=30&noTraverse=true&includeRefs=true&omit=meta.componentsUsed&userAttributes=%7B%22locale%22%3A%22en-US%22%7D"`;
66

7-
exports[`Generate Content URL > add userAttributes.locale when top-level locale option exist 1`] = `"https://cdn.builder.io/api/v3/content/page?apiKey=YJIGb4i01jvw0SRdL5Bt&limit=30&noTraverse=true&includeRefs=true&locale=en-US&omit=meta.componentsUsed&userAttributes=%7B%22locale%22%3A%22en-US%22%7D"`;
7+
exports[`Generate Content URL > does not add userAttributes.locale when top-level locale option exist 1`] = `"https://cdn.builder.io/api/v3/content/page?apiKey=YJIGb4i01jvw0SRdL5Bt&limit=30&noTraverse=true&includeRefs=true&locale=en-US&omit=meta.componentsUsed"`;
88

99
exports[`Generate Content URL > generate content url when given invalid values of offset, includeUnpublished, cacheSeconds, staleCacheSeconds 1`] = `"https://cdn.builder.io/api/v3/content/page?apiKey=YJIGb4i01jvw0SRdL5Bt&limit=30&noTraverse=true&includeRefs=true&omit=meta.componentsUsed&includeUnpublished=false"`;
1010

packages/sdks/src/functions/get-content/generate-content-url.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ describe('Generate Content URL', () => {
237237
expect(output).toMatchSnapshot();
238238
});
239239

240-
test('add userAttributes.locale when top-level locale option exist', () => {
240+
test('does not add userAttributes.locale when top-level locale option exist', () => {
241241
const output = generateContentUrl({
242242
apiKey: testKey,
243243
model: testModel,
@@ -246,7 +246,7 @@ describe('Generate Content URL', () => {
246246
expect(output).toMatchSnapshot();
247247
});
248248

249-
test('add options.locale in userAttributes when no locale attribute set', () => {
249+
test('does not add options.locale in userAttributes when no locale attribute set', () => {
250250
const output = generateContentUrl({
251251
apiKey: testKey,
252252
model: testModel,

packages/sdks/src/functions/get-content/generate-content-url.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,12 @@ export const generateContentUrl = (options: GetContentOptions): URL => {
5252
url.searchParams.set('limit', String(limit));
5353
url.searchParams.set('noTraverse', String(noTraverse));
5454
url.searchParams.set('includeRefs', String(true));
55+
if (locale) {
56+
url.searchParams.set('locale', locale);
57+
}
5558

56-
const finalLocale = locale || userAttributes?.locale;
5759
let finalUserAttributes: Record<string, any> = userAttributes || {};
5860

59-
if (finalLocale) {
60-
url.searchParams.set('locale', finalLocale);
61-
finalUserAttributes = {
62-
locale: finalLocale,
63-
...finalUserAttributes,
64-
};
65-
}
6661
if (enrich) url.searchParams.set('enrich', String(enrich));
6762

6863
url.searchParams.set('omit', omit ?? 'meta.componentsUsed');

0 commit comments

Comments
 (0)