-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtincwebui.js
executable file
·102 lines (88 loc) · 2.54 KB
/
tincwebui.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
export class TincWebUIError extends Error {
constructor(message, code, details) {
super(code + ': ' + message);
this.code = code;
this.details = details;
}
}
export class TincWebUI {
/**
Operations with tinc-web-boot related to UI
**/
// Create new API handler to TincWebUI.
// preflightHandler (if defined) can return promise
constructor(base_url = 'http://127.0.0.1:8686/api/', preflightHandler = null) {
this.__url = base_url;
this.__id = 1;
this.__preflightHandler = preflightHandler;
}
/**
Issue and sign token
**/
async issueAccessToken(validDays){
return (await this.__call('IssueAccessToken', {
"jsonrpc" : "2.0",
"method" : "TincWebUI.IssueAccessToken",
"id" : this.__next_id(),
"params" : [validDays]
}));
}
/**
Make desktop notification if system supports it
**/
async notify(title, message){
return (await this.__call('Notify', {
"jsonrpc" : "2.0",
"method" : "TincWebUI.Notify",
"id" : this.__next_id(),
"params" : [title, message]
}));
}
/**
Endpoints list to access web UI
**/
async endpoints(){
return (await this.__call('Endpoints', {
"jsonrpc" : "2.0",
"method" : "TincWebUI.Endpoints",
"id" : this.__next_id(),
"params" : []
}));
}
/**
Configuration defined for the instance
**/
async configuration(){
return (await this.__call('Configuration', {
"jsonrpc" : "2.0",
"method" : "TincWebUI.Configuration",
"id" : this.__next_id(),
"params" : []
}));
}
__next_id() {
this.__id += 1;
return this.__id
}
async __call(method, req) {
const fetchParams = {
method: "POST",
headers: {
'Content-Type' : 'application/json',
},
body: JSON.stringify(req)
};
if (this.__preflightHandler) {
await Promise.resolve(this.__preflightHandler(method, fetchParams));
}
const res = await fetch(this.__url, fetchParams);
if (!res.ok) {
throw new Error(res.status + ' ' + res.statusText);
}
const data = await res.json();
if ('error' in data) {
throw new TincWebUIError(data.error.message, data.error.code, data.error.data);
}
return data.result;
}
}