environment service to ts

This commit is contained in:
Kyle Spearrin 2017-11-03 12:33:34 -04:00
parent 6ae7b2e035
commit 43ef921886
5 changed files with 92 additions and 107 deletions

View File

@ -12,7 +12,6 @@
<script type="text/javascript" src="services/syncService.js"></script>
<script type="text/javascript" src="services/autofillService.js"></script>
<script type="text/javascript" src="services/totpService.js"></script>
<script type="text/javascript" src="services/environmentService.js"></script>
</head>
<body>
</body>

View File

@ -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);

View File

@ -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;

View File

@ -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<void> {
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<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 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;
}
}

View File

@ -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;
}
}