From 3220252cb9d73930e64291f3874a81694caf1966 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 9 Jan 2018 20:20:54 -0500 Subject: [PATCH] convert environment service to jslib --- src/abstractions/environment.service.ts | 10 +++ src/abstractions/index.ts | 1 + src/services/environment.service.ts | 89 +++++++++++++++++++++++++ src/services/index.ts | 1 + 4 files changed, 101 insertions(+) create mode 100644 src/abstractions/environment.service.ts create mode 100644 src/services/environment.service.ts diff --git a/src/abstractions/environment.service.ts b/src/abstractions/environment.service.ts new file mode 100644 index 0000000000..a021db8910 --- /dev/null +++ b/src/abstractions/environment.service.ts @@ -0,0 +1,10 @@ +export interface EnvironmentService { + baseUrl: string; + webVaultUrl: string; + apiUrl: string; + identityUrl: string; + iconsUrl: string; + + setUrlsFromStorage(): Promise; + setUrls(urls: any): Promise; +} diff --git a/src/abstractions/index.ts b/src/abstractions/index.ts index 9804abdf32..74062e753c 100644 --- a/src/abstractions/index.ts +++ b/src/abstractions/index.ts @@ -1,6 +1,7 @@ export { ApiService } from './api.service'; export { AppIdService } from './appId.service'; export { CryptoService } from './crypto.service'; +export { EnvironmentService } from './environment.service'; export { MessagingService } from './messaging.service'; export { PasswordGenerationService } from './passwordGeneration.service'; export { PlatformUtilsService } from './platformUtils.service'; diff --git a/src/services/environment.service.ts b/src/services/environment.service.ts new file mode 100644 index 0000000000..292ee89a4d --- /dev/null +++ b/src/services/environment.service.ts @@ -0,0 +1,89 @@ +import { EnvironmentUrls } from '../models/domain/environmentUrls'; + +import { ConstantsService } from './constants.service'; + +import { ApiService } from '../abstractions/api.service'; +import { EnvironmentService as EnvironmentServiceInterface } from '../abstractions/environment.service'; +import { StorageService } from '../abstractions/storage.service'; + +export class EnvironmentService { + baseUrl: string; + webVaultUrl: string; + apiUrl: string; + identityUrl: string; + iconsUrl: string; + + constructor(private apiService: ApiService, private storageService: StorageService) { + } + + async setUrlsFromStorage(): Promise { + const urlsObj: any = await this.storageService.get(ConstantsService.environmentUrlsKey); + const urls = urlsObj || { + base: null, + api: null, + identity: null, + icons: null, + webVault: null, + }; + + const envUrls = new EnvironmentUrls(); + + if (urls.base) { + this.baseUrl = envUrls.base = urls.base; + await this.apiService.setUrls(envUrls); + return; + } + + this.webVaultUrl = urls.webVault; + this.apiUrl = envUrls.api = urls.api; + this.identityUrl = envUrls.identity = urls.identity; + this.iconsUrl = urls.icons; + await this.apiService.setUrls(envUrls); + } + + async setUrls(urls: any): Promise { + urls.base = this.formatUrl(urls.base); + urls.webVault = this.formatUrl(urls.webVault); + urls.api = this.formatUrl(urls.api); + urls.identity = this.formatUrl(urls.identity); + urls.icons = this.formatUrl(urls.icons); + + await this.storageService.save(ConstantsService.environmentUrlsKey, { + base: urls.base, + api: urls.api, + identity: urls.identity, + webVault: urls.webVault, + icons: urls.icons, + }); + + this.baseUrl = urls.base; + this.webVaultUrl = urls.webVault; + this.apiUrl = urls.api; + this.identityUrl = urls.identity; + this.iconsUrl = urls.icons; + + const envUrls = new EnvironmentUrls(); + if (this.baseUrl) { + envUrls.base = this.baseUrl; + } else { + envUrls.api = this.apiUrl; + envUrls.identity = this.identityUrl; + } + + await this.apiService.setUrls(envUrls); + return urls; + } + + private formatUrl(url: string): string { + if (url == null || url === '') { + return null; + } + + url = url.replace(/\/+$/g, ''); + if (!url.startsWith('http://') && !url.startsWith('https://')) { + url = 'https://' + url; + } + + return url; + } +} diff --git a/src/services/index.ts b/src/services/index.ts index 084707f27d..a2fae25f3e 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -2,6 +2,7 @@ export { ApiService } from './api.service'; export { AppIdService } from './appId.service'; export { ConstantsService } from './constants.service'; export { CryptoService } from './crypto.service'; +export { EnvironmentService } from './environment.service'; export { PasswordGenerationService } from './passwordGeneration.service'; export { TokenService } from './token.service'; export { TotpService } from './totp.service';