UNPKG

modem-pay

Version:

A TypeScript SDK for integrating with the Modem Pay payment gateway, enabling seamless payment processing and financial services in your applications.

71 lines (70 loc) 3.66 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 }); const axios_1 = __importDefault(require("axios")); const utils_1 = require("../utils"); const error_1 = __importDefault(require("../core/error")); class BaseResource { constructor(apiKey, maxRetries, timeout, retryDelay = 1000) { // protected apiURL = "http://localhost:9090"; this.apiURL = "https://api.modempay.com"; this.apiKey = apiKey; this.timeout = timeout; this.maxRetries = Math.max(0, Math.min(3, maxRetries)); this.retryDelay = Math.max(100, retryDelay); this.enableRetryLogging = process.env.RETRY_LOGGING !== "false"; } getHeaders() { return { Authorization: `Bearer ${this.apiKey}` }; } request(config, retries = this.maxRetries) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { try { const timeoutSeconds = (_a = this.timeout) !== null && _a !== void 0 ? _a : 60; const clampedTimeout = Math.min(timeoutSeconds, 180); const { data } = yield (0, axios_1.default)(Object.assign(Object.assign({}, config), { baseURL: this.apiURL, headers: Object.assign(Object.assign({}, this.getHeaders()), config.headers), timeout: clampedTimeout * 1000 })); return data; } catch (error) { if (retries > 0 && this.isRetryableError(error)) { const attempt = this.maxRetries - retries + 1; const delay = this.calculateBackoff(attempt); if (this.enableRetryLogging) { console.log(`Retry attempt ${attempt} of ${this.maxRetries} after ${delay}ms delay`); } yield this.delay(delay); return this.request(config, retries - 1); } throw new error_1.default(JSON.stringify((0, utils_1.getErrorMessage)(error), null, 2), (_c = (_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.status) !== null && _c !== void 0 ? _c : 500); } }); } calculateBackoff(attempt) { const exponentialDelay = this.retryDelay * Math.pow(2, attempt); const jitter = Math.random() * Math.min(100, exponentialDelay * 0.1); return Math.min(exponentialDelay + jitter, BaseResource.MAX_DELAY); } isRetryableError(error) { return (axios_1.default.isAxiosError(error) && (!error.response || error.response.status >= 500)); } delay(ms) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => setTimeout(resolve, ms)); }); } } exports.default = BaseResource; BaseResource.MAX_DELAY = 30000;