Skip to content

Commit

Permalink
feat: Make mode option deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 committed Apr 19, 2022
1 parent 1dd6204 commit 7ecaf26
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 39 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/Properties.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TolgeeConfig } from './TolgeeConfig';
import { Scope } from './types';
import { Mode, Scope } from './types';

const PREFERRED_LANGUAGES_LOCAL_STORAGE_KEY = '__tolgee_preferredLanguages';
const CURRENT_LANGUAGE_LOCAL_STORAGE_KEY = '__tolgee_currentLanguage';
Expand All @@ -9,6 +9,7 @@ export class Properties {
scopes?: Scope[];
projectId?: number;
permittedLanguageIds?: number[];
mode?: Mode;
_currentLanguage?: string;

get currentLanguage(): string {
Expand Down
36 changes: 18 additions & 18 deletions packages/core/src/Tolgee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('Tolgee', () => {
});

test('will load api key details', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
propertiesMock.mock.instances[0].mode = 'development';
await tolgee.run();
expect(
coreServiceMock.mock.instances[0].loadApiKeyDetails
Expand All @@ -59,7 +59,7 @@ describe('Tolgee', () => {
});

test('will not set properties.scopes on run in production mode', async () => {
propertiesMock.mock.instances[0].config.mode = 'production';
propertiesMock.mock.instances[0].mode = 'production';
await tolgee.run();
expect(coreServiceMock.mock.instances[0].getApiKeyDetails).toBeCalledTimes(
0
Expand Down Expand Up @@ -149,7 +149,7 @@ describe('Tolgee', () => {

describe('async translate', () => {
test('will return wrapped string from text service in development mode', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
propertiesMock.mock.instances[0].mode = 'development';
const translated = await tolgee.translate(dummyKey, dummyParams);

expect(mockedWrap).toBeCalledWith(
Expand All @@ -162,7 +162,7 @@ describe('Tolgee', () => {
});

test('will return translated string from text service in production mode', async () => {
propertiesMock.mock.instances[0].config.mode = 'production';
propertiesMock.mock.instances[0].mode = 'production';
const translated = await tolgee.translate(dummyKey, dummyParams);

expect(translated).toEqual(translatedDummyText);
Expand All @@ -177,21 +177,21 @@ describe('Tolgee', () => {
});

test('will not wrap when development is on, but noWrap is true', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
propertiesMock.mock.instances[0].mode = 'development';
const translated = await tolgee.translate(dummyKey, dummyParams, true);

expect(mockedWrap).not.toBeCalled();
expect(translated).toEqual(translatedDummyText);
});

test('will wait for translations load before wrapping', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
propertiesMock.mock.instances[0].mode = 'development';
await tolgee.translate(dummyKey, dummyParams);
expect(mockedLoadTranslations).toBeCalled();
});

test('passes default value to wrap fn', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
propertiesMock.mock.instances[0].mode = 'development';
await tolgee.translate(dummyKey, dummyParams, false, 'Default');
expect(mockedWrap).toBeCalledWith(
'dummyText',
Expand All @@ -202,7 +202,7 @@ describe('Tolgee', () => {
});

test('passes default value to translate fn', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
propertiesMock.mock.instances[0].mode = 'development';
await tolgee.translate(dummyKey, dummyParams, true, 'Default');
expect(mockedTranslate).toBeCalledWith(
'dummyText',
Expand All @@ -214,7 +214,7 @@ describe('Tolgee', () => {
});

test('props object works correctly', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
propertiesMock.mock.instances[0].mode = 'development';
await tolgee.translate({
key: dummyKey,
params: dummyParams,
Expand All @@ -231,7 +231,7 @@ describe('Tolgee', () => {
});

test('passes orEmpty correctly', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
propertiesMock.mock.instances[0].mode = 'development';
await tolgee.translate({
key: dummyKey,
params: dummyParams,
Expand All @@ -250,7 +250,7 @@ describe('Tolgee', () => {

describe('sync instant', () => {
test('will return wrapped string from text service in development mode', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
propertiesMock.mock.instances[0].mode = 'development';
const dummyParams = {};
const translated = tolgee.instant(dummyKey, dummyParams);

Expand All @@ -264,7 +264,7 @@ describe('Tolgee', () => {
});

test('will return translated string from text service in production mode', async () => {
propertiesMock.mock.instances[0].config.mode = 'production';
propertiesMock.mock.instances[0].mode = 'production';
const translated = tolgee.instant(dummyKey, dummyParams);

expect(translated).toEqual(translatedDummyText);
Expand All @@ -290,7 +290,7 @@ describe('Tolgee', () => {
});

test('will not wrap when development is on, but noWrap is true', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
propertiesMock.mock.instances[0].mode = 'development';
const translated = tolgee.instant(dummyKey, dummyParams, true);

expect(mockedWrap).not.toBeCalled();
Expand All @@ -309,7 +309,7 @@ describe('Tolgee', () => {
});

test('passes default value to wrap fn', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
propertiesMock.mock.instances[0].mode = 'development';
const dummyParams = {};
tolgee.instant(dummyKey, dummyParams, false, false, 'Default');

Expand All @@ -322,7 +322,7 @@ describe('Tolgee', () => {
});

test('props object works correctly', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
propertiesMock.mock.instances[0].mode = 'development';
await tolgee.instant({
key: dummyKey,
params: dummyParams,
Expand All @@ -341,7 +341,7 @@ describe('Tolgee', () => {
});

test('passes default value to instant fn', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
propertiesMock.mock.instances[0].mode = 'development';
const dummyParams = {};
tolgee.instant(dummyKey, dummyParams, true, false, 'Default');

Expand Down Expand Up @@ -372,7 +372,7 @@ describe('Tolgee', () => {
});

test('will return proper initialLoading', () => {
tolgee.properties.config.mode = 'production';
tolgee.properties.mode = 'production';
tolgee.properties.config.preloadFallback = true;
tolgee.properties.currentLanguage = 'cs';
tolgee.properties.config.staticData = {
Expand All @@ -385,7 +385,7 @@ describe('Tolgee', () => {
tolgee.properties.config.preloadFallback = true;
tolgee.properties.config.staticData.en = {};
expect(tolgee.initialLoading).toEqual(false);
tolgee.properties.config.mode = 'development';
tolgee.properties.mode = 'development';
expect(tolgee.initialLoading).toEqual(false);
});

Expand Down
16 changes: 10 additions & 6 deletions packages/core/src/Tolgee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,24 @@ export class Tolgee {

init(config: TolgeeConfig) {
this.dependencyService.init(config);
const { apiKey, apiUrl } = this.dependencyService.properties.config;
this.dependencyService.properties.mode =
apiKey && apiUrl ? 'development' : 'production';

return this;
}

public async run(): Promise<void> {
this.dependencyService.run();
if (this.properties.config.mode === 'development') {
if (this.properties.mode === 'development') {
try {
await this.coreService.loadApiKeyDetails();
} catch (e) {
// eslint-disable-next-line no-console
console.error("Couldn't connect to tolgee");
console.error("Couldn't connect to Tolgee");
// eslint-disable-next-line no-console
console.error(e);
this.properties.config.mode = 'production';
this.properties.mode = 'production';
}
}

Expand Down Expand Up @@ -206,7 +210,7 @@ export class Tolgee {
defaultValue
);

if (this.properties.config.mode === 'development' && !noWrap) {
if (this.properties.mode === 'development' && !noWrap) {
await this.coreService.loadApiKeyDetails();
return this.dependencyService.wrapper.wrap(
key,
Expand Down Expand Up @@ -238,7 +242,7 @@ export class Tolgee {
defaultValue?: string | undefined,
translation?: TranslationTags<any>
): TranslationTags<any> {
if (this.properties.config.mode === 'development') {
if (this.properties.mode === 'development') {
return this.dependencyService.wrapper.wrap(
key,
params,
Expand Down Expand Up @@ -295,7 +299,7 @@ export class Tolgee {
defaultValue
);

if (this.properties.config.mode === 'development' && !noWrap) {
if (this.properties.mode === 'development' && !noWrap) {
return this.dependencyService.wrapper.wrap(
key,
params,
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/TolgeeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Mode, TreeTranslationsData } from './types';
import { TreeTranslationsData } from './types';
import { NodeHelper } from './helpers/NodeHelper';
import { ModifierKey } from './Constants/ModifierKey';
import { Mode } from 'fs';

const API_KEY_LOCAL_STORAGE = '__tolgee_apiKey';
const API_URL_LOCAL_STORAGE = '__tolgee_apiUrl';
Expand All @@ -24,6 +25,10 @@ type UiType =
| Promise<UiLibInterface>;

export class TolgeeConfig {
/**
* @deprecated This option won't have any effect,
* because mode is now automatically detected when apiKey + apiUrl are set
*/
mode?: Mode;
apiUrl?: string;
apiKey?: string;
Expand Down Expand Up @@ -117,10 +122,9 @@ export class TolgeeConfig {
if (this._targetElement === undefined) {
this._targetElement = DEFAULT_TARGET_ELEMENT_SUPPLIER();
}
this.mode = this.mode || (this.apiKey ? 'development' : 'production');
this.fallbackLanguage = this.fallbackLanguage || this.defaultLanguage;
if (this.watch === undefined) {
this.watch = this.mode === 'development';
this.watch = Boolean(this.apiKey && this.apiUrl);
}
if (this.availableLanguages === undefined && this.staticData) {
this.availableLanguages = Object.keys(this.staticData);
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/services/CoreService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('CoreService', () => {
throw new Error();
});
await coreService.getApiKeyDetails();
expect(getMockedInstance(Properties).config.mode).toEqual('production');
expect(getMockedInstance(Properties).mode).toEqual('production');
// eslint-disable-next-line no-console
expect(console.error).toBeCalledTimes(2);
});
Expand All @@ -102,15 +102,15 @@ describe('CoreService', () => {

test('will set properties.scopes on run in development mode', async () => {
const propertiesMock = getMockedInstance(Properties);
propertiesMock.config.mode = 'development';
propertiesMock.mode = 'development';
await coreService.loadApiKeyDetails();
expect(propertiesMock.scopes).toContain('translations.edit' as Scope);
expect(propertiesMock.scopes).not.toContain('translations.view' as Scope);
});

test('will set properties.projectId on run in development mode', async () => {
const propertiesMock = getMockedInstance(Properties);
propertiesMock.config.mode = 'development';
propertiesMock.mode = 'development';
await coreService.loadApiKeyDetails();
expect(propertiesMock.projectId).toEqual(0);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/services/CoreService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class CoreService {
console.error(
'Error getting scopes. Trying to switch to production mode!'
);
this.properties.config.mode = 'production';
this.properties.mode = 'production';
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/services/ElementRegistrar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('ElementRegistrar', () => {
const element = createElement(1, 1);

beforeEach(async () => {
getMockedInstance(Properties).config.mode = 'development';
getMockedInstance(Properties).mode = 'development';
document.body.append(element);
await elementRegistrar.register(element);
});
Expand All @@ -57,7 +57,7 @@ describe('ElementRegistrar', () => {

test('throws error on register element without any node', async () => {
const element = createElement(0, 0);
getMockedInstance(Properties).config.mode = 'development';
getMockedInstance(Properties).mode = 'development';
document.body.append(element);
elementRegistrar.register(element);
expect((elementRegistrar as any).registeredElements).toBeInstanceOf(Set);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/services/ElementRegistrar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ElementRegistrar {
return;
}
if (
this.properties.config.mode === 'development' &&
this.properties.mode === 'development' &&
!this.registeredElements.has(element)
) {
this.translationHighlighter.listen(element);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/services/TranslationService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('TranslationService', () => {
});

test('will load translations in development mode', async () => {
getMockedInstance(Properties).config.mode = 'development';
getMockedInstance(Properties).mode = 'development';
await translationService.loadTranslations('en');
expect(translationService.getFromCacheOrFallback('key', 'en')).toEqual(
'translated'
Expand Down Expand Up @@ -184,7 +184,7 @@ describe('TranslationService', () => {
});

test("won't throw exception when there is null in translation data", async () => {
getMockedInstance(Properties).config.mode = 'development';
getMockedInstance(Properties).mode = 'development';
getMockedInstance(ApiHttpService).fetchJson = jest.fn(async () => ({
en: {
hello: null,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/services/TranslationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,14 @@ export class TranslationService {
};

private isFetchNeeded(lang: string) {
const isDevMode = this.properties.config.mode === 'development';
const isDevMode = this.properties.mode === 'development';
const dataPresent = this.translationsCache.get(lang) !== undefined;
const devFetched = Boolean(this.fetchedDev[lang]);
return (isDevMode && !devFetched) || !dataPresent;
}

private async fetchTranslations(lang: string) {
const isDevMode = this.properties.config.mode === 'development';
const isDevMode = this.properties.mode === 'development';
if (isDevMode) {
return await this.fetchTranslationsDevelopment(lang);
} else {
Expand Down

0 comments on commit 7ecaf26

Please sign in to comment.