fivesim-api
Version:
Node.js wrapper for the 5sim.net API - SMS verification service with TypeScript support
312 lines (311 loc) • 9.31 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserService = void 0;
const axios_1 = __importDefault(require("axios"));
const error_1 = require("../utils/error");
const guest_service_1 = require("./guest.service");
class UserService {
constructor(token) {
if (!token) {
throw new Error("API token is required");
}
this.client = axios_1.default.create({
baseURL: UserService.baseURL,
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/json",
},
});
}
/**
* Wait for order status to become RECEIVED
* @private
*/
async waitForOrderReceived(orderId) {
while (true) {
const order = await this.checkOrder(orderId);
if (order.status === "RECEIVED") {
return order;
}
if (order.status !== "PENDING") {
throw new Error(`Order failed with status: ${order.status}`);
}
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait 1 second
}
}
/**
* Find the best operator based on lowest cost and highest count
* @private
*/
async findBestOperator(country, product) {
const prices = await guest_service_1.GuestService.getPricesByCountryAndProduct(country, product);
const countryData = prices[country.toLowerCase()];
if (!countryData || !countryData[product]) {
throw new Error(`No pricing data found for ${country}/${product}`);
}
const operators = Object.entries(countryData[product]);
if (operators.length === 0) {
throw new Error(`No operators available for ${country}/${product}`);
}
// Sort by cost (ascending) and then by count (descending)
operators.sort(([, a], [, b]) => {
if (a.cost === b.cost) {
return (b.count || 0) - (a.count || 0); // Higher count first, default to 0 if undefined
}
return (a.cost || 0) - (b.cost || 0); // Lower cost first, default to 0 if undefined
});
return operators[0][0]; // Return the operator name
}
/**
* Get user profile information
*/
async getProfile() {
try {
const response = await this.client.get("/user/profile");
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Get order history with filtering options
*/
async getOrderHistory(options = {}) {
try {
const response = await this.client.get("/user/orders", { params: options });
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Get payment history with pagination
*/
async getPaymentHistory(options = {}) {
try {
const response = await this.client.get("/user/payments", { params: options });
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Get list of price limits
*/
async getPriceLimits() {
try {
const response = await this.client.get("/user/max-prices");
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Set price limit for a product
*/
async setPriceLimit(params) {
try {
const response = await this.client.post("/user/max-prices", params);
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Delete price limit for a product
*/
async deletePriceLimit(params) {
try {
const response = await this.client.delete("/user/max-prices", {
data: params,
});
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Buy activation number
*/
async buyActivationNumber(country, operator, product, options = {}) {
try {
const { wait = false, ...restOptions } = options;
const actualOperator = operator === "best" ? await this.findBestOperator(country, product) : operator;
const response = await this.client.get(`/user/buy/activation/${country}/${actualOperator}/${product}`, { params: restOptions });
let order = response.data;
if (wait) {
order = await this.waitForOrderReceived(order.id);
}
return order;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Buy hosting number
*/
async buyHostingNumber(country, operator, product, options = {}) {
try {
const { wait = false } = options;
const actualOperator = operator === "best" ? await this.findBestOperator(country, product) : operator;
const response = await this.client.get(`/user/buy/hosting/${country}/${actualOperator}/${product}`);
let order = response.data;
if (wait) {
order = await this.waitForOrderReceived(order.id);
}
return order;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Reuse a number
*/
async reuseNumber(product, number) {
try {
const response = await this.client.get(`/user/reuse/${product}/${number}`);
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Check order status and get SMS messages
*/
async checkOrder(id) {
try {
const response = await this.client.get(`/user/check/${id}`);
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Finish an order
*/
async finishOrder(id) {
try {
const response = await this.client.get(`/user/finish/${id}`);
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Cancel an order
*/
async cancelOrder(id) {
try {
const response = await this.client.get(`/user/cancel/${id}`);
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Ban an order
*/
async banOrder(id) {
try {
const response = await this.client.get(`/user/ban/${id}`);
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Get SMS inbox for an order
*/
async getSMSInbox(id) {
try {
const response = await this.client.get(`/user/sms/inbox/${id}`);
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Get vendor profile
*/
async getVendorProfile() {
try {
const response = await this.client.get("/user/vendor");
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Get vendor wallet balances
*/
async getVendorWallets() {
try {
const response = await this.client.get("/vendor/wallets");
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Get vendor order history
*/
async getVendorOrders(options = {}) {
try {
const response = await this.client.get("/vendor/orders", { params: options });
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Get vendor payment history
*/
async getVendorPayments(options = {}) {
try {
const response = await this.client.get("/vendor/payments", { params: options });
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
/**
* Create a payout request
*/
async createPayout(receiver, method, amount, fee) {
try {
const response = await this.client.post("/vendor/withdraw", {
receiver,
method,
amount,
fee,
});
return response.data;
}
catch (error) {
throw (0, error_1.createError)(error);
}
}
}
exports.UserService = UserService;
UserService.baseURL = "https://5sim.net/v1";