"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 http_1 = __importDefault(require("http"));
const https_1 = __importDefault(require("https"));
/**
 * HTTP工具类
 */
class HttpUtil {
    static get(url, config) {
        return __awaiter(this, void 0, void 0, function* () {
            return (yield this.axiosInstance.get(url, config)).data;
        });
    }
    static getBuffer(url, config) {
        return __awaiter(this, void 0, void 0, function* () {
            if (config)
                config.responseType = 'arraybuffer';
            else
                config = { responseType: 'arraybuffer' };
            return yield this.get(url, config);
        });
    }
    static getStream(url, config) {
        return __awaiter(this, void 0, void 0, function* () {
            if (config)
                config.responseType = 'stream';
            else
                config = { responseType: 'stream' };
            return yield this.get(url, config);
        });
    }
    static post(url, data, config) {
        return __awaiter(this, void 0, void 0, function* () {
            return (yield this.axiosInstance.post(url, data, config)).data;
        });
    }
    static postJson(url, data, config) {
        return __awaiter(this, void 0, void 0, function* () {
            if (config) {
                if (config.headers) {
                    config.headers['Content-Type'] = 'application/json;charset=utf-8';
                }
                else {
                    config.headers = { 'Content-Type': 'application/json;charset=utf-8' };
                }
            }
            else {
                config = { headers: { 'Content-Type': 'application/json;charset=utf-8' } };
            }
            return yield this.post(url, data, config);
        });
    }
}
exports.default = HttpUtil;
HttpUtil.axiosInstance = axios_1.default.create({
    httpAgent: new http_1.default.Agent({ timeout: 5000 }),
    httpsAgent: new https_1.default.Agent({ timeout: 5000 }),
});
