You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

141 lines
2.8 KiB

export default class API {
constructor() {
this.SERVER = 'http://localhost:51821'; //! DEV
}
async call({ method, path, body }) {
const res = await fetch(`${this.SERVER}/api${path}`, {
method,
headers: {
'Content-Type': 'application/json',
},
credentials: 'include', //! DEV
body: body ? JSON.stringify(body) : undefined,
});
if (res.status === 204) {
return undefined;
}
const json = await res.json();
if (!res.ok) {
throw new Error(json.error || res.statusText);
}
return json;
}
async getRelease() {
return this.call({
method: 'get',
path: '/release',
});
}
async getLang() {
const res = await this.call({
method: 'get',
path: '/lang',
});
return res.slice(0, 2);
}
async getuiTrafficStats() {
return this.call({
method: 'get',
path: '/ui-traffic-stats',
});
}
async getChartType() {
return this.call({
method: 'get',
path: '/ui-chart-type',
});
}
async getSession() {
return this.call({
method: 'get',
path: '/session',
});
}
async createSession({ password }) {
return this.call({
method: 'post',
path: '/session',
body: { password },
});
}
async deleteSession() {
return this.call({
method: 'delete',
path: '/session',
});
}
async getClients() {
return this.call({
method: 'get',
path: '/wireguard/client',
}).then((clients) =>
clients.map((client) => ({
...client,
createdAt: new Date(client.createdAt),
updatedAt: new Date(client.updatedAt),
latestHandshakeAt: client.latestHandshakeAt !== null ? new Date(client.latestHandshakeAt) : null,
}))
);
}
async createClient({ name }) {
return this.call({
method: 'post',
path: '/wireguard/client',
body: { name },
});
}
async deleteClient({ clientId }) {
return this.call({
method: 'delete',
path: `/wireguard/client/${clientId}`,
});
}
async enableClient({ clientId }) {
return this.call({
method: 'post',
path: `/wireguard/client/${clientId}/enable`,
});
}
async disableClient({ clientId }) {
return this.call({
method: 'post',
path: `/wireguard/client/${clientId}/disable`,
});
}
async updateClientName({ clientId, name }) {
return this.call({
method: 'put',
path: `/wireguard/client/${clientId}/name/`,
body: { name },
});
}
async updateClientAddress({ clientId, address }) {
return this.call({
method: 'put',
path: `/wireguard/client/${clientId}/address/`,
body: { address },
});
}
async getQrCode({ clientId }) {
return `${this.SERVER}/api/wireguard/client/${clientId}/qrcode.svg`;
}
}