From 148b771d9a8923a73a0491cb05130e35812d6402 Mon Sep 17 00:00:00 2001 From: Valerian Saliou Date: Sun, 10 Nov 2024 16:27:27 +0100 Subject: [PATCH] feat: large refactor to commonize axios client --- config/development.json | 6 +- config/production.json | 6 +- src/api/index.ts | 44 ++++ src/api/login.ts | 52 ---- src/api/{ => providers}/advancedBackup.ts | 0 src/api/{ => providers}/advancedNetwork.ts | 4 +- src/api/{ => providers}/advancedSecurity.ts | 4 +- src/api/{ => providers}/connect.ts | 4 +- .../{ => providers}/customizationReactions.ts | 4 +- .../{ => providers}/customizationWorkspace.ts | 4 +- src/api/{ => providers}/global.ts | 4 +- src/api/providers/login.ts | 47 ++++ .../{ => providers}/serverConfiguration.ts | 4 +- src/api/{ => providers}/teamMembers.ts | 4 +- src/assemblies/start/StartLoginForm.vue | 21 +- src/components/form/FormSelect.vue | 55 ++--- src/store/tables/account.ts | 225 +----------------- src/store/tables/customizationEmojis.ts | 64 ++--- src/store/tables/customizationWorkspace.ts | 69 +++--- src/views/start/StartLogin.vue | 36 +-- vite.config.ts | 4 + 21 files changed, 230 insertions(+), 431 deletions(-) create mode 100644 src/api/index.ts delete mode 100644 src/api/login.ts rename src/api/{ => providers}/advancedBackup.ts (100%) rename src/api/{ => providers}/advancedNetwork.ts (90%) rename src/api/{ => providers}/advancedSecurity.ts (94%) rename src/api/{ => providers}/connect.ts (94%) rename src/api/{ => providers}/customizationReactions.ts (93%) rename src/api/{ => providers}/customizationWorkspace.ts (93%) rename src/api/{ => providers}/global.ts (87%) create mode 100644 src/api/providers/login.ts rename src/api/{ => providers}/serverConfiguration.ts (95%) rename src/api/{ => providers}/teamMembers.ts (95%) diff --git a/config/development.json b/config/development.json index 8199865..e2b3ea0 100644 --- a/config/development.json +++ b/config/development.json @@ -1,3 +1,7 @@ { - "environment": "development" + "environment": "development", + + "api": { + "endpoint": "http://127.0.0.1:8000/v1/" + } } diff --git a/config/production.json b/config/production.json index 2dc1963..5a3955e 100644 --- a/config/production.json +++ b/config/production.json @@ -1,3 +1,7 @@ { - "environment": "production" + "environment": "production", + + "api": { + "endpoint": "/api/v1/" + } } diff --git a/src/api/index.ts b/src/api/index.ts new file mode 100644 index 0000000..10fd8e1 --- /dev/null +++ b/src/api/index.ts @@ -0,0 +1,44 @@ +/* + * This file is part of prose-pod-dashboard + * + * Copyright 2024, Prose Foundation + */ + +/************************************************************************** + * IMPORTS + * ************************************************************************* */ + +// NPM +import { default as axios, AxiosInstance } from "axios"; + +// PROJECT: COMMONS +import CONFIG from "@/commons/config"; + +/************************************************************************** + * STORE + * ************************************************************************* */ + +class API { + public readonly client: AxiosInstance; + + constructor() { + // Initialize API HTTP client + this.client = this.__createClient(); + } + + private __createClient(): AxiosInstance { + return axios.create({ + baseURL: CONFIG.api.endpoint, + + headers: { + "Content-Type": "application/json" + } + }); + } +} + +/************************************************************************** + * EXPORTS + * ************************************************************************* */ + +export default new API(); diff --git a/src/api/login.ts b/src/api/login.ts deleted file mode 100644 index 610d7e2..0000000 --- a/src/api/login.ts +++ /dev/null @@ -1,52 +0,0 @@ -/* - * This file is part of prose-pod-dashboard - * - * Copyright 2024, Prose Foundation - */ - -import axios from "axios"; - -class Login { - async authenticate(password: string, attemptOnceOnly = false): Promise { - // Incomplete parameters? - if (!password) { - throw new Error("Please provide a password"); - } - - // Attempt connecting (this might fail, eg. wrong credentials) - await axios.post("/login", { - password - }); - - // Store credentials AFTER connection attempt succeeded - if (attemptOnceOnly === true) { - // doStoreCredentials(); - } - - // // Another connection active? - // if ( - // Store.$session.connected === true || - // Store.$session.connecting === true - // ) { - // throw new Error("Another connection already exists"); - // } - - // // Make credentials store handler - // const doStoreCredentials = () => { - // // Store credentials - // this.__credentials = { - // jid, - // password - // }; - - // // Store authentication JID - // this.authenticationJID = this.__credentials?.jid; - // }; - - // // Store credentials BEFORE attempting to connect? (this allows retries) - // if (attemptOnceOnly !== true) { - // doStoreCredentials(); - } -} - -export default new Login(); diff --git a/src/api/advancedBackup.ts b/src/api/providers/advancedBackup.ts similarity index 100% rename from src/api/advancedBackup.ts rename to src/api/providers/advancedBackup.ts diff --git a/src/api/advancedNetwork.ts b/src/api/providers/advancedNetwork.ts similarity index 90% rename from src/api/advancedNetwork.ts rename to src/api/providers/advancedNetwork.ts index c2d54ec..d3185c3 100644 --- a/src/api/advancedNetwork.ts +++ b/src/api/providers/advancedNetwork.ts @@ -13,7 +13,7 @@ import axios from "axios"; const NETWORK_CHECK_URL = "/network/checks"; //// EFFACER ?? -class AdvancedNetwork { +class APIAdvancedNetwork { /** SERVER **/ /** NETWORK **/ @@ -26,4 +26,4 @@ class AdvancedNetwork { } } -export default new AdvancedNetwork(); +export default new APIAdvancedNetwork(); diff --git a/src/api/advancedSecurity.ts b/src/api/providers/advancedSecurity.ts similarity index 94% rename from src/api/advancedSecurity.ts rename to src/api/providers/advancedSecurity.ts index 2701c5c..ee26e9d 100644 --- a/src/api/advancedSecurity.ts +++ b/src/api/providers/advancedSecurity.ts @@ -6,7 +6,7 @@ import axios from "axios"; -export class AdvancedSecurity { +export class APIAdvancedSecurity { /** CONFIG **/ async getWorkspaceConfig(): Promise { @@ -58,4 +58,4 @@ export class AdvancedSecurity { } } -export default new CustomizationWorkspace(); +export default new APIAdvancedSecurity(); diff --git a/src/api/connect.ts b/src/api/providers/connect.ts similarity index 94% rename from src/api/connect.ts rename to src/api/providers/connect.ts index 33bf406..765e78e 100644 --- a/src/api/connect.ts +++ b/src/api/providers/connect.ts @@ -6,7 +6,7 @@ import axios from "axios"; -class Connect { +class APIConnect { /** CONFIG **/ async initializeAccount(): Promise { @@ -39,4 +39,4 @@ class Connect { } } -export default new Connect(); +export default new APIConnect(); diff --git a/src/api/customizationReactions.ts b/src/api/providers/customizationReactions.ts similarity index 93% rename from src/api/customizationReactions.ts rename to src/api/providers/customizationReactions.ts index 1640c4f..0c18a43 100644 --- a/src/api/customizationReactions.ts +++ b/src/api/providers/customizationReactions.ts @@ -13,7 +13,7 @@ import axios from "axios"; const REACTIONS_URL = "/workspace/reactions"; -class CustomizationReactions { +class APICustomizationReactions { /** REACTIONS **/ async getAllReactions(): Promise { @@ -46,4 +46,4 @@ class CustomizationReactions { } } -export default new CustomizationReactions(); +export default new APICustomizationReactions(); diff --git a/src/api/customizationWorkspace.ts b/src/api/providers/customizationWorkspace.ts similarity index 93% rename from src/api/customizationWorkspace.ts rename to src/api/providers/customizationWorkspace.ts index 9699004..49d02d8 100644 --- a/src/api/customizationWorkspace.ts +++ b/src/api/providers/customizationWorkspace.ts @@ -6,7 +6,7 @@ import axios from "axios"; -export class CustomizationWorkspace { +export class APICustomizationWorkspace { /** CONFIG **/ async getWorkspaceConfig(): Promise { @@ -58,4 +58,4 @@ export class CustomizationWorkspace { } } -export default new CustomizationWorkspace(); +export default new APICustomizationWorkspace(); diff --git a/src/api/global.ts b/src/api/providers/global.ts similarity index 87% rename from src/api/global.ts rename to src/api/providers/global.ts index 76c508d..f6e3d99 100644 --- a/src/api/global.ts +++ b/src/api/providers/global.ts @@ -6,7 +6,7 @@ import axios from "axios"; -class Global { +class APIGlobal { /** CONFIG **/ async getServerConfig(): Promise { @@ -18,4 +18,4 @@ class Global { } } -export default new Global(); +export default new APIGlobal(); diff --git a/src/api/providers/login.ts b/src/api/providers/login.ts new file mode 100644 index 0000000..c8f75fc --- /dev/null +++ b/src/api/providers/login.ts @@ -0,0 +1,47 @@ +/* + * This file is part of prose-pod-dashboard + * + * Copyright 2024, Prose Foundation + */ + +/************************************************************************** + * IMPORTS + * ************************************************************************* */ + +// PROJECT: API +import Api from "@/api"; + +/************************************************************************** + * INTERFACES + * ************************************************************************* */ + +interface LoginResponse { + token: string; +} + +/************************************************************************** + * API + * ************************************************************************* */ + +class APILogin { + async login(username: string, password: string): Promise { + return ( + await Api.client.post( + "/login", + {}, + { + auth: { + username, + password + } + } + ) + ).data; + } +} + +/************************************************************************** + * EXPORTS + * ************************************************************************* */ + +export default new APILogin(); diff --git a/src/api/serverConfiguration.ts b/src/api/providers/serverConfiguration.ts similarity index 95% rename from src/api/serverConfiguration.ts rename to src/api/providers/serverConfiguration.ts index b9c61d8..eab706a 100644 --- a/src/api/serverConfiguration.ts +++ b/src/api/providers/serverConfiguration.ts @@ -12,7 +12,7 @@ import axios from "axios"; const CONFIG_URL = "/server/config"; -class ServerConfiguration { +class APIServerConfiguration { /** MESSAGING **/ async resetMessagesConfig(): Promise { @@ -63,4 +63,4 @@ class ServerConfiguration { } } -export default new ServerConfiguration(); +export default new APIServerConfiguration(); diff --git a/src/api/teamMembers.ts b/src/api/providers/teamMembers.ts similarity index 95% rename from src/api/teamMembers.ts rename to src/api/providers/teamMembers.ts index 4e9b1f2..cdf78af 100644 --- a/src/api/teamMembers.ts +++ b/src/api/providers/teamMembers.ts @@ -6,7 +6,7 @@ import axios from "axios"; -class TeamMembers { +class APITeamMembers { /** INVITATIONS **/ async getAllInvitations(): Promise { @@ -53,4 +53,4 @@ class TeamMembers { } } -export default new TeamMembers(); +export default new APITeamMembers(); diff --git a/src/assemblies/start/StartLoginForm.vue b/src/assemblies/start/StartLoginForm.vue index 62770dc..24b493b 100644 --- a/src/assemblies/start/StartLoginForm.vue +++ b/src/assemblies/start/StartLoginForm.vue @@ -25,7 +25,7 @@ class="a-start-login-form__field" type="email" name="jid" - placeholder="Enter your Prose address…" + placeholder="Enter your Prose admin address…" size="ultra-large" autofocus ) @@ -66,15 +66,11 @@