11/**
22 * wretch extension for proxy header support.
33 *
4- * Registers a custom `fetch` (node-fetch + ProxyHeadersAgent) as wretch's fetch polyfill.
5- * wretch stores polyfills on a module singleton; avoid mixing different proxy configs
6- * in the same process without coordinating polyfills.
4+ * Wraps the wretch factory so each request chain uses a custom `fetch`
5+ * (node-fetch + ProxyHeadersAgent). wretch v3 uses per-chain `.fetchPolyfill()`
6+ * instead of the old module-level `. polyfills()` .
77 *
88 * @example
99 * const wretch = await createProxyWretch({ proxy: 'http://proxy:8080' });
@@ -20,7 +20,7 @@ import { createProxyFetch } from './node-fetch-proxy.js';
2020 * @param {string } options.proxy - Proxy URL
2121 * @param {Object } [options.proxyHeaders] - Headers to send on CONNECT
2222 * @param {Function } [options.onProxyConnect] - CONNECT callback
23- * @returns {Promise<import('wretch').Wretch > } Default wretch export after polyfills are set
23+ * @returns {Promise<typeof import('wretch').default > } Wretch factory wired to proxy-header fetch
2424 */
2525export async function createProxyWretch ( options ) {
2626 const { proxy, proxyHeaders = { } , onProxyConnect } = options ;
@@ -29,15 +29,20 @@ export async function createProxyWretch(options) {
2929 throw new Error ( 'proxy option is required' ) ;
3030 }
3131
32- let wretch ;
32+ let rawWretch ;
3333 try {
34- wretch = ( await import ( 'wretch' ) ) . default ;
34+ rawWretch = ( await import ( 'wretch' ) ) . default ;
3535 } catch {
3636 throw new Error ( 'wretch is required. Install it with: npm install wretch' ) ;
3737 }
3838
39- const fetch = createProxyFetch ( { proxy, proxyHeaders, onProxyConnect } ) ;
40- wretch . polyfills ( { fetch } ) ;
39+ const fetchImpl = createProxyFetch ( { proxy, proxyHeaders, onProxyConnect } ) ;
4140
42- return wretch ;
41+ function proxyWretch ( url , opts ) {
42+ return rawWretch ( url , opts ) . fetchPolyfill ( fetchImpl ) ;
43+ }
44+ proxyWretch . default = proxyWretch ;
45+ proxyWretch . WretchError = rawWretch . WretchError ;
46+
47+ return proxyWretch ;
4348}
0 commit comments