Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions lib/wretch-proxy.js
Original file line number Diff line number Diff line change
@@ -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' });
Expand All @@ -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<import('wretch').Wretch>} Default wretch export after polyfills are set
* @returns {Promise<typeof import('wretch').default>} Wretch factory wired to proxy-header fetch
*/
export async function createProxyWretch(options) {
const { proxy, proxyHeaders = {}, onProxyConnect } = options;
Expand All @@ -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;
}
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"ignoreDeprecations": "6.0",
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "Bundler",
Expand Down
6 changes: 3 additions & 3 deletions types/wretch.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Wretch } from 'wretch';

export interface CreateProxyWretchOptions {
proxy: string;
proxyHeaders?: Record<string, string>;
onProxyConnect?: (headers: Map<string, string>) => void;
}

export function createProxyWretch(options: CreateProxyWretchOptions): Promise<Wretch>;
export function createProxyWretch(
options: CreateProxyWretchOptions
): Promise<typeof import('wretch').default>;
Loading