UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

57 lines 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getStandardRetryConfig = exports.getExponentialBackoffDelay = exports.shouldRetryRequest = void 0; const axios_retry_1 = require("axios-retry"); /** * Determines whether a request should be retried based on the error response * @param error The axios error object * @returns boolean indicating whether to retry the request */ function shouldRetryRequest(error) { var _a, _b, _c, _d, _e, _f, _g; // Check for network or idempotent request errors (built-in axios-retry condition) if (axios_retry_1.default.isNetworkOrIdempotentRequestError(error)) { return true; } const status = (_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : 0; // Standard server errors that should always be retried if ([500, 502, 503, 504].includes(status)) { return true; } // Retry DELETE requests that receive a 409 Conflict if (status === 409 && ((_d = (_c = error.config) === null || _c === void 0 ? void 0 : _c.method) === null || _d === void 0 ? void 0 : _d.toLowerCase()) === 'delete') { return true; } // Retry 429 Too Many Requests only if NOT quota related if (status === 429) { const errorMessage = (_f = (_e = error === null || error === void 0 ? void 0 : error.response) === null || _e === void 0 ? void 0 : _e.data) === null || _f === void 0 ? void 0 : _f.message; return !((_g = errorMessage === null || errorMessage === void 0 ? void 0 : errorMessage.toLowerCase()) === null || _g === void 0 ? void 0 : _g.includes('quota')); } return false; } exports.shouldRetryRequest = shouldRetryRequest; /** * Calculates exponential backoff delay with jitter * @param retryCount Current retry attempt number * @returns Delay in milliseconds */ function getExponentialBackoffDelay(retryCount) { const baseDelay = 1000; // Initial delay in ms const maxDelay = 20000; // Maximum delay in ms const jitter = Math.random() * 1000; // Random jitter in ms return Math.min(baseDelay * Math.pow(2, retryCount) + jitter, maxDelay); } exports.getExponentialBackoffDelay = getExponentialBackoffDelay; /** * Creates a standard axios-retry configuration * @returns axios-retry configuration object */ function getStandardRetryConfig() { return { retries: 10, retryCondition: shouldRetryRequest, retryDelay: getExponentialBackoffDelay, }; } exports.getStandardRetryConfig = getStandardRetryConfig; //# sourceMappingURL=axiosRetry.js.map