diff --git a/lib/wretch-proxy.js b/lib/wretch-proxy.js index 2c1071b..9ce5be2 100644 --- a/lib/wretch-proxy.js +++ b/lib/wretch-proxy.js @@ -1,9 +1,9 @@ /** * wretch extension for proxy header support. * - * Registers a custom `fetch` (node-fetch + ProxyHeadersAgent) as wretch's fetch polyfill. - * wretch stores polyfills on a module singleton; avoid mixing different proxy configs - * in the same process without coordinating polyfills. + * 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' }); @@ -20,7 +20,7 @@ import { createProxyFetch } from './node-fetch-proxy.js'; * @param {string} options.proxy - Proxy URL * @param {Object} [options.proxyHeaders] - Headers to send on CONNECT * @param {Function} [options.onProxyConnect] - CONNECT callback - * @returns {Promise} Default wretch export after polyfills are set + * @returns {Promise} Wretch factory wired to proxy-header fetch */ export async function createProxyWretch(options) { const { proxy, proxyHeaders = {}, onProxyConnect } = options; @@ -29,15 +29,20 @@ export async function createProxyWretch(options) { throw new Error('proxy option is required'); } - let wretch; + let rawWretch; try { - wretch = (await import('wretch')).default; + rawWretch = (await import('wretch')).default; } catch { throw new Error('wretch is required. Install it with: npm install wretch'); } - const fetch = createProxyFetch({ proxy, proxyHeaders, onProxyConnect }); - wretch.polyfills({ fetch }); + const fetchImpl = createProxyFetch({ proxy, proxyHeaders, onProxyConnect }); - return wretch; + function proxyWretch(url, opts) { + return rawWretch(url, opts).fetchPolyfill(fetchImpl); + } + proxyWretch.default = proxyWretch; + proxyWretch.WretchError = rawWretch.WretchError; + + return proxyWretch; } diff --git a/package-lock.json b/package-lock.json index bceca19..4e6377d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,7 @@ "typed-rest-client": "^2.2.0", "typescript": "^6.0.2", "undici": "^8.0.2", - "wretch": "^2.11.0" + "wretch": "^3.0.7" }, "engines": { "node": ">=18.0.0" @@ -34,13 +34,13 @@ "axios": "^1.0.0", "got": "^12.0.0 || ^13.0.0 || ^14.0.0", "ky": "^1.0.0", - "make-fetch-happen": "^15.0.5", + "make-fetch-happen": "^14.0.0", "needle": "^3.0.0", "node-fetch": "^3.0.0", "superagent": "^8.0.0 || ^9.0.0 || ^10.0.0", "typed-rest-client": "^2.0.0", - "undici": "^8.0.2", - "wretch": "^2.0.0" + "undici": "^5.0.0 || ^6.0.0 || ^7.0.0", + "wretch": "^2.0.0 || ^3.0.0" }, "peerDependenciesMeta": { "axios": { @@ -2922,13 +2922,13 @@ "dev": true }, "node_modules/wretch": { - "version": "2.11.1", - "resolved": "https://registry.npmjs.org/wretch/-/wretch-2.11.1.tgz", - "integrity": "sha512-rJejp30zE8Igs4Mvo4pxXaiOKdSIGYLxyVaY+jZTlAMfQyi/hZsDwMRa9R09N0iS9Q8cLvplKfHwwyBv6XRWsg==", + "version": "3.0.7", + "resolved": "https://registry.npmjs.org/wretch/-/wretch-3.0.7.tgz", + "integrity": "sha512-1zvhHLcwjGx5OMGHcdkuVmHWd/8NQ0gS4oV6+Er1YTnav4QTMTDl7ZI5QklELNVsX1BDNQk2kUrhoUSILomelg==", "dev": true, "license": "MIT", "engines": { - "node": ">=14" + "node": ">=22" } }, "node_modules/yallist": { diff --git a/package.json b/package.json index 572fd03..c9a9a31 100644 --- a/package.json +++ b/package.json @@ -116,7 +116,7 @@ "superagent": "^8.0.0 || ^9.0.0 || ^10.0.0", "undici": "^5.0.0 || ^6.0.0 || ^7.0.0", "ky": "^1.0.0", - "wretch": "^2.0.0", + "wretch": "^2.0.0 || ^3.0.0", "make-fetch-happen": "^14.0.0", "needle": "^3.0.0", "typed-rest-client": "^2.0.0" @@ -163,7 +163,7 @@ "typescript": "^6.0.2", "undici": "^8.0.2", "ky": "^1.7.0", - "wretch": "^2.11.0", + "wretch": "^3.0.7", "make-fetch-happen": "^15.0.5", "needle": "^3.3.0", "typed-rest-client": "^2.2.0" diff --git a/tsconfig.json b/tsconfig.json index b6545ef..3eae74c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "ignoreDeprecations": "6.0", "target": "ES2020", "module": "ESNext", "moduleResolution": "Bundler", diff --git a/types/wretch.d.ts b/types/wretch.d.ts index 37b2991..24dd96f 100644 --- a/types/wretch.d.ts +++ b/types/wretch.d.ts @@ -1,9 +1,9 @@ -import type { Wretch } from 'wretch'; - export interface CreateProxyWretchOptions { proxy: string; proxyHeaders?: Record; onProxyConnect?: (headers: Map) => void; } -export function createProxyWretch(options: CreateProxyWretchOptions): Promise; +export function createProxyWretch( + options: CreateProxyWretchOptions +): Promise;