mirror of
https://github.com/bitwarden/browser
synced 2025-12-22 03:03:43 +00:00
environment service to ts
This commit is contained in:
87
src/services/environment.service.ts
Normal file
87
src/services/environment.service.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user