UNPKG

youzanyun-devtool-worker

Version:

- web - ws - proxy

86 lines (85 loc) 3.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const axios_1 = tslib_1.__importDefault(require("axios")); const spring4js_nodejs_1 = require("spring4js-nodejs"); let RequestService = class RequestService { constructor() { this._loginFailedEvent = new spring4js_nodejs_1.Emitter(); this.onLoginFailedEvent = this._loginFailedEvent.registerEvent; } async start() { let useProxy = this.configService.isUseProxy(); const sid = await this.userService.getSid(); let config = { headers: { 'cookie': sid ? `sid=${sid};` : '' }, transformResponse: [(data) => { try { const result = JSON.parse(data || '{}'); if (result.msg === '登录失效') { this._loginFailedEvent.fire({}); } return result; } catch (err) { return data; } }], }; if (useProxy) { let host = this.configService.getProxyHost(); let port = this.configService.getProxyPort(); config.proxy = { host, port, }; } this.axiosInstance = axios_1.default.create(config); } async setRequestCookie(sid) { this.axiosInstance.defaults.headers['cookie'] = sid ? `sid=${sid};` : ''; return true; } async request(config) { let res = await this.axiosInstance.request(config); return res.data; } async post(url, data, config) { let res = await this.axiosInstance.post(url, data, config); return res.data; } async get(url, config) { let res = await this.axiosInstance.get(url, config); return res.data; } async del(url, config) { let res = await this.axiosInstance.delete(url, config); return res.data; } async dubboCall(serviceName, methodName, args) { let res = await this.axiosInstance.post(`https://diy.youzanyun.com/api/custom/devtool/request?api=${serviceName}.${methodName}`, { serviceName, methodName, args }); return res.data; } async dubboCallWithAuth(serviceName, methodName, args) { let res = await this.axiosInstance.post(`https://diy.youzanyun.com/api/custom/devtool/dubborequest?api=${serviceName}.${methodName}`, { serviceName, methodName, args }); return res.data; } }; tslib_1.__decorate([ spring4js_nodejs_1.Resource() ], RequestService.prototype, "configService", void 0); tslib_1.__decorate([ spring4js_nodejs_1.Resource() ], RequestService.prototype, "userService", void 0); RequestService = tslib_1.__decorate([ spring4js_nodejs_1.Service() ], RequestService); exports.default = RequestService;