@@ -14,23 +14,30 @@ This library solves both problems for popular JavaScript HTTP libraries.
1414
1515## Supported Libraries
1616
17- | Library | Module | Use Case |
18- | ---------| --------| ----------|
19- | [ axios] ( https://axios-http.com/ ) | ` axios-proxy ` | Most popular HTTP client |
20- | [ node-fetch] ( https://github.com/node-fetch/node-fetch ) | ` node-fetch-proxy ` | Fetch API for Node.js |
21- | [ got] ( https://github.com/sindresorhus/got ) | ` got-proxy ` | Human-friendly HTTP client |
22- | [ undici] ( https://undici.nodejs.org/ ) | ` undici-proxy ` | Fast HTTP client (Node.js core) |
23- | [ superagent] ( https://github.com/ladjs/superagent ) | ` superagent-proxy ` | Flexible HTTP client |
17+ | Library | Subpath export | Notes |
18+ | ---------| ----------------| --------|
19+ | [ axios] ( https://axios-http.com/ ) | ` javascript-proxy-headers/axios ` | Widely used client |
20+ | [ node-fetch] ( https://github.com/node-fetch/node-fetch ) | ` javascript-proxy-headers/node-fetch ` | Fetch API on Node |
21+ | [ got] ( https://github.com/sindresorhus/got ) | ` javascript-proxy-headers/got ` | Ergonomic API |
22+ | [ undici] ( https://undici.nodejs.org/ ) | ` javascript-proxy-headers/undici ` | Node’s fast HTTP stack |
23+ | [ superagent] ( https://github.com/ladjs/superagent ) | ` javascript-proxy-headers/superagent ` | Chaining API |
24+ | [ ky] ( https://github.com/sindresorhus/ky ) | ` javascript-proxy-headers/ky ` | Tiny fetch wrapper |
25+ | [ wretch] ( https://github.com/elbywan/wretch ) | ` javascript-proxy-headers/wretch ` | Fetch wrapper (sets wretch’s global fetch polyfill) |
26+ | [ make-fetch-happen] ( https://github.com/npm/make-fetch-happen ) | ` javascript-proxy-headers/make-fetch-happen ` | npm-style fetch (cache, retries, proxy) |
27+ | [ needle] ( https://github.com/tomas/needle ) | ` javascript-proxy-headers/needle ` | Lean HTTP client |
28+ | [ typed-rest-client] ( https://github.com/microsoft/typed-rest-client ) | ` javascript-proxy-headers/typed-rest-client ` | Azure / DevOps–style REST client |
29+
30+ ** urllib** is not integrated yet: it expects an [ undici] ( https://undici.nodejs.org/ ) ` Dispatcher ` , not a Node ` Agent ` . See ` notes/urllib-integration-deferred.md ` in this repo for a possible approach.
2431
2532## Installation
2633
2734``` bash
2835npm install javascript-proxy-headers
2936```
3037
31- Then install the HTTP library you want to use (e.g. , ` npm install axios ` ) .
38+ Then install the HTTP client(s) you use (for example ` axios ` , ` got ` , ` ky ` , ` wretch ` , ` make-fetch-happen ` , ` needle ` , or ` typed-rest-client ` ). Each is an optional peer dependency .
3239
33- > ** Note:** This package has no dependencies by default - install only what you need.
40+ > ** Note:** This package has no runtime dependencies by default— install only the adapters you need.
3441
3542## Quick Start
3643
@@ -94,6 +101,85 @@ const { statusCode, headers, body, proxyHeaders } = await request(
94101console .log (proxyHeaders .get (' x-proxymesh-ip' ));
95102```
96103
104+ ### ky
105+
106+ Uses a custom ` fetch ` built from node-fetch + ` ProxyHeadersAgent ` (` ky.create({ fetch }) ` ).
107+
108+ ``` javascript
109+ import { createProxyKy } from ' javascript-proxy-headers/ky' ;
110+
111+ const api = await createProxyKy ({
112+ proxy: ' http://user:pass@proxy.example.com:8080' ,
113+ proxyHeaders: { ' X-ProxyMesh-Country' : ' US' }
114+ });
115+
116+ const response = await api (' https://httpbin.org/ip' );
117+ console .log (response .proxyHeaders .get (' x-proxymesh-ip' ));
118+ ```
119+
120+ ### wretch
121+
122+ Registers the same custom ` fetch ` as wretch’s fetch polyfill. Use the normal wretch chain (for example ` .get().res() ` ).
123+
124+ ``` javascript
125+ import { createProxyWretch } from ' javascript-proxy-headers/wretch' ;
126+
127+ const wretch = await createProxyWretch ({
128+ proxy: ' http://user:pass@proxy.example.com:8080' ,
129+ proxyHeaders: { ' X-ProxyMesh-Country' : ' US' }
130+ });
131+
132+ const response = await wretch (' https://httpbin.org/ip' ).get ().res ();
133+ console .log (response .proxyHeaders .get (' x-proxymesh-ip' ));
134+ ```
135+
136+ ### make-fetch-happen
137+
138+ Passes a ` ProxyHeadersAgent ` as ` agent ` ; ` @npmcli/agent ` uses it as-is when set.
139+
140+ ``` javascript
141+ import { createProxyMakeFetchHappen } from ' javascript-proxy-headers/make-fetch-happen' ;
142+
143+ const fetch = createProxyMakeFetchHappen ({
144+ proxy: ' http://user:pass@proxy.example.com:8080' ,
145+ proxyHeaders: { ' X-ProxyMesh-Country' : ' US' }
146+ });
147+
148+ const response = await fetch (' https://httpbin.org/ip' );
149+ console .log (response .proxyHeaders .get (' x-proxymesh-ip' ));
150+ ```
151+
152+ ### needle
153+
154+ ``` javascript
155+ import { proxyNeedleGet } from ' javascript-proxy-headers/needle' ;
156+
157+ const res = await proxyNeedleGet (' https://httpbin.org/ip' , {
158+ proxy: ' http://user:pass@proxy.example.com:8080' ,
159+ proxyHeaders: { ' X-ProxyMesh-Country' : ' US' }
160+ });
161+
162+ // CONNECT response headers merged onto res.headers where missing
163+ console .log (res .headers [' x-proxymesh-ip' ]);
164+ ```
165+
166+ ### typed-rest-client
167+
168+ Uses a subclass of ` HttpClient ` that routes HTTPS through ` ProxyHeadersAgent ` (no ` tunnel ` agent).
169+
170+ ``` javascript
171+ import { createProxyRestClient } from ' javascript-proxy-headers/typed-rest-client' ;
172+
173+ const client = createProxyRestClient ({
174+ userAgent: ' my-app' ,
175+ proxy: ' http://user:pass@proxy.example.com:8080' ,
176+ proxyHeaders: { ' X-ProxyMesh-Country' : ' US' }
177+ });
178+
179+ await client .get (' https://httpbin.org/ip' );
180+ console .log (client .proxyAgent .lastProxyHeaders ? .get (' x-proxymesh-ip' ));
181+ ` ` `
182+
97183### Core Agent (Advanced)
98184
99185For direct control, use the core ` ProxyHeadersAgent` :
@@ -116,19 +202,22 @@ https.get('https://httpbin.org/ip', { agent }, (res) => {
116202
117203## Testing
118204
119- A test harness is included to verify proxy header functionality :
205+ Integration tests need a real proxy (set ` PROXY_URL ` or ` HTTPS_PROXY ` ) :
120206
121207` ` ` bash
122- # Set your proxy
123208export PROXY_URL = ' http://user:pass@proxy.example.com:8080'
124209
125- # Test all modules
126- npm test
210+ npm test # all adapters (see package .json " test" )
211+ node run_tests .js - v # same harness from repo root
212+ npm run test: ts # same checks via tsx + TypeScript harness
213+ npm run test: types # ` tsc --noEmit` only (no network)
127214
128- # Test specific module
129- npm test axios
215+ # Limit modules
216+ node test/ test_proxy_headers . js - v axios ky
130217` ` `
131218
219+ Verbose (` - v` ) prints captured header values. See ` test/ test_proxy_headers .js -- help` .
220+
132221## Requirements
133222
134223- Node.js >= 18.0.0
0 commit comments