From 5ca907b3c4262e3115778e391d358b3282bb13eb Mon Sep 17 00:00:00 2001 From: rozetko Date: Mon, 25 Feb 2019 15:29:14 +0300 Subject: [PATCH] Hastic URL doesn't update #172 (#189) --- package.json | 1 + src/config/config_ctrl.ts | 25 ++++++++++++++++++++++++- src/config/template.html | 7 ++++++- src/utlis.ts | 29 +++++++++++++++++++++++++++++ tests/utils.jest.ts | 26 ++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/utlis.ts create mode 100644 tests/utils.jest.ts diff --git a/package.json b/package.json index c590768..6e25851 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "ts-jest": "^22.4.6", "ts-loader": "^4.2.0", "typescript": "^2.8.3", + "url-parse": "^1.4.4", "webpack": "4.7.0", "webpack-cli": "^2.1.2" }, diff --git a/src/config/config_ctrl.ts b/src/config/config_ctrl.ts index 85e2c38..6e125a4 100644 --- a/src/config/config_ctrl.ts +++ b/src/config/config_ctrl.ts @@ -1,13 +1,36 @@ import template from './template.html'; - +import { normalizeUrl } from '../utlis'; class ConfigCtrl { static template = template; appModel: any; + appEditCtrl: any; + constructor() { if(this.appModel.jsonData === undefined) { this.appModel.jsonData = {}; } + + this.appEditCtrl.setPreUpdateHook(this.preUpdate.bind(this)); + this.appEditCtrl.setPostUpdateHook(this.postUpdate.bind(this)); + } + + preUpdate() { + this.normalizeUrl(); + return Promise.resolve(); + } + + postUpdate() { + // TODO: check whether hasticServerUrl is accessible + if(!this.appModel.enabled) { + return Promise.resolve(); + } + + return { message: 'Hastic app installed!' }; + } + + normalizeUrl() { + this.appModel.jsonData.hasticServerUrl = normalizeUrl(this.appModel.jsonData.hasticServerUrl); } } diff --git a/src/config/template.html b/src/config/template.html index 2312aa4..309c183 100644 --- a/src/config/template.html +++ b/src/config/template.html @@ -2,6 +2,11 @@
- +
diff --git a/src/utlis.ts b/src/utlis.ts new file mode 100644 index 0000000..4bab590 --- /dev/null +++ b/src/utlis.ts @@ -0,0 +1,29 @@ +import url from 'url-parse'; + +export function normalizeUrl(grafanaUrl: string) { + if(!grafanaUrl) { + return grafanaUrl; + } + let urlObj = new url(grafanaUrl, {}); + if(urlObj.protocol !== 'http:' && urlObj.protocol !== 'https:') { + grafanaUrl = `http://${grafanaUrl}`; + urlObj = new url(grafanaUrl, {}); + console.log('No protocol provided in GRAFANA_URL -> inserting "http://"'); + } + if(urlObj.slashes === false) { + urlObj = new url(`${urlObj.protocol}//${urlObj.pathname}`, {}); + console.log('No slashes were provided after the protocol -> inserting slashes'); + } + if(urlObj.pathname.slice(-1) === '/') { + urlObj.pathname = urlObj.pathname.slice(0, -1); + console.log('Removing the slash at the end of GRAFANA_URL'); + } + let finalUrl = `${urlObj.protocol}//${urlObj.hostname}`; + if(urlObj.port !== '') { + finalUrl = finalUrl + ':' + urlObj.port; + } + if(urlObj.pathname !== '') { + finalUrl = finalUrl + urlObj.pathname; + } + return finalUrl; +} diff --git a/tests/utils.jest.ts b/tests/utils.jest.ts new file mode 100644 index 0000000..5ad7711 --- /dev/null +++ b/tests/utils.jest.ts @@ -0,0 +1,26 @@ +import { normalizeUrl } from '../src/utlis'; + +describe('Normalize URL', function() { + const cases = [ + { value: '127.0.0.1:8000', expected: 'http://127.0.0.1:8000' }, + { value: '127.0.0.1:8000/', expected: 'http://127.0.0.1:8000' }, + { value: 'localhost:8000', expected: 'http://localhost:8000' }, + { value: 'localhost:8000/', expected: 'http://localhost:8000' }, + { value: 'http://localhost:3000', expected: 'http://localhost:3000' }, + { value: 'http://localhost:3000/', expected: 'http://localhost:3000' }, + { value: 'https://localhost:8000', expected: 'https://localhost:8000' }, + { value: 'https://localhost:8000/', expected: 'https://localhost:8000' }, + { value: 'http://example.com', expected: 'http://example.com' }, + { value: 'http://example.com/', expected: 'http://example.com' }, + { value: 'https://example.com', expected: 'https://example.com' }, + { value: 'https://example.com/', expected: 'https://example.com' }, + { value: 'https://example.com/grafana', expected: 'https://example.com/grafana' }, + { value: 'https://example.com/grafana/', expected: 'https://example.com/grafana' }, + ]; + + it('should normalize URLs correctly', function() { + cases.forEach(testCase => { + expect(normalizeUrl(testCase.value)).toBe(testCase.expected); + }); + }); +});