From 0665d26c8563d01042831a8ea03aafe143b72769 Mon Sep 17 00:00:00 2001 From: starnakin Date: Mon, 13 Nov 2023 17:11:15 +0100 Subject: [PATCH] add accounts --- src/accounts.js | 26 ++++++++++++++++++++++++++ src/client.js | 27 +++++++++++++++------------ src/urls.js | 3 +-- 3 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 src/accounts.js diff --git a/src/accounts.js b/src/accounts.js new file mode 100644 index 0000000..fcd9173 --- /dev/null +++ b/src/accounts.js @@ -0,0 +1,26 @@ +//import Client from "./client.js" +import * as urls from './urls.js'; + +class Accounts +{ + /** + * @param {Client} client - the client + */ + constructor(client) + { + this.client = client; + } + + /** + * Create an accounts + * + * @param {string} username - The username of the accounts + * @param {string} password - The password of the accounts + */ + async create(username, password) + { + this.client._post(urls.accounts_register, {'username': username, 'password': password}) + } +} + +export {Accounts} \ No newline at end of file diff --git a/src/client.js b/src/client.js index 9ff581d..185f45f 100644 --- a/src/client.js +++ b/src/client.js @@ -1,4 +1,6 @@ import axios from "axios"; +import * as urls from './urls.js'; +import { Accounts } from "./accounts.js"; axios.defaults.withCredentials = true; @@ -14,12 +16,14 @@ class Client /** * The axios client * @type AxiosInstance */ - this.client = axios.create({ + this._client = axios.create({ baseURL: url, headers: { 'Content-Type': 'application/json' - } + } }); + + this.accounts = new Accounts(this); } /** @@ -37,23 +41,21 @@ class Client */ async _get(uri) { - let response = await this.client.get(uri); + let response = this._client.get(uri); return response; } /** * A no consummer function to send post requests - * * @param {string} uri - The path want to post + * @type {AxiosInstance}- The path want to post */ async _post(uri, data) { /** * The axios client * @type AxiosInstance */ - console.log(data) - let response = await this.client.post(uri, data) - console.log(response.status) + let response = this._client.post(uri, data).catch(() => {}); return response; } @@ -65,17 +67,18 @@ class Client */ async login(username, password) { - let response = this._post(accounts_login, {username: username, password: password}) + let response = this._post(urls.accounts_login, {'username': username, 'password': password}) return response; } } -import {accounts_login} from "./urls.js" - let url = "http://0.0.0.0:8000/" let client = new Client(url) -let response = await client.login("bozoman", "man") +let response1 = await client.accounts.create("bozoman", "man") +let response2 = await client?.login("bozoman", "man") -export {Client} \ No newline at end of file +//console.log(response1.data) + +export { Client } \ No newline at end of file diff --git a/src/urls.js b/src/urls.js index a54f7cc..0137609 100644 --- a/src/urls.js +++ b/src/urls.js @@ -1,9 +1,8 @@ let api = "" - let accounts = api + "accounts/" let accounts_register = accounts + "register" let accounts_login = accounts + "login" let accounts_delete = accounts + "delete" let accounts_change_password = accounts + "change_password" -export { accounts_login }; \ No newline at end of file +export { accounts_register, accounts_login, accounts_delete, accounts_change_password}; \ No newline at end of file