@phystack/axios-proxy 4.4.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +88 -0
- package/dist/index.js.map +1 -0
- package/package.json +30 -0
- package/src/index.ts +108 -0
- package/tsconfig.json +45 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAO7C,QAAA,MAAM,yBAAyB,QAAa,OAAO,CAAC,aAAa,CAkGhE,CAAC;AAEF,eAAe,yBAAyB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const axios_1 = __importDefault(require("axios"));
|
|
7
|
+
let axiosInstance = null;
|
|
8
|
+
const getAxiosInstanceWithProxy = async () => {
|
|
9
|
+
if (axiosInstance) {
|
|
10
|
+
return axiosInstance;
|
|
11
|
+
}
|
|
12
|
+
const isBrowser = typeof window !== 'undefined';
|
|
13
|
+
if (isBrowser) {
|
|
14
|
+
console.log('getAxiosInstanceWithProxy(): creating axios instance in browser');
|
|
15
|
+
console.log('Axios typeof:', typeof axios_1.default);
|
|
16
|
+
console.log('Axios keys:', Object.keys(axios_1.default));
|
|
17
|
+
axiosInstance = axios_1.default.create();
|
|
18
|
+
console.log('getAxiosInstanceWithProxy(): axiosInstance', axiosInstance);
|
|
19
|
+
return axiosInstance;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
let HttpsProxyAgent = null;
|
|
23
|
+
let HttpProxyAgent = null;
|
|
24
|
+
try {
|
|
25
|
+
const [{ HttpsProxyAgent: HttpsAgentClass }, { HttpProxyAgent: HttpAgentClass }] = await Promise.all([
|
|
26
|
+
import('https-proxy-agent'),
|
|
27
|
+
import('http-proxy-agent'),
|
|
28
|
+
]);
|
|
29
|
+
HttpsProxyAgent = HttpsAgentClass;
|
|
30
|
+
HttpProxyAgent = HttpAgentClass;
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
console.error('[getAxiosInstanceWithProxy] Failed to load proxy agent modules:', err);
|
|
34
|
+
axiosInstance = axios_1.default.create({
|
|
35
|
+
headers: {
|
|
36
|
+
'User-Agent': 'Phygrid Axios',
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
return axiosInstance;
|
|
40
|
+
}
|
|
41
|
+
const proxyUrl = process.env.HTTPS_PROXY ||
|
|
42
|
+
process.env.https_proxy ||
|
|
43
|
+
process.env.HTTP_PROXY ||
|
|
44
|
+
process.env.http_proxy;
|
|
45
|
+
if (!proxyUrl || !HttpsProxyAgent || !HttpProxyAgent) {
|
|
46
|
+
axiosInstance = axios_1.default.create({
|
|
47
|
+
headers: {
|
|
48
|
+
'User-Agent': 'Phygrid Axios',
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
try {
|
|
54
|
+
const httpsAgent = new HttpsProxyAgent(proxyUrl);
|
|
55
|
+
const httpAgent = new HttpProxyAgent(proxyUrl);
|
|
56
|
+
axiosInstance = axios_1.default.create({
|
|
57
|
+
proxy: false,
|
|
58
|
+
httpAgent,
|
|
59
|
+
httpsAgent,
|
|
60
|
+
headers: {
|
|
61
|
+
'User-Agent': 'Phygrid Axios',
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
axiosInstance.interceptors.response.use((response) => response, (error) => {
|
|
65
|
+
console.error('[getAxiosInstanceWithProxy] Axios Error:', {
|
|
66
|
+
url: error.config?.url,
|
|
67
|
+
status: error.response?.status,
|
|
68
|
+
message: error.message,
|
|
69
|
+
proxyUrl,
|
|
70
|
+
responseHeaders: error.response?.headers,
|
|
71
|
+
});
|
|
72
|
+
return Promise.reject(error);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
console.error('[getAxiosInstanceWithProxy] Failed to setup proxy, using direct connection:', error);
|
|
77
|
+
axiosInstance = axios_1.default.create({
|
|
78
|
+
headers: {
|
|
79
|
+
'User-Agent': 'Phygrid Axios',
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return axiosInstance;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
exports.default = getAxiosInstanceWithProxy;
|
|
88
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,kDAA6C;AAE7C,IAAI,aAAa,GAAyB,IAAI,CAAC;AAK/C,MAAM,yBAAyB,GAAG,KAAK,IAA4B,EAAE;IAEnE,IAAI,aAAa,EAAE,CAAC;QAClB,OAAO,aAAa,CAAC;IACvB,CAAC;IAGD,MAAM,SAAS,GAAG,OAAO,MAAM,KAAK,WAAW,CAAC;IAEhD,IAAI,SAAS,EAAE,CAAC;QAEd,OAAO,CAAC,GAAG,CAAC,iEAAiE,CAAC,CAAC;QAC/E,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,OAAO,eAAK,CAAC,CAAC;QAC3C,OAAO,CAAC,GAAG,CAAC,aAAa,EAAE,MAAM,CAAC,IAAI,CAAC,eAAK,CAAC,CAAC,CAAC;QAC/C,aAAa,GAAG,eAAK,CAAC,MAAM,EAAmB,CAAC;QAChD,OAAO,CAAC,GAAG,CAAC,4CAA4C,EAAE,aAAa,CAAC,CAAC;QACzE,OAAO,aAAa,CAAC;IACvB,CAAC;SAAM,CAAC;QAEN,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BACV,OAAO,EAAE;wBACP,YAAY,EAAE,eAAe;qBAC9B;iBACF,CAAkB,CAAC;gBAGpB,aAAa,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CACrC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,EACtB,CAAC,KAAK,EAAE,EAAE;oBACR,OAAO,CAAC,KAAK,CAAC,0CAA0C,EAAE;wBACxD,GAAG,EAAE,KAAK,CAAC,MAAM,EAAE,GAAG;wBACtB,MAAM,EAAE,KAAK,CAAC,QAAQ,EAAE,MAAM;wBAC9B,OAAO,EAAE,KAAK,CAAC,OAAO;wBACtB,QAAQ;wBACR,eAAe,EAAE,KAAK,CAAC,QAAQ,EAAE,OAAO;qBACzC,CAAC,CAAC;oBACH,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAC/B,CAAC,CACF,CAAC;YACJ,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,CAAC,KAAK,CACX,6EAA6E,EAC7E,KAAK,CACN,CAAC;gBACF,aAAa,GAAG,eAAK,CAAC,MAAM,CAAC;oBAC3B,OAAO,EAAE;wBACP,YAAY,EAAE,eAAe;qBAC9B;iBACF,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QAED,OAAO,aAAa,CAAC;IACvB,CAAC;AACH,CAAC,CAAC;AAEF,kBAAe,yBAAyB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@phystack/axios-proxy",
|
|
3
|
+
"version": "4.4.29",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"private": false,
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "rimraf dist && tsc",
|
|
12
|
+
"format": "prettier --write \"src/**/*.{ts,js,json,md}\"",
|
|
13
|
+
"format:check": "prettier --check \"src/**/*.{ts,js,json,md}\""
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"axios": "^1.7.9"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"prettier": "^3.4.2",
|
|
20
|
+
"rimraf": "^5.0.7",
|
|
21
|
+
"typescript": "^5.4.5"
|
|
22
|
+
},
|
|
23
|
+
"optionalDependencies": {
|
|
24
|
+
"http-proxy-agent": "^7.0.2",
|
|
25
|
+
"https-proxy-agent": "^7.0.6"
|
|
26
|
+
},
|
|
27
|
+
"test": "react-scripts test",
|
|
28
|
+
"test:coverage": "npm run test -- --coverage --watchAll=false",
|
|
29
|
+
"gitHead": "7dba834ca1f446f669992ff14352862d2fbcfe22"
|
|
30
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import axios, { AxiosInstance } from 'axios';
|
|
2
|
+
|
|
3
|
+
let axiosInstance: AxiosInstance | null = null;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns a singleton Axios instance, configured with Node proxy agents if needed.
|
|
7
|
+
*/
|
|
8
|
+
const getAxiosInstanceWithProxy = async (): Promise<AxiosInstance> => {
|
|
9
|
+
// If the instance already exists, return it
|
|
10
|
+
if (axiosInstance) {
|
|
11
|
+
return axiosInstance;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Check environment
|
|
15
|
+
const isBrowser = typeof window !== 'undefined';
|
|
16
|
+
|
|
17
|
+
if (isBrowser) {
|
|
18
|
+
// In the browser, create a normal Axios instance
|
|
19
|
+
console.log('getAxiosInstanceWithProxy(): creating axios instance in browser');
|
|
20
|
+
console.log('Axios typeof:', typeof axios);
|
|
21
|
+
console.log('Axios keys:', Object.keys(axios));
|
|
22
|
+
axiosInstance = axios.create() as AxiosInstance;
|
|
23
|
+
console.log('getAxiosInstanceWithProxy(): axiosInstance', axiosInstance);
|
|
24
|
+
return axiosInstance;
|
|
25
|
+
} else {
|
|
26
|
+
// In Node, dynamically import proxy-agent modules
|
|
27
|
+
let HttpsProxyAgent: any = null;
|
|
28
|
+
let HttpProxyAgent: any = null;
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
const [{ HttpsProxyAgent: HttpsAgentClass }, { HttpProxyAgent: HttpAgentClass }] = await Promise.all([
|
|
32
|
+
import('https-proxy-agent'),
|
|
33
|
+
import('http-proxy-agent'),
|
|
34
|
+
]);
|
|
35
|
+
HttpsProxyAgent = HttpsAgentClass;
|
|
36
|
+
HttpProxyAgent = HttpAgentClass;
|
|
37
|
+
} catch (err) {
|
|
38
|
+
console.error('[getAxiosInstanceWithProxy] Failed to load proxy agent modules:', err);
|
|
39
|
+
// Create a fallback instance (no proxy)
|
|
40
|
+
axiosInstance = axios.create({
|
|
41
|
+
headers: {
|
|
42
|
+
'User-Agent': 'Phygrid Axios',
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
return axiosInstance;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Get proxy url from env
|
|
49
|
+
const proxyUrl =
|
|
50
|
+
process.env.HTTPS_PROXY ||
|
|
51
|
+
process.env.https_proxy ||
|
|
52
|
+
process.env.HTTP_PROXY ||
|
|
53
|
+
process.env.http_proxy;
|
|
54
|
+
|
|
55
|
+
if (!proxyUrl || !HttpsProxyAgent || !HttpProxyAgent) {
|
|
56
|
+
// No proxy URL or missing modules -> normal instance
|
|
57
|
+
axiosInstance = axios.create({
|
|
58
|
+
headers: {
|
|
59
|
+
'User-Agent': 'Phygrid Axios',
|
|
60
|
+
},
|
|
61
|
+
}) as AxiosInstance;
|
|
62
|
+
} else {
|
|
63
|
+
// Configure Axios with the proxy agents
|
|
64
|
+
try {
|
|
65
|
+
const httpsAgent = new HttpsProxyAgent(proxyUrl);
|
|
66
|
+
const httpAgent = new HttpProxyAgent(proxyUrl);
|
|
67
|
+
|
|
68
|
+
axiosInstance = axios.create({
|
|
69
|
+
proxy: false, // Disable Axios's built-in proxy config
|
|
70
|
+
httpAgent,
|
|
71
|
+
httpsAgent,
|
|
72
|
+
headers: {
|
|
73
|
+
'User-Agent': 'Phygrid Axios',
|
|
74
|
+
},
|
|
75
|
+
}) as AxiosInstance;
|
|
76
|
+
|
|
77
|
+
// Optional interceptor for logging errors
|
|
78
|
+
axiosInstance.interceptors.response.use(
|
|
79
|
+
(response) => response,
|
|
80
|
+
(error) => {
|
|
81
|
+
console.error('[getAxiosInstanceWithProxy] Axios Error:', {
|
|
82
|
+
url: error.config?.url,
|
|
83
|
+
status: error.response?.status,
|
|
84
|
+
message: error.message,
|
|
85
|
+
proxyUrl,
|
|
86
|
+
responseHeaders: error.response?.headers,
|
|
87
|
+
});
|
|
88
|
+
return Promise.reject(error);
|
|
89
|
+
}
|
|
90
|
+
);
|
|
91
|
+
} catch (error) {
|
|
92
|
+
console.error(
|
|
93
|
+
'[getAxiosInstanceWithProxy] Failed to setup proxy, using direct connection:',
|
|
94
|
+
error
|
|
95
|
+
);
|
|
96
|
+
axiosInstance = axios.create({
|
|
97
|
+
headers: {
|
|
98
|
+
'User-Agent': 'Phygrid Axios',
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return axiosInstance;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export default getAxiosInstanceWithProxy;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "NodeNext",
|
|
4
|
+
"target": "es2022",
|
|
5
|
+
"outDir": "dist",
|
|
6
|
+
"rootDir": "./src",
|
|
7
|
+
"baseUrl": "src",
|
|
8
|
+
"removeComments": true,
|
|
9
|
+
"moduleResolution": "NodeNext",
|
|
10
|
+
"sourceMap": true,
|
|
11
|
+
"strict": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"alwaysStrict": true,
|
|
14
|
+
"allowJs": true,
|
|
15
|
+
"noEmitOnError": true,
|
|
16
|
+
"noFallthroughCasesInSwitch": true,
|
|
17
|
+
"noImplicitAny": true,
|
|
18
|
+
"noImplicitReturns": false,
|
|
19
|
+
"noImplicitThis": true,
|
|
20
|
+
"noUnusedLocals": true,
|
|
21
|
+
"noUnusedParameters": true,
|
|
22
|
+
"strictBindCallApply": true,
|
|
23
|
+
"strictNullChecks": true,
|
|
24
|
+
"allowSyntheticDefaultImports": true,
|
|
25
|
+
"resolveJsonModule": true,
|
|
26
|
+
"esModuleInterop": true,
|
|
27
|
+
"declarationDir": "dist",
|
|
28
|
+
"declarationMap": true,
|
|
29
|
+
"declaration": true,
|
|
30
|
+
"paths": {
|
|
31
|
+
"@/*": ["*"]
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"include": [
|
|
35
|
+
"./src/**/*"
|
|
36
|
+
],
|
|
37
|
+
"exclude": [
|
|
38
|
+
"node_modules/**/*",
|
|
39
|
+
".serverless/**/*",
|
|
40
|
+
".webpack/**/*",
|
|
41
|
+
"_warmup/**/*",
|
|
42
|
+
"dist/**/*",
|
|
43
|
+
".vscode/**/*"
|
|
44
|
+
]
|
|
45
|
+
}
|