From ef73d8fa70cbfe2695e5d571204ed75fc3655907 Mon Sep 17 00:00:00 2001 From: Daniil Isakov <12859907+oplexz@users.noreply.github.com> Date: Sat, 13 Jan 2024 02:16:32 +0300 Subject: [PATCH] Move API functions to a separate file --- webui/src/App.vue | 115 +------------------------------------- webui/src/services/api.js | 114 +++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 114 deletions(-) create mode 100644 webui/src/services/api.js diff --git a/webui/src/App.vue b/webui/src/App.vue index f98e0ef9..a5a79e53 100644 --- a/webui/src/App.vue +++ b/webui/src/App.vue @@ -25,120 +25,7 @@ import IconAvatarDefault from './components/icons/IconAvatarDefault.vue'; import ClientTransfer from './components/ClientTransfer.vue'; import VueApexCharts from 'vue3-apexcharts'; -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 getRelease() { - return this.call({ - method: 'get', - path: '/release', - }); - } - - 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 }, - }); - } -} +import API from './services/api'; export default { setup() { diff --git a/webui/src/services/api.js b/webui/src/services/api.js new file mode 100644 index 00000000..5fa82485 --- /dev/null +++ b/webui/src/services/api.js @@ -0,0 +1,114 @@ +export default 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 getRelease() { + return this.call({ + method: 'get', + path: '/release', + }); + } + + 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 }, + }); + } +}