convert environment service to jslib

This commit is contained in:
Kyle Spearrin 2018-01-09 20:20:45 -05:00
parent eb031eda0f
commit 0069c2b700
2 changed files with 3 additions and 92 deletions

View File

@ -4,6 +4,7 @@ import {
ApiService,
AppIdService,
CryptoService,
EnvironmentService,
PasswordGenerationService,
TokenService,
TotpService,
@ -15,6 +16,7 @@ import {
ApiService as ApiServiceAbstraction,
AppIdService as AppIdServiceAbstraction,
CryptoService as CryptoServiceAbstraction,
EnvironmentService as EnvironmentServiceAbstraction,
MessagingService as MessagingServiceAbstraction,
PasswordGenerationService as PasswordGenerationServiceAbstraction,
PlatformUtilsService as PlatformUtilsServiceAbstraction,
@ -43,7 +45,6 @@ import CipherService from '../services/cipher.service';
import CollectionService from '../services/collection.service';
import ConstantsService from '../services/constants.service';
import ContainerService from '../services/container.service';
import EnvironmentService from '../services/environment.service';
import FolderService from '../services/folder.service';
import i18nService from '../services/i18n.service';
import LockService from '../services/lock.service';
@ -61,7 +62,7 @@ export default class MainBackground {
tokenService: TokenServiceAbstraction;
appIdService: AppIdServiceAbstraction;
apiService: ApiServiceAbstraction;
environmentService: EnvironmentService;
environmentService: EnvironmentServiceAbstraction;
userService: UserServiceAbstraction;
settingsService: SettingsService;
cipherService: CipherService;

View File

@ -1,90 +0,0 @@
import ConstantsService from './constants.service';
import { EnvironmentUrls } from 'jslib/models/domain';
import {
ApiService,
StorageService,
} from 'jslib/abstractions';
export default 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;
}
}