Skip to content

Commit

Permalink
Default preferred locale (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos authored Aug 27, 2024
1 parent 406dfbc commit a8f7eaa
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 54 deletions.
2 changes: 2 additions & 0 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type Configuration = {
eventMetadata?: {[key: string]: string},
eventProcessor?: DependencyResolver<TrackingEventProcessor>,
defaultFetchTimeout?: number,
defaultPreferredLocale?: string,
};

export class Container {
Expand Down Expand Up @@ -119,6 +120,7 @@ export class Container {
baseEndpointUrl: this.configuration.contentBaseEndpointUrl,
logger: this.getLogger('ContentFetcher'),
defaultTimeout: this.configuration.defaultFetchTimeout ?? Container.DEFAULT_FETCH_TIMEOUT,
defaultPreferredLocale: this.configuration.defaultPreferredLocale,
});
}

Expand Down
9 changes: 7 additions & 2 deletions src/contentFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ export type Configuration = {
baseEndpointUrl?: string,
logger?: Logger,
defaultTimeout?: number,
defaultPreferredLocale?: string,
};

type InternalConfiguration = {
appId?: string,
apiKey?: string,
defaultTimeout?: number,
defaultPreferredLocale?: string,
};

export class ContentFetcher {
Expand Down Expand Up @@ -117,6 +119,7 @@ export class ContentFetcher {
appId: configuration.appId,
apiKey: apiKey,
defaultTimeout: configuration.defaultTimeout,
defaultPreferredLocale: configuration.defaultPreferredLocale,
};
}

Expand Down Expand Up @@ -214,8 +217,10 @@ export class ContentFetcher {
payload.version = `${options.version}`;
}

if (options.preferredLocale !== undefined) {
payload.preferredLocale = options.preferredLocale;
const preferredLocale = options.preferredLocale ?? this.configuration.defaultPreferredLocale;

if (preferredLocale !== undefined) {
payload.preferredLocale = preferredLocale;
}

const dynamic = ContentFetcher.isDynamicContent(options);
Expand Down
13 changes: 2 additions & 11 deletions src/facade/contentFetcherFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ function validate(options: unknown): asserts options is FetchOptions {
}
}

type Options = {
preferredLocale?: string,
};

export type Configuration = Options & {
export type Configuration = {
contentFetcher: ContentFetcher,
contextFactory: ContextFactory,
previewTokenProvider: TokenProvider,
Expand All @@ -44,17 +40,12 @@ export class ContentFetcherFacade {

private readonly cidAssigner: CidAssigner;

private readonly options: Options;

public constructor(configuration: Configuration) {
this.fetcher = configuration.contentFetcher;
this.previewTokenProvider = configuration.previewTokenProvider;
this.userTokenProvider = configuration.userTokenProvider;
this.cidAssigner = configuration.cidAssigner;
this.contextFactory = configuration.contextFactory;
this.options = {
preferredLocale: configuration.preferredLocale,
};
}

public async fetch<P extends JsonObject>(slotId: string, options: FetchOptions = {}): Promise<FetchResponse<P>> {
Expand All @@ -72,7 +63,7 @@ export class ContentFetcherFacade {
version: options.version,
context: this.contextFactory.createContext(options.attributes),
timeout: options.timeout,
preferredLocale: options.preferredLocale ?? this.options.preferredLocale,
preferredLocale: options.preferredLocale,
});
}
}
18 changes: 4 additions & 14 deletions src/facade/sdkFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ import {ContentFetcherFacade} from './contentFetcherFacade';
import {CookieCacheConfiguration} from '../cache/cookieCache';
import {EventSubjectProcessor} from '../eventSubjectProcessor';

type Options = {
preferredLocale?: string,
};

export type Configuration = Options & {
export type Configuration = {
appId: string,
tokenScope?: TokenScope,
debug?: boolean,
Expand All @@ -42,6 +38,7 @@ export type Configuration = Options & {
previewToken?: CookieCacheConfiguration,
},
defaultFetchTimeout?: number,
defaultPreferredLocale?: string,
};

function validateConfiguration(configuration: unknown): asserts configuration is Configuration {
Expand All @@ -65,17 +62,14 @@ export class SdkFacade {

private contentFetcherFacade?: ContentFetcherFacade;

private readonly options: Options;

private constructor(sdk: Sdk, options: Options = {}) {
private constructor(sdk: Sdk) {
this.sdk = sdk;
this.options = options;
}

public static init(configuration: Configuration): SdkFacade {
validateConfiguration(configuration);

const {track = true, userId, token, preferredLocale, ...containerConfiguration} = configuration;
const {track = true, userId, token, ...containerConfiguration} = configuration;

if (userId !== undefined && token !== undefined) {
throw new Error('Either the user ID or token can be specified, but not both.');
Expand All @@ -90,9 +84,6 @@ export class SdkFacade {
disableCidMirroring: containerConfiguration.disableCidMirroring ?? false,
eventProcessor: container => new EventSubjectProcessor(container.getLogger('EventSubjectProcessor')),
}),
{
preferredLocale: preferredLocale,
},
);

if (userId !== undefined) {
Expand Down Expand Up @@ -185,7 +176,6 @@ export class SdkFacade {
cidAssigner: this.sdk.cidAssigner,
previewTokenProvider: this.sdk.previewTokenStore,
userTokenProvider: this.sdk.userTokenStore,
preferredLocale: this.options.preferredLocale,
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/schema/sdkFacadeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const sdkFacadeConfigurationSchema = new ObjectType({
integer: true,
minimum: 1,
}),
preferredLocale: new StringType({
defaultPreferredLocale: new StringType({
pattern: /^[a-z]{2,3}([-_][a-z]{2,3})?$/i,
}),
},
Expand Down
3 changes: 3 additions & 0 deletions src/schema/sdkSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,8 @@ export const sdkConfigurationSchema = new ObjectType({
integer: true,
minimum: 1,
}),
defaultPreferredLocale: new StringType({
pattern: /^[a-z]{2,3}([-_][a-z]{2,3})?$/i,
}),
},
});
1 change: 1 addition & 0 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type Configuration = {
},
eventProcessor?: DependencyResolver<TrackingEventProcessor>,
defaultFetchTimeout?: number,
defaultPreferredLocale?: string,
};

function validateConfiguration(configuration: unknown): asserts configuration is Configuration {
Expand Down
2 changes: 2 additions & 0 deletions test/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ describe('A container', () => {
contentBaseEndpointUrl: 'https://localtest/content',
evaluationBaseEndpointUrl: 'https://localtest/evaluate',
trackerEndpointUrl: 'https://localtest/track',
defaultFetchTimeout: 5000,
defaultPreferredLocale: 'en-us',
};

it('should provide its configuration', () => {
Expand Down
22 changes: 21 additions & 1 deletion test/contentFetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 +473,32 @@ describe('A content fetcher', () => {
await expect(promise).resolves.toEqual(content);
});

it('should fetch dynamic content for the default preferred locale', async () => {
const fetcher = new ContentFetcher({
appId: appId,
defaultPreferredLocale: 'en-us',
});

fetchMock.mock({
...requestMatcher,
body: {
...requestMatcher.body,
preferredLocale: 'en-us',
},
response: content,
});

const promise = fetcher.fetch(contentId);

await expect(promise).resolves.toEqual(content);
});

it('should fetch dynamic content for the specified preferred locale', async () => {
const fetcher = new ContentFetcher({
appId: appId,
});

const preferredLocale = 'pt_BR';
const preferredLocale = 'pt-br';

fetchMock.mock({
...requestMatcher,
Expand Down
6 changes: 1 addition & 5 deletions test/facade/contentFecherFacade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ describe('A content fetcher facade', () => {
userTokenProvider: new FixedTokenProvider(userToken),
previewTokenProvider: new FixedTokenProvider(previewToken),
contextFactory: new TabContextFactory(tab),
preferredLocale: 'pt-br',
});

const options: FetchOptions = {
Expand Down Expand Up @@ -131,10 +130,7 @@ describe('A content fetcher facade', () => {
attributes: options?.context?.attributes,
});

expect(fetcher.fetch).toHaveBeenNthCalledWith(1, slotId, {
...options,
preferredLocale: 'pt-br',
});
expect(fetcher.fetch).toHaveBeenNthCalledWith(1, slotId, options);

await fetcherFacade.fetch(slotId, {
timeout: options.timeout,
Expand Down
21 changes: 15 additions & 6 deletions test/facade/sdkFacade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe('A SDK facade', () => {
logger: logger,
urlSanitizer: urlSanitizer,
defaultFetchTimeout: 5,
defaultPreferredLocale: 'en-us',
});

expect(initialize).toHaveBeenCalledWith(
Expand All @@ -134,6 +135,7 @@ describe('A SDK facade', () => {
urlSanitizer: urlSanitizer,
eventProcessor: expect.any(Function),
defaultFetchTimeout: 5,
defaultPreferredLocale: 'en-us',
} satisfies ResolvedConfiguration,
);
});
Expand Down Expand Up @@ -392,21 +394,28 @@ describe('A SDK facade', () => {
const sdkFacade = SdkFacade.init({
appId: appId,
track: false,
preferredLocale: preferredLocale,
});

const options: FetchOptions = {
timeout: 5,
};

const expectedOptions: FetchOptions = {
...options,
version: '1',
preferredLocale: preferredLocale,
attributes: {
foo: 'bar',
},
};

await expect(sdkFacade.contentFetcher.fetch(slotId, options)).resolves.toBe(result);

expect(fetcher.fetch).toHaveBeenCalledWith(slotId, expect.objectContaining(expectedOptions));
expect(fetcher.fetch).toHaveBeenCalledWith(slotId, expect.objectContaining({
timeout: options.timeout,
version: options.version,
preferredLocale: preferredLocale,
context: expect.objectContaining({
attributes: options.attributes,
}),
}));

expect(fetcher.fetch).toHaveBeenCalledTimes(1);
});

Expand Down
28 changes: 14 additions & 14 deletions test/schemas/sdkFacadeSchemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ describe('The SDK facade configuration schema', () => {
}],
[{
appId: '7e9d59a9-e4b3-45d4-b1c7-48287f1e5e8a',
preferredLocale: 'pt',
defaultPreferredLocale: 'pt',
}],
[{
appId: '7e9d59a9-e4b3-45d4-b1c7-48287f1e5e8a',
preferredLocale: 'pt_br',
defaultPreferredLocale: 'pt_br',
}],
[{
appId: '7e9d59a9-e4b3-45d4-b1c7-48287f1e5e8a',
preferredLocale: 'pt_BR',
defaultPreferredLocale: 'pt_BR',
}],
[{
appId: '7e9d59a9-e4b3-45d4-b1c7-48287f1e5e8a',
preferredLocale: 'pt-br',
defaultPreferredLocale: 'pt-br',
}],
[{
appId: '7e9d59a9-e4b3-45d4-b1c7-48287f1e5e8a',
preferredLocale: 'pt-BR',
defaultPreferredLocale: 'pt-BR',
}],
[{
appId: '7e9d59a9-e4b3-45d4-b1c7-48287f1e5e8a',
preferredLocale: 'abc_cde',
defaultPreferredLocale: 'abc_cde',
}],
[{
appId: '7e9d59a9-e4b3-45d4-b1c7-48287f1e5e8a',
Expand Down Expand Up @@ -195,30 +195,30 @@ describe('The SDK facade configuration schema', () => {
[
{
appId: '7e9d59a9-e4b3-45d4-b1c7-48287f1e5e8a',
preferredLocale: '',
defaultPreferredLocale: '',
},
"Invalid format at path '/preferredLocale'.",
"Invalid format at path '/defaultPreferredLocale'.",
],
[
{
appId: '7e9d59a9-e4b3-45d4-b1c7-48287f1e5e8a',
preferredLocale: 'fooo',
defaultPreferredLocale: 'fooo',
},
'Invalid format at path \'/preferredLocale\'.',
'Invalid format at path \'/defaultPreferredLocale\'.',
],
[
{
appId: '7e9d59a9-e4b3-45d4-b1c7-48287f1e5e8a',
preferredLocale: 'foo-baar',
defaultPreferredLocale: 'foo-baar',
},
'Invalid format at path \'/preferredLocale\'.',
'Invalid format at path \'/defaultPreferredLocale\'.',
],
[
{
appId: '7e9d59a9-e4b3-45d4-b1c7-48287f1e5e8a',
preferredLocale: 'foo_baar',
defaultPreferredLocale: 'foo_baar',
},
'Invalid format at path \'/preferredLocale\'.',
'Invalid format at path \'/defaultPreferredLocale\'.',
],
[
{
Expand Down
Loading

0 comments on commit a8f7eaa

Please sign in to comment.