UNPKG

youzanyun-devtool-worker

Version:

68 lines (67 loc) 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const axios_1 = tslib_1.__importDefault(require("axios")); const lodash_1 = require("lodash"); const spring4js_nodejs_1 = require("spring4js-nodejs"); let ActionBypassService = class ActionBypassService { async start() { this.proxy = this.configService.isUseProxy(); this.host = this.configService.getProxyHost(); this.port = this.configService.getProxyPort(); } async processRequest(ctx, next) { let hasBody = false; switch ((0, lodash_1.toLower)(ctx.method)) { case 'post': case 'patch': case 'put': hasBody = true; break; default: hasBody = false; } let axiosRes; let config = { method: ctx.method, baseURL: ctx.origin, url: ctx.url.includes('/wa/wecom-app/api') ? `${ctx.url}&_yzy_preview_type_=h5-preview-editor` : ctx.url, headers: Object.assign(ctx.headers, {}), data: hasBody ? (ctx.isJsonHeader ? ctx.request.body : ctx.req) : null, responseType: 'stream', validateStatus: function (status) { return true; }, maxRedirects: 0, }; if (this.proxy) { config.proxy = { host: this.host, port: this.port, protocol: 'http:', }; } try { axiosRes = await axios_1.default.request(config); ctx.status = axiosRes.status; let header = JSON.parse(JSON.stringify(axiosRes.headers)); delete header['content-length']; ctx.set(header); ctx.body = axiosRes.data; } catch (err) { console.log('err', err); ctx.body = ''; } return true; } }; tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], ActionBypassService.prototype, "configService", void 0); ActionBypassService = tslib_1.__decorate([ (0, spring4js_nodejs_1.Service)() ], ActionBypassService); exports.default = ActionBypassService;