"use strict";
/**
 * axios帮助模块
 * @date 2022-03
 * @author 何华东
 */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateApiErrorFromAxiosResponse = exports.generateApiErrorFromAxiosError = exports.throwApiError = exports.ApiError = void 0;
/**
 * API异常类
 *
 * 主要是为代替AxiosError异常，AxiosError异常属性较多，可读性差
 */
var ApiError = /** @class */ (function (_super) {
    __extends(ApiError, _super);
    function ApiError() {
        var _this = _super.call(this) || this;
        _this.name = 'ApiError';
        return _this;
    }
    return ApiError;
}(Error));
exports.ApiError = ApiError;
/**
 * 抛出ApiError异常
 *
 * 如果指定的异常对象类型为AxiosError，则转化为ApiError异常对象并抛出，其他异常则直接抛出
 *
 * 主要应用于Axios请求拦截器和响应拦截器中的异常处理
 * @param error 指定的异常对象
 */
function throwApiError(error) {
    if (error.isAxiosError)
        throw generateApiErrorFromAxiosError(error);
    throw error;
}
exports.throwApiError = throwApiError;
/**
 * 从指定的AxiosError异常对象生成一个ApiError异常对象
 * @param error 指定的AxiosError异常对象
 * @returns
 */
function generateApiErrorFromAxiosError(error) {
    var _a, _b, _c;
    var apiError = new ApiError();
    apiError.name = error.name;
    apiError.message = error.message;
    apiError.requestUrl = ((_a = error.config.url) === null || _a === void 0 ? void 0 : _a.startsWith('http')) ? error.config.url : (error.config.baseURL + (error.config.url || ''));
    apiError.requestHeaders = error.config.headers;
    apiError.requestData = error.config.data;
    apiError.code = error.code;
    apiError.statusCode = (_b = error.response) === null || _b === void 0 ? void 0 : _b.status;
    apiError.apiResponse = (_c = error.response) === null || _c === void 0 ? void 0 : _c.data;
    return apiError;
}
exports.generateApiErrorFromAxiosError = generateApiErrorFromAxiosError;
/**
 * 从指定的AxiosResponse对象生成一个ApiError对象
 *
 * 主要应用于调用了第三方API但返回错误信息，把错误信息转成APIError异常
 * @param axiosResponse 指定的AxiosResponse对象
 * @param message 错误信息
 * @returns
 */
function generateApiErrorFromAxiosResponse(axiosResponse, message) {
    var _a;
    var apiError = new ApiError();
    apiError.message = message;
    apiError.requestUrl = ((_a = axiosResponse.config.url) === null || _a === void 0 ? void 0 : _a.startsWith('http')) ? axiosResponse.config.url : (axiosResponse.config.baseURL + (axiosResponse.config.url || ''));
    apiError.requestHeaders = axiosResponse.config.headers;
    apiError.requestData = axiosResponse.config.data;
    apiError.statusCode = axiosResponse.status;
    apiError.apiResponse = axiosResponse.data;
    return apiError;
}
exports.generateApiErrorFromAxiosResponse = generateApiErrorFromAxiosResponse;
//# sourceMappingURL=AxiosUtils.js.map