Skip to content

Commit 13c507e

Browse files
author
Dennis Labordus
committed
Small changes to code.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent 0221f67 commit 13c507e

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

src/compas-services/CompasValidatorService.ts

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
11
import { CompasSettings } from "../compas/CompasSettings.js";
2+
import { Websockets } from "./Websockets.js";
23
import {
3-
createLogEvent,
4+
getWebsocketUri,
45
handleError,
56
handleResponse,
67
parseXml
78
} from "./foundation.js";
8-
import {Websockets} from "./Websockets";
99

1010
export const SVS_NAMESPACE = 'https://www.lfenergy.org/compas/SclValidatorService/v1';
1111

1212
export function CompasSclValidatorService() {
13-
function getCompasSettings() {
14-
return CompasSettings().compasSettings;
15-
}
16-
17-
function getWebsocketPort(): string {
18-
if (document.location.port === "") {
19-
return (document.location.protocol == "http:" ? "80" : "443")
20-
}
21-
return document.location.port;
22-
}
23-
24-
function getWebsocketUri(): string {
25-
const sclValidatorServiceUrl = getCompasSettings().sclValidatorServiceUrl;
26-
if (sclValidatorServiceUrl.startsWith("http://") || sclValidatorServiceUrl.startsWith("https://")) {
27-
return sclValidatorServiceUrl.replace("http://", "ws://").replace("https://", "wss://");
28-
}
29-
30-
return (document.location.protocol == "http:" ? "ws://" : "wss://")
31-
+ document.location.hostname + ":" + getWebsocketPort()
32-
+ sclValidatorServiceUrl;
13+
function getSclValidatorServiceUrl(): string {
14+
return CompasSettings().compasSettings.sclValidatorServiceUrl;
3315
}
3416

3517
function createRequest(doc: Document): string {
@@ -41,11 +23,11 @@ export function CompasSclValidatorService() {
4123

4224
return {
4325
useWebsocket(): boolean {
44-
return getCompasSettings().useWebsockets === 'on';
26+
return CompasSettings().useWebsockets();
4527
},
4628

4729
validateSCLUsingRest(type: string, doc: Document): Promise<Document> {
48-
const svsUrl = getCompasSettings().sclValidatorServiceUrl + '/validate/v1/' + type;
30+
const svsUrl = getSclValidatorServiceUrl() + '/validate/v1/' + type;
4931
return fetch(svsUrl, {
5032
method: 'POST',
5133
headers: {
@@ -60,20 +42,20 @@ export function CompasSclValidatorService() {
6042
validateSCLUsingWebsockets(type: string, doc: Document, callback: (doc: Document) => void) {
6143
Websockets('CompasValidatorService')
6244
.execute(
63-
getWebsocketUri() + '/validate-ws/v1/' + type,
45+
getWebsocketUri(getSclValidatorServiceUrl()) + '/validate-ws/v1/' + type,
6446
createRequest(doc),
6547
callback);
6648
},
6749

6850
listNsdocFiles(): Promise<Document> {
69-
const svsUrl = getCompasSettings().sclValidatorServiceUrl + '/nsdoc/v1';
51+
const svsUrl = getSclValidatorServiceUrl() + '/nsdoc/v1';
7052
return fetch(svsUrl).catch(handleError)
7153
.then(handleResponse)
7254
.then(parseXml);
7355
},
7456

7557
getNsdocFile(id: string): Promise<string> {
76-
const svsUrl = getCompasSettings().sclValidatorServiceUrl + '/nsdoc/v1/' + id;
58+
const svsUrl = getSclValidatorServiceUrl() + '/nsdoc/v1/' + id;
7759
return fetch(svsUrl).catch(handleError)
7860
.then(handleResponse);
7961
},

src/compas-services/foundation.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,21 @@ export function createLogEvent(reason: any): void {
7777
message: get('compas.error.serverDetails', {type: reason.type, message: message})
7878
}));
7979
}
80+
81+
export function getWebsocketUri(settingsUrl: string): string {
82+
if (settingsUrl.startsWith("http://") || settingsUrl.startsWith("https://")) {
83+
return settingsUrl.replace("http://", "ws://").replace("https://", "wss://");
84+
}
85+
86+
return (document.location.protocol == "http:" ? "ws://" : "wss://")
87+
+ document.location.hostname + ":" + getWebsocketPort()
88+
+ settingsUrl;
89+
}
90+
91+
export function getWebsocketPort(): string {
92+
if (document.location.port === "") {
93+
return (document.location.protocol == "http:" ? "80" : "443")
94+
}
95+
return document.location.port;
96+
}
97+

src/compas/CompasSettings.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export function CompasSettings() {
4040
}
4141
},
4242

43+
useWebsockets(): boolean {
44+
return this.compasSettings.useWebsockets === 'on';
45+
},
46+
4347
/** Update the `value` of `setting`, storing to `localStorage`. */
4448
setCompasSetting<T extends keyof CompasSettingsRecord>(setting: T, value: CompasSettingsRecord[T]): void {
4549
localStorage.setItem(setting, <string>(<unknown>value));

0 commit comments

Comments
 (0)