mirror of
https://github.com/ad1217/PrinterStatus
synced 2024-11-21 23:13:49 -05:00
Use WHATWG URL API for server URL building
This commit is contained in:
parent
3bf5b6b476
commit
41f5201d6b
@ -54,7 +54,7 @@ app.get('/webcam/:printer', (req, res) => {
|
|||||||
);
|
);
|
||||||
if (printer?.webcamURL) {
|
if (printer?.webcamURL) {
|
||||||
req.url = ''; // truncate the url for passing to the proxy
|
req.url = ''; // truncate the url for passing to the proxy
|
||||||
proxy.web(req, res, { target: printer.webcamURL });
|
proxy.web(req, res, { target: printer.webcamURL.toString() });
|
||||||
} else res.status(404).send('Not Found: Printer not known or has no webcam.');
|
} else res.status(404).send('Not Found: Printer not known or has no webcam.');
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -67,7 +67,7 @@ class PrinterStatus {
|
|||||||
address: string;
|
address: string;
|
||||||
apikey: string;
|
apikey: string;
|
||||||
|
|
||||||
webcamURL?: string;
|
webcamURL?: URL;
|
||||||
name?: string;
|
name?: string;
|
||||||
|
|
||||||
websocket?: WebSocket;
|
websocket?: WebSocket;
|
||||||
@ -87,10 +87,7 @@ class PrinterStatus {
|
|||||||
async init() {
|
async init() {
|
||||||
// TODO: error handling (try/catch)
|
// TODO: error handling (try/catch)
|
||||||
const settings = await this.api_get('settings');
|
const settings = await this.api_get('settings');
|
||||||
this.webcamURL = settings.webcam.streamUrl;
|
this.webcamURL = new URL(settings.webcam.streamUrl, this.address);
|
||||||
if (this.webcamURL?.startsWith('/')) {
|
|
||||||
this.webcamURL = this.address + this.webcamURL;
|
|
||||||
}
|
|
||||||
this.name = settings.appearance.name;
|
this.name = settings.appearance.name;
|
||||||
|
|
||||||
// do passive login to get a session key from the API key
|
// do passive login to get a session key from the API key
|
||||||
@ -102,9 +99,9 @@ class PrinterStatus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
connect_websocket(authToken: string): void {
|
connect_websocket(authToken: string): void {
|
||||||
this.websocket = new WebSocket(
|
const url = new URL('/sockjs/websocket', this.address)
|
||||||
url.resolve(this.address, '/sockjs/websocket')
|
url.protocol = 'ws';
|
||||||
);
|
this.websocket = new WebSocket(url.toString());
|
||||||
this.websocket
|
this.websocket
|
||||||
.on('open', () => {
|
.on('open', () => {
|
||||||
this.websocket!.send(JSON.stringify({ auth: authToken }));
|
this.websocket!.send(JSON.stringify({ auth: authToken }));
|
||||||
@ -129,14 +126,14 @@ class PrinterStatus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async api_get(endpoint: string): Promise<any> {
|
async api_get(endpoint: string): Promise<any> {
|
||||||
const r = await fetch(url.resolve(this.address, '/api/' + endpoint), {
|
const r = await fetch(new URL('/api/' + endpoint, this.address), {
|
||||||
headers: { 'X-Api-Key': this.apikey },
|
headers: { 'X-Api-Key': this.apikey },
|
||||||
});
|
});
|
||||||
return await r.json();
|
return await r.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
async api_post(endpoint: string, data: any): Promise<any> {
|
async api_post(endpoint: string, data: any): Promise<any> {
|
||||||
const r = await fetch(url.resolve(this.address, '/api/' + endpoint), {
|
const r = await fetch(new URL('/api/' + endpoint, this.address), {
|
||||||
headers: {
|
headers: {
|
||||||
'X-Api-Key': this.apikey,
|
'X-Api-Key': this.apikey,
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
|
Loading…
Reference in New Issue
Block a user