diff --git a/packages/core/src/Properties.ts b/packages/core/src/Properties.ts index e058f96000..d8bca379da 100644 --- a/packages/core/src/Properties.ts +++ b/packages/core/src/Properties.ts @@ -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'; @@ -9,6 +9,7 @@ export class Properties { scopes?: Scope[]; projectId?: number; permittedLanguageIds?: number[]; + mode?: Mode; _currentLanguage?: string; get currentLanguage(): string { diff --git a/packages/core/src/Tolgee.test.ts b/packages/core/src/Tolgee.test.ts index b162253cc5..0c445eef08 100644 --- a/packages/core/src/Tolgee.test.ts +++ b/packages/core/src/Tolgee.test.ts @@ -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 @@ -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 @@ -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( @@ -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); @@ -177,7 +177,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 = await tolgee.translate(dummyKey, dummyParams, true); expect(mockedWrap).not.toBeCalled(); @@ -185,13 +185,13 @@ describe('Tolgee', () => { }); 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', @@ -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', @@ -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, @@ -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, @@ -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); @@ -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); @@ -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(); @@ -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'); @@ -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, @@ -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'); @@ -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 = { @@ -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); }); diff --git a/packages/core/src/Tolgee.ts b/packages/core/src/Tolgee.ts index 28af038dc4..8d80971663 100644 --- a/packages/core/src/Tolgee.ts +++ b/packages/core/src/Tolgee.ts @@ -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 { 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'; } } @@ -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, @@ -238,7 +242,7 @@ export class Tolgee { defaultValue?: string | undefined, translation?: TranslationTags ): TranslationTags { - if (this.properties.config.mode === 'development') { + if (this.properties.mode === 'development') { return this.dependencyService.wrapper.wrap( key, params, @@ -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, diff --git a/packages/core/src/TolgeeConfig.ts b/packages/core/src/TolgeeConfig.ts index ca5560c8c7..66a2b78006 100644 --- a/packages/core/src/TolgeeConfig.ts +++ b/packages/core/src/TolgeeConfig.ts @@ -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'; @@ -24,6 +25,10 @@ type UiType = | Promise; 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; @@ -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); diff --git a/packages/core/src/services/CoreService.test.ts b/packages/core/src/services/CoreService.test.ts index 1885842a73..34c918763b 100644 --- a/packages/core/src/services/CoreService.test.ts +++ b/packages/core/src/services/CoreService.test.ts @@ -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); }); @@ -102,7 +102,7 @@ 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); @@ -110,7 +110,7 @@ describe('CoreService', () => { 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); }); diff --git a/packages/core/src/services/CoreService.ts b/packages/core/src/services/CoreService.ts index 6f5046ff1f..7821738fb4 100644 --- a/packages/core/src/services/CoreService.ts +++ b/packages/core/src/services/CoreService.ts @@ -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'; } } diff --git a/packages/core/src/services/ElementRegistrar.test.ts b/packages/core/src/services/ElementRegistrar.test.ts index b47ebd1efc..11fb8fa7f3 100644 --- a/packages/core/src/services/ElementRegistrar.test.ts +++ b/packages/core/src/services/ElementRegistrar.test.ts @@ -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); }); @@ -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); diff --git a/packages/core/src/services/ElementRegistrar.ts b/packages/core/src/services/ElementRegistrar.ts index 5e00fc6b73..2ea5359e3f 100644 --- a/packages/core/src/services/ElementRegistrar.ts +++ b/packages/core/src/services/ElementRegistrar.ts @@ -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); diff --git a/packages/core/src/services/TranslationService.test.ts b/packages/core/src/services/TranslationService.test.ts index 39cdeab478..b266b05241 100644 --- a/packages/core/src/services/TranslationService.test.ts +++ b/packages/core/src/services/TranslationService.test.ts @@ -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' @@ -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, diff --git a/packages/core/src/services/TranslationService.ts b/packages/core/src/services/TranslationService.ts index 7f66fd67ba..d6badd9919 100644 --- a/packages/core/src/services/TranslationService.ts +++ b/packages/core/src/services/TranslationService.ts @@ -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 {