UNPKG

mnotify-ts-sdk

Version:

Third-Party TypeScript SDK for mNotify BMS API

87 lines 4.07 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MNotifyClient = void 0; const axios_1 = __importDefault(require("axios")); const MNotifyError_1 = require("../errors/MNotifyError"); /** * Core HTTP client for mNotify API communication * * @remarks * This class handles all low-level HTTP requests to the mNotify API, * including authentication, retries, and error handling. */ class MNotifyClient { /** * Creates a new MNotifyClient instance * @param config - Configuration object * @param config.apiKey - Your mNotify API key * @param config.baseUrl - Base API URL (default: 'https://api.mnotify.com/api') * @param config.timeout - Request timeout in ms (default: 10000) * @param config.maxRetries - Maximum retry attempts for failed requests (default: 3) */ constructor(config) { this.maxRetries = config.maxRetries || 3; this.axiosInstance = axios_1.default.create({ baseURL: config.baseUrl || "https://api.mnotify.com/api", timeout: config.timeout || 100000, headers: { Authorization: config.apiKey, "Content-Type": "application/json", Accept: "application/json", }, params: { key: config.apiKey }, }); } /** * Makes an HTTP request to the mNotify API * @param config - Axios request configuration * @param retryCount - Current retry attempt (used internally) * @param T - Type of the response data * @returns Promise with the parsed response data * @throws {MNotifyError} When API returns an error response * * @example * ```typescript * const client = new MNotifyClient({ apiKey: 'your-key' }); * const response = await client.request<T>({ * method: 'GET', * url: '/account/balance' * }); * ``` */ request(config_1) { return __awaiter(this, arguments, void 0, function* (config, retryCount = 0) { var _a, _b, _c, _d, _e; try { const response = yield this.axiosInstance(config); return response.data; } catch (error) { if (axios_1.default.isAxiosError(error)) { if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 429 && retryCount < this.maxRetries) { const retryAfter = Number.parseInt(error.response.headers["retry-after"] || "1") * 1000; yield new Promise((resolve) => setTimeout(resolve, retryAfter)); return this.request(config, retryCount + 1); } throw new MNotifyError_1.MNotifyError(((_c = (_b = error.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.message) || error.message, ((_d = error.response) === null || _d === void 0 ? void 0 : _d.status) || 500, (_e = error.response) === null || _e === void 0 ? void 0 : _e.data); } throw error; } }); } } exports.MNotifyClient = MNotifyClient; //# sourceMappingURL=MNotifyClient.js.map