MCPcopy Index your code
hub / github.com/CopyTranslator/CopyTranslator / getProxyAxios

Function getProxyAxios

src/common/translate/proxy.ts:6–89  ·  view source on GitHub ↗
(info?: boolean, googleMirror?: string)

Source from the content-addressed store, hash-verified

4const defaultGoogleAPI = "https://translate.googleapis.com";
5
6export const getProxyAxios = (info?: boolean, googleMirror?: string) => {
7 if (info) {
8 const axiosOptions: AxiosRequestConfig = {
9 timeout: 5000,
10 adapter: (config: AxiosRequestConfig) => {
11 delete config.adapter;
12 if ((config.url as string).startsWith(defaultGoogleAPI)) {
13 if (googleMirror != undefined && config.url != undefined) {
14 config.url = config.url.replace(defaultGoogleAPI, googleMirror);
15 config.method = "post";
16 return axios_(config).then((res) => {
17 if (typeof res.data !== "string") {
18 return res; //没有遇到阻碍
19 } else {
20 throw "API MIRROR FAIL";
21 }
22 });
23 }
24 console.log(config.url);
25
26 //仅在google翻译中使用网页代理
27 return new Promise(function (resolve, reject) {
28 // console.log(`[GoogleTranslate] Request start: ${config.url}`);
29
30 const request = net.request({
31 url: config.url as string,
32 method: config.method,
33 });
34
35 request.on("response", (response) => {
36 // const startTime = Date.now();
37 // console.log(`[GoogleTranslate] Response received. Status: ${response.statusCode}`);
38
39 let chunks: Buffer[] = [];
40
41 response.on("data", (chunk) => {
42 chunks.push(chunk);
43 });
44
45 response.on("end", () => {
46 // console.log(`[GoogleTranslate] Request done. Content download time: ${Date.now() - startTime}ms`);
47 const text = Buffer.concat(chunks as Buffer[]).toString();
48 try {
49 resolve({
50 data: JSON.parse(text),
51 status: response.statusCode, // 接口的http 状态
52 statusText: response.statusMessage,
53 headers: response.headers,
54 config,
55 });
56 } catch (e) {
57 console.log(config.url);
58 console.log("google translate input=", text);
59 reject(e);
60 }
61 });
62
63 response.on("error", (error:Error) => {

Callers 3

getGoogleTranslatorMethod · 0.90
proxy.tsFile · 0.85

Calls 3

onMethod · 0.80
pushMethod · 0.80
createMethod · 0.80

Tested by

no test coverage detected