mirror of
https://github.com/w0rng/amnezia-wg-easy.git
synced 2025-04-29 23:00:06 +03:00
wip
This commit is contained in:
parent
eaf3d5c3fb
commit
d7bb645470
25 changed files with 4632 additions and 2 deletions
90
src/www/js/api.js
Normal file
90
src/www/js/api.js
Normal file
|
@ -0,0 +1,90 @@
|
|||
/* eslint-disable no-unused-vars */
|
||||
/* eslint-disable no-undef */
|
||||
|
||||
'use strict';
|
||||
|
||||
class API {
|
||||
|
||||
async call({ method, path, body }) {
|
||||
const res = await fetch(`/api${path}`, {
|
||||
method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
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 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',
|
||||
});
|
||||
}
|
||||
|
||||
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`,
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue