"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 CommUtils_1 = require("../utils/CommUtils");
class DefaultClient {
    constructor(baseURL) {
        this._axios = axios_1.default.create({
            timeout: 10000,
            baseURL: baseURL
        });
        //默认拦截器
        this._axios.interceptors.response.use((res) => {
            const data = res.data;
            if (res.status === 200) {
                return data;
            }
            return Promise.reject(res);
        });
    }
    /**
     * 设置axios Request拦截器
     *
     * @param onFulfilled
     * @param onRejected
     */
    setRequestInterceptors(onFulfilled, onRejected) {
        this._axios.interceptors.request.use((config) => __awaiter(this, void 0, void 0, function* () {
            config.action = config.url;
            config.url = config.url;
            config.inParams = config.params || config.data || {};
            return yield onFulfilled(config);
        }), onRejected);
    }
    /**
     * 设置axios Response拦截器
     *
     * @param onFulfilled
     * @param onRejected
     */
    setResponsetInterceptors(onFulfilled, onRejected) {
        this._axios.interceptors.response.use((response) => {
            return onFulfilled(response);
        }, onRejected);
    }
    /**
     * 微服务请求
     *
     * @param url
     * @param method
     * @param contentType
     * @param headers
     * @param parameters
     */
    request(url, method, contentType, headers, parameters) {
        if (this._isUploadFileRequest(contentType)) {
            return this._upload(url, contentType, headers, parameters);
        }
        else {
            return this._request(url, method, contentType, headers, parameters);
        }
    }
    /**
     * ajax 请求
     *
     * @param method
     * @param contentType
     * @param headers
     * @param parameters
     */
    _request(url, method, contentType, headers, parameters) {
        const config = {
            url
        };
        config.method = method;
        config.headers = this._parseHeaders(contentType, headers);
        if (method === 'GET') {
            config.params = parameters;
        }
        else {
            config.data = parameters;
        }
        return this._axios.request(config);
    }
    /**
     * 文件上传
     */
    _upload(url, contentType, headers, parameters) {
        const config = {
            url
        };
        config.headers = this._parseHeaders(contentType, headers);
        if (CommUtils_1.CommUtils.isNodeClient()) {
            var FormData = require('form-data');
        }
        const formData = new FormData();
        for (let attr in parameters) {
            formData.append(attr, parameters[attr]);
        }
        return this._axios.post('', formData, config);
    }
    _isUploadFileRequest(contentType) {
        return contentType.indexOf("multipart") > -1;
    }
    _parseHeaders(contentType, headers) {
        const _headers = Object.assign({}, headers);
        _headers['Content-Type'] = contentType;
        return _headers;
    }
}
exports.DefaultClient = DefaultClient;
