"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = require("axios");
const cm_tools_1 = require("refs/cm-tools");
const querystring = require('querystring');
/**
 * 基于Axios框架实现的Http工具
 * @author wufl2019@qq.com
 * @date 2020-06-23
 */
class HttpUtils {
    constructor(_timeout = 5000) {
        this.axios = axios_1.default.create({
            timeout: _timeout
        });
    }
    /**
     * @desc get请求
     * @param {String} path 请求api路径
     * @param {Object} data 参数对象
     * @param {Object} config 局部配置 {headers: {'X-Custom-Header': 'foobar'}}
     */
    get(url, data = {}, headers = {}) {
        return __awaiter(this, void 0, void 0, function* () {
            if (cm_tools_1.StringUtils.isEmpty(url)) {
                throw Error('url can not empty !!');
            }
            let config = {};
            config['headers'] = headers;
            const response = yield this.axios.get(url, Object.assign({ params: data }, config));
            return response;
        });
    }
    /**
     * @desc post表单请求
     * @param {String} path 请求api路径
     * @param {Object} data 参数对象
     * @param {Object} config 局部配置 {headers: {'X-Custom-Header': 'foobar'}}
     */
    postForm(url, data = {}, headers = {}) {
        return __awaiter(this, void 0, void 0, function* () {
            let config = {};
            config['headers'] = headers;
            config['headers']['content-type'] = 'application/x-www-form-urlencoded;charset=utf-8';
            const response = yield this.axios.post(url, querystring.encode(data), config);
            return response;
        });
    }
    /**
     * @desc post表单请求
     * @param {String} path 请求api路径
     * @param {Object} data 参数对象
     * @param {Object} config 局部配置 {headers: {'X-Custom-Header': 'foobar'}}
     */
    postJson(url, params = {}, bodyObj = {}, headers = {}) {
        return __awaiter(this, void 0, void 0, function* () {
            let config = {}, requestUrl = url;
            config['headers'] = headers;
            config['headers']['content-type'] = 'application/json;charset=utf-8';
            if (params && Object.keys(params).length) {
                requestUrl = requestUrl + '?' + querystring.encode(params);
            }
            const response = yield this.axios.post(requestUrl, bodyObj, config);
            return response;
        });
    }
    /**
     * @desc post表单请求
     * @param {String} path 请求api路径
     * @param {Object} data 参数对象
     * @param {Object} config 局部配置 {headers: {'X-Custom-Header': 'foobar'}}
     */
    post(url, data = {}, headers = {}, contentType) {
        return __awaiter(this, void 0, void 0, function* () {
            let config = {};
            config['headers'] = headers;
            config['headers']['content-type'] = contentType;
            const response = yield this.axios.post(url, data, config);
            return response;
        });
    }
}
exports.HttpUtils = HttpUtils;
exports.default = new HttpUtils();
//# sourceMappingURL=HttpUtils.js.map