"use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});const axios_1=__importDefault(require("axios")),hpagent_1=require("hpagent"),Middleware_1=__importDefault(require("./Middleware")),Logger_1=require("../../../common/Logger"),request_1=__importDefault(require("request"));class Request{constructor(e){this.instance=axios_1.default.create({baseURL:e.baseURL,headers:e.headers}),e.logger&&(this.instance.interceptors.request.use(Middleware_1.default.logger.request(e.logger)),this.instance.interceptors.response.use(Middleware_1.default.logger.response(e.logger))),this.instance.interceptors.request.use(Middleware_1.default.urlParams),this.instance.interceptors.request.use(Middleware_1.default.queryParams),this.instance.interceptors.response.use(Middleware_1.default.isSessionExpired(e)),this.instance.interceptors.response.use(Middleware_1.default.formatResponse(e)),this.instance.interceptors.response.use(Middleware_1.default.parseCookie)}genProxy(e){var t,r;let o,s,a;switch(e.type){case"direct":s=!1,o=void 0;break;case"system":s=void 0,o=void 0;break;case"manual":if(e.proxy.host&&e.proxy.port&&e.proxy.protocol){const i={headers:{}},n=e.proxy.auth?Buffer.from(`${e.proxy.auth.username}:${e.proxy.auth.password}`).toString("base64"):void 0;n&&(i["Proxy-Authorization"]="Basic "+n),a=new hpagent_1.HttpsProxyAgent({proxy:`${e.proxy.protocol}://${e.proxy.host}:${e.proxy.port}`,proxyRequestOptions:{headers:i},rejectUnauthorized:!1}),s={host:e.proxy.host,port:e.proxy.port,protocol:e.proxy.protocol,auth:(null===(t=e.proxy.auth)||void 0===t?void 0:t.username)?e.proxy.auth:void 0},o={proxy:`${e.proxy.protocol}://${e.proxy.host}:${e.proxy.port}`,auth:(null===(r=e.proxy.auth)||void 0===r?void 0:r.username)?e.proxy.auth:void 0}}else s=!1,o=void 0}return{proxyForAxios:s,proxyForRequest:o,httpsAgent:a}}fetch(e,t){let r;(0,Logger_1.logger)().info("Request","fetch","start",JSON.stringify({url:e,params:t}));const o=e.includes("/preview")||e.includes("/testing")||e.includes("/invoke/upload_log")||e.includes("/web_offline/upload")||"put"===t.method.toLowerCase(),s=this.genProxy(t.proxy),{httpsAgent:a}=s;if((0,Logger_1.logger)().info("Request","fetch","proxy",JSON.stringify(s)),o){"development"!==process.env.CLI_ENV&&"development"!==process.env.ELECTRON_ENV||console.log("使用request 库");const o=e.includes("/preview")||e.includes("/testing")||e.includes("/release")||e.includes("/invoke/upload_log")||e.includes("/web_offline/upload")?{formData:t.data}:{body:t.data,json:!0};r=e=>new Promise((r,s)=>{(0,request_1.default)(Object.assign({baseUrl:e.baseURL,method:t.method,headers:e.headers,url:e.url,agent:a,timeout:t.timeout,callback:(e,t,o)=>{if(e)return s(e);t.data="string"==typeof o?JSON.parse(o):o,t.responseCode=!0,r(t)}},o))})}return this.instance(Object.assign(Object.assign({url:e},t),{adapter:r,proxy:!1,httpsAgent:a}))}}exports.default=Request;