UNPKG

youzanyun-devtool-worker

Version:

128 lines (127 loc) 4.7 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"); const cookie2Str_1 = tslib_1.__importDefault(require("../../utils/cookie2Str")); let RequestService = class RequestService { constructor() { this._loginFailedEvent = new spring4js_nodejs_1.Emitter(); this.onLoginFailedEvent = this._loginFailedEvent.registerEvent; } async start() { let useProxy = this.configService.isUseProxy(); let config = { timeout: 3000, headers: { 'x-custom-devtool': true, }, 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) { return true; } async handleHeader(options) { let header = { 'x-custom-devtool': true, }; const diycookie = { app_env: 'dev', }; const sid = await this.userService.getSid(); diycookie['sid'] = sid; const { projectName, projectId } = options; if (!projectName && projectId) { let container = global.container; const projectService = await container.getServiceInstance('projectService'); const { proName } = (await projectService.getProjectById(options.projectId)); diycookie['project_name'] = proName; } else if (projectName) { diycookie['project_name'] = projectName; } header.cookie = (0, cookie2Str_1.default)(diycookie); return header; } async request(config, options = {}) { const headers = await this.handleHeader(options); config.headers = headers; let res = await this.axiosInstance.request(config); return res.data; } async post(url, data, config, options) { let res = await this.request(Object.assign({ method: 'POST', url, data }, config), options); return res; } async get(url, config, options) { let res = await this.request(Object.assign({ method: 'GET', url }, config), options); return res; } async del(url, config, options) { let res = await this.request(Object.assign({ method: 'DELETE', url }, config), options); return res; } 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, options = {}) { return this.dubboCallWithAuthV2(serviceName, methodName, args, options); } async dubboCallWithAuthV2(serviceName, methodName, args, options = {}) { if (!options.projectId && !options.projectName) { throw new Error('参数错误:未传入应用信息'); } const headers = await this.handleHeader(options); delete options.projectId; delete options.projectName; let res = await this.axiosInstance.post(`https://diy.youzanyun.com/api/custom/devtool/dubborequest?api=${serviceName}.${methodName}`, { serviceName, methodName, args, ops: options, }, { headers }); return res.data; } }; tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], RequestService.prototype, "configService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], RequestService.prototype, "userService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], RequestService.prototype, "projectService", void 0); RequestService = tslib_1.__decorate([ (0, spring4js_nodejs_1.Service)() ], RequestService); exports.default = RequestService;