Skip to content

Commit 80f79a2

Browse files
authored
Normalize empty preferred locale (#324)
1 parent d8c5030 commit 80f79a2

File tree

4 files changed

+109
-9
lines changed

4 files changed

+109
-9
lines changed

src/api/fetchContent.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ export function fetchContent<I extends VersionedSlotId, C extends JsonObject>(
4646
const {apiKey, appId, fallback, baseEndpointUrl, logger, ...fetchOptions} = options ?? {};
4747
const auth = {appId: appId, apiKey: apiKey};
4848
const [id, version = 'latest'] = slotId.split('@') as [I, `${number}` | 'latest' | undefined];
49+
const preferredLocale = options?.preferredLocale !== undefined && options.preferredLocale !== ''
50+
? options.preferredLocale
51+
: undefined;
4952

5053
const promise = (new ContentFetcher({...auth, baseEndpointUrl: baseEndpointUrl}))
51-
.fetch<SlotContent<I, C>>(
52-
id,
53-
version === 'latest'
54-
? fetchOptions
55-
: {...fetchOptions, version: version},
56-
);
54+
.fetch<SlotContent<I, C>>(id, {
55+
...fetchOptions,
56+
preferredLocale: preferredLocale,
57+
version: version === 'latest' ? undefined : version,
58+
});
5759

5860
return promise.catch(
5961
async error => {
@@ -65,7 +67,7 @@ export function fetchContent<I extends VersionedSlotId, C extends JsonObject>(
6567
return {content: fallback};
6668
}
6769

68-
const staticContent = await loadSlotContent(id, (fetchOptions as DynamicContentOptions).preferredLocale);
70+
const staticContent = await loadSlotContent(id, preferredLocale);
6971

7072
if (staticContent === null) {
7173
throw error;

src/plug.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ export class GlobalPlug implements Plug {
149149

150150
const {plugins, test, ...sdkConfiguration} = configuration;
151151

152+
if (sdkConfiguration.defaultPreferredLocale === '') {
153+
delete sdkConfiguration.defaultPreferredLocale;
154+
}
155+
152156
const sdk = SdkFacade.init({
153157
...sdkConfiguration,
154158
appId: appId,
@@ -383,15 +387,22 @@ export class GlobalPlug implements Plug {
383387
): Promise<FetchResponse<I>> {
384388
const [id, version = 'latest'] = slotId.split('@') as [string, `${number}` | 'latest' | undefined];
385389
const logger = this.sdk.getLogger();
390+
const preferredLocale = options.preferredLocale !== undefined && options.preferredLocale !== ''
391+
? options.preferredLocale
392+
: undefined;
386393

387394
return this.sdk
388395
.contentFetcher
389-
.fetch<SlotContent<I>>(id, version === 'latest' ? options : {...options, version: version})
396+
.fetch<SlotContent<I>>(id, {
397+
...options,
398+
preferredLocale: preferredLocale,
399+
version: version === 'latest' ? undefined : version,
400+
})
390401
.catch(async error => {
391402
logger.error(`Failed to fetch content for slot "${id}@${version}": ${formatCause(error)}`);
392403

393404
const resolvedFallback = fallback === undefined
394-
? (await loadSlotContent(slotId, options.preferredLocale) as SlotContent<I>|null ?? undefined)
405+
? (await loadSlotContent(slotId, preferredLocale) as SlotContent<I>|null ?? undefined)
395406
: fallback;
396407

397408
if (resolvedFallback === undefined) {

test/api/fetchContent.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,25 @@ describe('fetchContent', () => {
308308

309309
expect(logger.error).toHaveBeenCalledWith('Failed to fetch content for slot "test@latest": reason');
310310
});
311+
312+
it('should normalize an empty preferred locale to undefined', async () => {
313+
const options: FetchOptions = {
314+
apiKey: apiKey,
315+
preferredLocale: '',
316+
};
317+
318+
const error = new Error('Reason');
319+
320+
jest.mocked(mockFetch).mockRejectedValue(error);
321+
322+
jest.mocked(loadSlotContent).mockResolvedValue(null);
323+
324+
await expect(fetchContent('test', options)).rejects.toBe(error);
325+
326+
expect(loadSlotContent).toHaveBeenCalledWith('test', undefined);
327+
328+
expect(mockFetch).toHaveBeenCalledWith('test', {
329+
preferredLocale: undefined,
330+
});
331+
});
311332
});

test/plug.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,33 @@ describe('The Croct plug', () => {
157157
expect(initialize).toHaveBeenCalledWith(config);
158158
});
159159

160+
it('should normalize an empty default preferred locale to undefined', () => {
161+
const config: SdkFacadeConfiguration = {
162+
appId: APP_ID,
163+
track: false,
164+
debug: false,
165+
test: true,
166+
tokenScope: 'isolated',
167+
userId: 'c4r0l',
168+
eventMetadata: {
169+
foo: 'bar',
170+
},
171+
};
172+
173+
const sdkFacade = SdkFacade.init(config);
174+
const initialize = jest.spyOn(SdkFacade, 'init').mockReturnValue(sdkFacade);
175+
176+
croct.plug({
177+
...config,
178+
defaultPreferredLocale: '',
179+
});
180+
181+
expect(initialize).toHaveBeenCalledWith({
182+
...config,
183+
defaultPreferredLocale: undefined,
184+
});
185+
});
186+
160187
it.each([
161188
'test',
162189
'development',
@@ -1055,6 +1082,45 @@ describe('The Croct plug', () => {
10551082
);
10561083
});
10571084

1085+
it('should normalize an empty locale to undefined', async () => {
1086+
const logger: Logger = {
1087+
debug: jest.fn(),
1088+
info: jest.fn(),
1089+
warn: jest.fn(),
1090+
error: jest.fn(),
1091+
};
1092+
1093+
const config: SdkFacadeConfiguration = {
1094+
appId: APP_ID,
1095+
logger: logger,
1096+
};
1097+
1098+
const sdkFacade = SdkFacade.init(config);
1099+
1100+
const initialize = jest.spyOn(SdkFacade, 'init').mockReturnValue(sdkFacade);
1101+
1102+
croct.plug(config);
1103+
1104+
expect(initialize).toHaveBeenCalledWith(expect.objectContaining(config));
1105+
1106+
const error = new Error('Reason');
1107+
const fetch = jest.spyOn(sdkFacade.contentFetcher, 'fetch').mockRejectedValue(error);
1108+
1109+
const slotId = 'test';
1110+
1111+
const options: FetchOptions = {preferredLocale: ''};
1112+
1113+
jest.mocked(loadSlotContent).mockResolvedValue(null);
1114+
1115+
await expect(croct.fetch(slotId, options)).rejects.toBe(error);
1116+
1117+
expect(fetch).toHaveBeenCalledWith(slotId, {
1118+
preferredLocale: undefined,
1119+
});
1120+
1121+
expect(loadSlotContent).toHaveBeenCalledWith(slotId, undefined);
1122+
});
1123+
10581124
it('should extract the slot ID and version', async () => {
10591125
const config: SdkFacadeConfiguration = {appId: APP_ID};
10601126
const sdkFacade = SdkFacade.init(config);

0 commit comments

Comments
 (0)