UNPKG

fivesim-api

Version:

Node.js wrapper for the 5sim.net API - SMS verification service with TypeScript support

169 lines (168 loc) 5.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HostingOrderError = exports.OrderHasSMSError = exports.OrderExpiredError = exports.ServerError = exports.ValidationError = exports.InsufficientRatingError = exports.InsufficientBalanceError = exports.NoPhoneNumbersError = exports.NotFoundError = exports.RateLimitError = exports.AuthenticationError = exports.FiveSimError = void 0; exports.createError = createError; /** * Base class for all 5sim API errors */ class FiveSimError extends Error { constructor(message, status, response) { super(message); this.name = "FiveSimError"; this.status = status; this.response = response; } } exports.FiveSimError = FiveSimError; /** * Error thrown when authentication fails */ class AuthenticationError extends FiveSimError { constructor(message = "Authentication failed. Please check your API token.") { super(message, 401); this.name = "AuthenticationError"; } } exports.AuthenticationError = AuthenticationError; /** * Error thrown when rate limits are exceeded */ class RateLimitError extends FiveSimError { constructor(message = "Rate limit exceeded. Please slow down your requests.") { super(message, 429); this.name = "RateLimitError"; } } exports.RateLimitError = RateLimitError; /** * Error thrown when a resource is not found */ class NotFoundError extends FiveSimError { constructor(message = "The requested resource was not found.") { super(message, 404); this.name = "NotFoundError"; } } exports.NotFoundError = NotFoundError; /** * Error thrown when there are no available phone numbers */ class NoPhoneNumbersError extends FiveSimError { constructor(message = "No phone numbers available for the requested parameters.") { super(message, 400); this.name = "NoPhoneNumbersError"; } } exports.NoPhoneNumbersError = NoPhoneNumbersError; /** * Error thrown when the user has insufficient balance */ class InsufficientBalanceError extends FiveSimError { constructor(message = "Insufficient balance to complete the operation.") { super(message, 400); this.name = "InsufficientBalanceError"; } } exports.InsufficientBalanceError = InsufficientBalanceError; /** * Error thrown when the user has insufficient rating */ class InsufficientRatingError extends FiveSimError { constructor(message = "Insufficient rating to complete the operation.") { super(message, 400); this.name = "InsufficientRatingError"; } } exports.InsufficientRatingError = InsufficientRatingError; /** * Error thrown when an invalid parameter is provided */ class ValidationError extends FiveSimError { constructor(message) { super(message, 400); this.name = "ValidationError"; } } exports.ValidationError = ValidationError; /** * Error thrown when the server is unavailable */ class ServerError extends FiveSimError { constructor(message = "The 5sim server is currently unavailable. Please try again later.") { super(message, 503); this.name = "ServerError"; } } exports.ServerError = ServerError; /** * Error thrown when an order has expired */ class OrderExpiredError extends FiveSimError { constructor(message = "The order has expired.") { super(message, 400); this.name = "OrderExpiredError"; } } exports.OrderExpiredError = OrderExpiredError; /** * Error thrown when an order already has SMS */ class OrderHasSMSError extends FiveSimError { constructor(message = "The order already has SMS messages.") { super(message, 400); this.name = "OrderHasSMSError"; } } exports.OrderHasSMSError = OrderHasSMSError; /** * Error thrown when attempting operations on a hosting order that are not allowed */ class HostingOrderError extends FiveSimError { constructor(message = "This operation is not allowed for hosting orders.") { super(message, 400); this.name = "HostingOrderError"; } } exports.HostingOrderError = HostingOrderError; /** * Helper function to create appropriate error based on API response */ function createError(error) { if (!error.response) { return new FiveSimError("Network error occurred", 0, error); } const { status, data } = error.response; const message = (data === null || data === void 0 ? void 0 : data.message) || JSON.stringify(data); switch (status) { case 401: return new AuthenticationError(); case 404: return new NotFoundError(); case 429: return new RateLimitError(); case 503: return new ServerError(); case 400: if (message.includes("no free phones")) { return new NoPhoneNumbersError(); } if (message.includes("not enough user balance")) { return new InsufficientBalanceError(); } if (message.includes("not enough rating")) { return new InsufficientRatingError(); } if (message.includes("order expired")) { return new OrderExpiredError(); } if (message.includes("order has sms")) { return new OrderHasSMSError(); } if (message.includes("hosting order")) { return new HostingOrderError(); } return new ValidationError(message); default: return new FiveSimError(message, status, data); } }