mirror of
https://github.com/bitwarden/browser.git
synced 2024-12-27 17:18:04 +01:00
convert environment service to jslib
This commit is contained in:
parent
38502b5447
commit
3220252cb9
10
src/abstractions/environment.service.ts
Normal file
10
src/abstractions/environment.service.ts
Normal file
@ -0,0 +1,10 @@
|
||||
export interface EnvironmentService {
|
||||
baseUrl: string;
|
||||
webVaultUrl: string;
|
||||
apiUrl: string;
|
||||
identityUrl: string;
|
||||
iconsUrl: string;
|
||||
|
||||
setUrlsFromStorage(): Promise<void>;
|
||||
setUrls(urls: any): Promise<any>;
|
||||
}
|
@ -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';
|
||||
|
89
src/services/environment.service.ts
Normal file
89
src/services/environment.service.ts
Normal file
@ -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<void> {
|
||||
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<any> {
|
||||
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;
|
||||
}
|
||||
}
|
@ -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';
|
||||
|
Loading…
Reference in New Issue
Block a user