http-retrier
Version:
77 lines (76 loc) • 3.04 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpRetrierRequestInit = exports.HttpRetrierSettingsInit = void 0;
/**
* HttpRetrierSettingsInit class
* This class is used to initialize the settings for the HttpRetrier.
* It contains default values for the settings and a method to create the settings object.
*/
class HttpRetrierSettingsInit {
/**
* Constructor for the HttpRetrierSettingsInit class
* @param {Partial<HttpRetrierSettingsInit>} settings - The settings to initialize the retrier with
*/
constructor(settings = {}) {
this.title = "HttpRetrier"; // The title of the retrier
this.maxAttempts = 3; // The maximum number of attempts to make before giving up
this.delay = 100; // The delay between attempts in milliseconds
this.exponentialBackoff = true; // Whether to use exponential backoff for the delay
this.outputConverter = {}; // The output converter to use
this.logging = {}; // The logging object to use
this.retryOn = function (response) { return response.status >= 500; }; // The function to determine whether to retry on a given response
Object.assign(this, settings);
}
/**
* Create a new HttpRetrierSettings object
* @param {Record<string, any>} settings - The settings to initialize the retrier with
* @returns {HttpRetrierSettings} A new HttpRetrierSettings object
*/
create(settings = {}) {
// Initialize the settings object with default values
for (const key of Object.keys(settings)) {
if (Object.prototype.hasOwnProperty.call(this, key)) {
// @ts-ignore
this[key] = settings[key];
}
}
// return the settings object
return {
title: this.title,
maxAttempts: this.maxAttempts,
delay: this.delay,
exponentialBackoff: this.exponentialBackoff,
outputConverter: this.outputConverter,
logging: this.logging,
retryOn: this.retryOn
};
}
}
exports.HttpRetrierSettingsInit = HttpRetrierSettingsInit;
/**
* HttpRetrierRequestInit class
* This class is used to initialize the request for the HttpRetrier.
* It contains the URL and options for the request and a method to create the request object.
*/
class HttpRetrierRequestInit {
/**
* Constructor for the HttpRetrierRequestInit class
* @param {string} url - The URL to make the request to
* @param {HttpRetrierOptions} options - The options for the request
*/
constructor(url, options) {
this.url = url;
this.options = options;
}
/**
* Create a new AxiosRequestConfig object
* @returns {AxiosRequestConfig} A new AxiosRequestConfig object
*/
create() {
const request = {};
request.url = this.url;
Object.assign(request, this.options);
return request;
}
}
exports.HttpRetrierRequestInit = HttpRetrierRequestInit;