diff --git a/src/background.html b/src/background.html
index 8ab1e3135a9..4a69fa81869 100644
--- a/src/background.html
+++ b/src/background.html
@@ -12,7 +12,6 @@
-
diff --git a/src/background.js b/src/background.js
index bec397329b4..cfb42b5674c 100644
--- a/src/background.js
+++ b/src/background.js
@@ -3,6 +3,7 @@ import ApiService from './services/api.service';
import AppIdService from './services/appId.service';
import ConstantsService from './services/constants.service';
import CryptoService from './services/crypto.service';
+import EnvironmentService from './services/environment.service';
import i18nService from './services/i18nService.js';
import LockService from './services/lockService.js';
import PasswordGenerationService from './services/passwordGeneration.service';
@@ -81,7 +82,7 @@ var bg_isBackground = true,
window.bg_tokenService = bg_tokenService = new TokenService();
window.bg_appIdService = bg_appIdService = new AppIdService();
window.bg_apiService = bg_apiService = new ApiService(bg_tokenService, logout);
- window.bg_environmentService = bg_environmentService = new EnvironmentService(bg_constantsService, bg_apiService);
+ window.bg_environmentService = bg_environmentService = new EnvironmentService(bg_apiService);
window.bg_userService = bg_userService = new UserService(bg_tokenService, bg_apiService, bg_cryptoService, bg_utilsService);
window.bg_settingsService = bg_settingsService = new SettingsService(bg_userService, bg_utilsService);
window.bg_cipherService = bg_cipherService = new CipherService(bg_cryptoService, bg_userService, bg_apiService, bg_settingsService, bg_utilsService,
@@ -943,7 +944,7 @@ var bg_isBackground = true,
// Bootstrap
- bg_environmentService.setUrlsFromStorage(function () {
+ bg_environmentService.setUrlsFromStorage().then(function () {
setIcon();
cleanupbg_loginsToAdd();
fullSync(true);
diff --git a/src/popup/app/settings/settingsEnvironmentController.js b/src/popup/app/settings/settingsEnvironmentController.js
index 61c776442e7..914dd4a0ec3 100644
--- a/src/popup/app/settings/settingsEnvironmentController.js
+++ b/src/popup/app/settings/settingsEnvironmentController.js
@@ -1,4 +1,4 @@
-angular
+angular
.module('bit.settings')
.controller('settingsEnvironmentController', function ($scope, i18nService, $analytics, utilsService,
@@ -20,7 +20,7 @@
identity: $scope.identityUrl,
webVault: $scope.webVaultUrl,
icons: $scope.iconsUrl
- }, function (resUrls) {
+ }).then(function (resUrls) {
$timeout(function () {
// re-set urls since service can change them, ex: prefixing https://
$scope.baseUrl = resUrls.base;
diff --git a/src/services/environment.service.ts b/src/services/environment.service.ts
new file mode 100644
index 00000000000..1ff5b98b106
--- /dev/null
+++ b/src/services/environment.service.ts
@@ -0,0 +1,87 @@
+import ApiService from './api.service';
+import ConstantsService from './constants.service';
+import UtilsService from './utils.service';
+
+import EnvironmentUrls from '../models/domain/environmentUrls';
+
+export default class EnvironmentService {
+ baseUrl: string;
+ webVaultUrl: string;
+ apiUrl: string;
+ identityUrl: string;
+ iconsUrl: string;
+
+ constructor(private apiService: ApiService) {
+ }
+
+ async setUrlsFromStorage(): Promise {
+ const urlsObj: any = await UtilsService.getObjFromStorage(ConstantsService.environmentUrlsKey);
+ const urls = urlsObj[ConstantsService.environmentUrlsKey] || {
+ 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 UtilsService.saveObjToStorage(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/environmentService.js b/src/services/environmentService.js
deleted file mode 100644
index 5a8aa48cfcc..00000000000
--- a/src/services/environmentService.js
+++ /dev/null
@@ -1,102 +0,0 @@
-function EnvironmentService(constantsService, apiService) {
- this.constantsService = constantsService;
- this.apiService = apiService;
-
- this.baseUrl = null;
- this.webVaultUrl = null;
- this.apiUrl = null;
- this.identityUrl = null;
- this.iconsUrl = null;
-
- initEnvironmentService();
-}
-
-function initEnvironmentService() {
- EnvironmentService.prototype.setUrlsFromStorage = function (callback) {
- var self = this;
-
- chrome.storage.local.get(self.constantsService.environmentUrlsKey, function (urlsObj) {
- var urls = urlsObj[self.constantsService.environmentUrlsKey] || {
- base: null,
- api: null,
- identity: null,
- icons: null,
- webVault: null
- };
-
- self.baseUrl = urls.base;
- if (self.baseUrl) {
- self.apiService.setUrls({
- base: self.baseUrl
- });
- callback();
- return;
- }
-
- self.webVaultUrl = urls.webVault;
- self.apiUrl = urls.api;
- self.identityUrl = urls.identity;
- self.iconsUrl = urls.icons;
-
- self.apiService.setUrls({
- api: self.apiUrl,
- identity: self.identityUrl
- });
- callback();
- return;
- });
- };
-
- EnvironmentService.prototype.setUrls = function (urls, callback) {
- var self = this;
-
- urls.base = formatUrl(urls.base);
- urls.webVault = formatUrl(urls.webVault);
- urls.api = formatUrl(urls.api);
- urls.identity = formatUrl(urls.identity);
- urls.icons = formatUrl(urls.icons);
-
- var urlsObj = {};
- urlsObj[self.constantsService.environmentUrlsKey] = {
- base: urls.base,
- api: urls.api,
- identity: urls.identity,
- webVault: urls.webVault,
- icons: urls.icons
- };
-
- chrome.storage.local.set(urlsObj, function () {
- self.baseUrl = urls.base;
- self.webVaultUrl = urls.webVault;
- self.apiUrl = urls.api;
- self.identityUrl = urls.identity;
- self.iconsUrl = urls.icons;
-
- if (self.baseUrl) {
- self.apiService.setUrls({
- base: self.baseUrl
- });
- }
- else {
- self.apiService.setUrls({
- api: self.apiUrl,
- identity: self.identityUrl
- });
- }
-
- callback(urls);
- });
- };
-
- function formatUrl(url) {
- if (!url || url === '') {
- return null;
- }
-
- url = url.replace(/\/+$/g, '');
- if (!url.startsWith("http://") && !url.startsWith('https://')) {
- url = 'https://' + url;
- }
- return url;
- }
-}