-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwretch-proxy.js
More file actions
48 lines (41 loc) · 1.59 KB
/
Copy pathwretch-proxy.js
File metadata and controls
48 lines (41 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* wretch extension for proxy header support.
*
* Wraps the wretch factory so each request chain uses a custom `fetch`
* (node-fetch + ProxyHeadersAgent). wretch v3 uses per-chain `.fetchPolyfill()`
* instead of the old module-level `.polyfills()`.
*
* @example
* const wretch = await createProxyWretch({ proxy: 'http://proxy:8080' });
* const res = await wretch('https://example.com').get().res();
* console.log(res.proxyHeaders.get('x-proxymesh-ip'));
*/
import { createProxyFetch } from './node-fetch-proxy.js';
/**
* Configure wretch to use proxy-header fetch and return the wretch factory.
*
* @param {Object} options - Configuration
* @param {string} options.proxy - Proxy URL
* @param {Object} [options.proxyHeaders] - Headers to send on CONNECT
* @param {Function} [options.onProxyConnect] - CONNECT callback
* @returns {Promise<typeof import('wretch').default>} Wretch factory wired to proxy-header fetch
*/
export async function createProxyWretch(options) {
const { proxy, proxyHeaders = {}, onProxyConnect } = options;
if (!proxy) {
throw new Error('proxy option is required');
}
let rawWretch;
try {
rawWretch = (await import('wretch')).default;
} catch {
throw new Error('wretch is required. Install it with: npm install wretch');
}
const fetchImpl = createProxyFetch({ proxy, proxyHeaders, onProxyConnect });
function proxyWretch(url, opts) {
return rawWretch(url, opts).fetchPolyfill(fetchImpl);
}
proxyWretch.default = proxyWretch;
proxyWretch.WretchError = rawWretch.WretchError;
return proxyWretch;
}