"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 });
exports.ApiClient = void 0;
const axios_1 = require("axios");
const des3Util_1 = require("./des3Util");
const uuid_1 = require("uuid");
/**
 * 非标接口客户端
 */
class ApiClient {
    constructor(opts) {
        let axiosInstance = axios_1.default.create({
            baseURL: opts.url,
            timeout: opts.timeout || 10000,
            headers: {
                "Content-Type": "application/json;charset=utf-8",
                version: opts.version || "1.0.0",
            },
        });
        axiosInstance.interceptors.request.use((config) => {
            let headers = config.headers || {};
            if (!headers["trace-id"])
                headers["trace-id"] = (0, uuid_1.v4)().replace(/-/g, "");
            config.headers = headers;
            if (!opts.secure && config["secure"] !== true)
                return config;
            if (config["secure"] === false)
                return config;
            let data = config.data || {};
            let new_data = {
                data: des3Util_1.Des3Util.encrypt(data, opts.key, opts.iv),
            };
            if (data.imageInfo)
                new_data["imageInfo"] = data.imageInfo;
            config.data = new_data;
            return config;
        });
        this.axiosInstance = axiosInstance;
    }
    post(url, data, config) {
        return __awaiter(this, void 0, void 0, function* () {
            let res = yield this.axiosInstance.post(url, data, config);
            return res.data;
        });
    }
}
exports.ApiClient = ApiClient;
