tn-api.js/src/CreditsManager.ts

77 lines
3 KiB
TypeScript
Raw Normal View History

2023-12-04 19:21:33 +01:00
import axios from "axios";
export default class CreditsManager {
token: string | undefined;
URL = "https://api.thundernetwork.org";
constructor(customURL: string, token: string | undefined) {
this.URL = customURL || this.URL;
this.token = token;
}
/**
2023-12-07 17:15:38 +01:00
* Retrieves the balance for a given user ID.
2023-12-04 19:21:33 +01:00
*
2023-12-07 17:15:38 +01:00
* @param {string|number} id - The ID of the user.
2023-12-04 19:21:33 +01:00
* @return {Promise<number>} The balance of the user.
*/
2023-12-07 13:10:33 +01:00
async balance(id: string|number) {
2023-12-04 19:21:33 +01:00
let req = await axios.get(`${this.URL}/credits/${id}`);
2023-12-07 17:15:38 +01:00
if (req.status == 404) throw new Error("User not Found");
return req.data.credits;
}
/**
2023-12-09 16:00:08 +01:00
* Sets the credit number for a given ID.
2023-12-07 17:15:38 +01:00
*
2023-12-09 16:00:08 +01:00
* @param {string|number} id - The ID of the user.
* @param {number} number - The new credit number.
2023-12-07 17:15:38 +01:00
*/
2023-12-09 16:00:08 +01:00
async set(id: string|number, number: number) {
2023-12-09 14:14:19 +01:00
let req = await axios.post(`${this.URL}/credits/${id}`, { number }, { headers: { Authorization: `Bearer ${this.token}` } });
2023-12-09 16:00:08 +01:00
if (req.status == 403) throw new Error("No Permission");
2023-12-07 17:15:38 +01:00
if (req.status == 404) throw new Error("User not Found");
2023-12-09 14:14:19 +01:00
}
2023-12-09 16:00:08 +01:00
/**
* Verifies the given transaction.
*
* @param {number} code - The code of the transaction to be verified.
* @return {void} - This function does not return anything.
*/
async verifyTransaction(code: number) {
let req = await axios.post(`${this.URL}/credits/verify/${code}`, {}, { headers: { Authorization: `Bearer ${this.token}` } });
if (req.status == 403) throw new Error("No Permission");
if (req.status == 404) throw new Error("Transaction not Found");
}
/**
* Retrieves a transaction by its code.
*
* @param {number} code - The code of the transaction.
* @return {Promise<object>} The transaction object.
*/
async getTransaction(code: number) {
let req = await axios.get(`${this.URL}/credits/verify/${code}`);
if (req.status == 404) throw new Error("Transaction not Found");
return req.data.transaction;
2023-12-04 19:21:33 +01:00
}
2023-12-09 20:20:30 +01:00
/**
* Sends a payment request to the specified user.
*
* @param {number} id - The ID of the user making the payment.
* @param {number} toPayID - The ID of the user to whom the payment is being made.
* @param {number} amount - The amount of the payment.
* @return {void}
*/
async pay(id: number, toPayID: number, amount: number) {
2023-12-09 22:12:35 +01:00
if (amount < 0) throw new Error("Invalid Amount");
2023-12-09 22:40:53 +01:00
let req = await axios.patch(`${this.URL}/credits/${id}`, { amount, to: toPayID }, { headers: { Authorization: `Bearer ${this.token}` } }).catch();
if (req.data.error == "TOO_MANY_CREDITS") throw new Error("Not Enought Credits");
if (req.data.error == "NO_NEGATIVE_AMOUNT") throw new Error("Invalid Amount");
2023-12-09 22:24:03 +01:00
else if (req.status == 403) throw new Error("No Permission");
2023-12-09 20:20:30 +01:00
if (req.status == 404) throw new Error("User not Found");
}
2023-12-04 19:21:33 +01:00
}