add accounts

This commit is contained in:
starnakin 2023-11-13 17:11:15 +01:00
parent 029467aa59
commit 0665d26c85
3 changed files with 42 additions and 14 deletions

26
src/accounts.js Normal file
View File

@ -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}

View File

@ -1,4 +1,6 @@
import axios from "axios"; import axios from "axios";
import * as urls from './urls.js';
import { Accounts } from "./accounts.js";
axios.defaults.withCredentials = true; axios.defaults.withCredentials = true;
@ -14,12 +16,14 @@ class Client
/** /**
* The axios client * The axios client
* @type AxiosInstance */ * @type AxiosInstance */
this.client = axios.create({ this._client = axios.create({
baseURL: url, baseURL: url,
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
}); });
this.accounts = new Accounts(this);
} }
/** /**
@ -37,23 +41,21 @@ class Client
*/ */
async _get(uri) async _get(uri)
{ {
let response = await this.client.get(uri); let response = this._client.get(uri);
return response; return response;
} }
/** /**
* A no consummer function to send post requests * A no consummer function to send post requests
*
* @param {string} uri - The path want to post * @param {string} uri - The path want to post
* @type {AxiosInstance}- The path want to post
*/ */
async _post(uri, data) async _post(uri, data)
{ {
/** /**
* The axios client * The axios client
* @type AxiosInstance */ * @type AxiosInstance */
console.log(data) let response = this._client.post(uri, data).catch(() => {});
let response = await this.client.post(uri, data)
console.log(response.status)
return response; return response;
} }
@ -65,17 +67,18 @@ class Client
*/ */
async login(username, password) 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; return response;
} }
} }
import {accounts_login} from "./urls.js"
let url = "http://0.0.0.0:8000/" let url = "http://0.0.0.0:8000/"
let client = new Client(url) 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} //console.log(response1.data)
export { Client }

View File

@ -1,9 +1,8 @@
let api = "" let api = ""
let accounts = api + "accounts/" let accounts = api + "accounts/"
let accounts_register = accounts + "register" let accounts_register = accounts + "register"
let accounts_login = accounts + "login" let accounts_login = accounts + "login"
let accounts_delete = accounts + "delete" let accounts_delete = accounts + "delete"
let accounts_change_password = accounts + "change_password" let accounts_change_password = accounts + "change_password"
export { accounts_login }; export { accounts_register, accounts_login, accounts_delete, accounts_change_password};