tn-api.js/src/CreditsManager.ts

38 lines
1.2 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-08 16:21:33 +01:00
* Sets the number of credits for a given user.
2023-12-07 17:15:38 +01:00
*
* @param {number} id - The ID to set.
* @return {Promise<number>} - The credits of the user.
*/
2023-12-09 14:14:19 +01:00
async set(id: number, number: number) {
let req = await axios.post(`${this.URL}/credits/${id}`, { number }, { headers: { Authorization: `Bearer ${this.token}` } });
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
}
async verify(code: number) {
await axios.get(`${this.URL}/credits/verify/${code}`, { headers: { Authorization: `Bearer ${this.token}` } });
2023-12-04 19:21:33 +01:00
}
}