http-retrier
Version:
104 lines (103 loc) • 4.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpRetrierResponseInit = void 0;
const axios_1 = require("axios");
const statusCodes_js_1 = require("./statusCodes.js");
const xml_js_v2_1 = require("xml-js-v2");
// HttpRetrierResponseInit class
class HttpRetrierResponseInit {
/**
* Create a new HttpRetrierResponseInit object
* @param {string} url - The URL to request
* @param {string} method - The HTTP method to use
* @param {any} input - The input data to send with the request
*/
constructor(url, method = "GET", input) {
this.status = 0; // The HTTP status code
this.statusText = statusCodes_js_1.httpStatusCodes[0]; // The HTTP status message
this.headers = new axios_1.AxiosHeaders(); // The HTTP headers
this.attempt = 0; // The number of attempts made
// Validate input
if (typeof url !== "string")
throw new Error(`Invalid url: must be a string. Received ${typeof url}`);
if (typeof method !== "string")
throw new Error(`Invalid method: must be a string. Received ${typeof method}`);
// Initialize properties
this.url = url;
this.method = method;
this.input = input;
}
/**
* Update the status and statusText properties
* @param {HttpStatusCodes} status - The new status code
*/
set _status(status) {
this.status = status;
this.statusText = statusCodes_js_1.httpStatusCodes[status];
}
/**
* Update the response object with the given response and attempt
* @param {AxiosResponse} response - The response object to update
* @param {number} attempt - The attempt number
*/
update(response, attempt) {
// Validate response
if (response.status < 100 || response.status > 599)
throw new Error(`Invalid status code: ${this.status.toString()}.`);
if (attempt < 0)
throw new Error(`Invalid attempt: must be a positive integer. Received ${attempt}`);
// Update the response object
this._status = response.status;
this.headers = response.headers;
this.attempt = attempt;
this.output = response.data;
}
/**
* Update the response object with the given error and attempt
* @param {AxiosError | any} error - The error object to update
* @param {number} attempt - The attempt number
*/
error(error, attempt) {
// Initialize properties
if (error.isAxiosError) {
this._status = error.response?.status;
this.headers = error.response?.headers;
this.output = error.response?.data;
this.attempt = attempt;
}
else {
this._status = 599;
this.headers = new axios_1.AxiosHeaders();
this.output = error.message;
this.attempt = attempt;
}
}
/**
* Create a new HttpRetrierResponse object
* @param {OutputConverter} outputConverter - The output converter to use
* @returns {HttpRetrierResponse} A new HttpRetrierResponse object
*/
create(outputConverter = {}) {
// Validate outputConverter
if (typeof outputConverter?.custome !== "function")
throw new Error(`Invalid custome: must be a function. Received ${typeof outputConverter?.custome}`);
// convert xml output to json if xml2json is true
if (outputConverter?.xml2json === true)
this.output = (0, xml_js_v2_1.xml2js)(this.output);
// convert output to json if custome is a function
if (outputConverter?.custome)
this.output = outputConverter.custome(this.output);
// Return the response object
return {
url: this.url,
method: this.method,
status: this.status,
statusText: this.statusText,
headers: this.headers,
attempt: this.attempt,
input: this.input,
output: outputConverter?.custome ? outputConverter.custome(this.output) : this.output
};
}
}
exports.HttpRetrierResponseInit = HttpRetrierResponseInit;