Skip to content

Commit d50a204

Browse files
authored
docs: add proxy configuration for Bun and Deno (#539)
1 parent d61b2fc commit d50a204

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

README.md

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -306,63 +306,53 @@ while (true) {
306306
}
307307
```
308308

309-
## 🕵️ Adding HTTP(S) Agent
309+
## 🕵️ Proxy Support
310310

311-
In Node.js (>= 18) environments, you can provide a custom dispatcher to intercept requests and support features such as Proxy and self-signed certificates. This feature is enabled by [undici](https://undici.nodejs.org/) built-in Node.js. [read more](https://undici.nodejs.org/#/docs/api/Dispatcher) about the Dispatcher API.
312-
313-
Some available agents:
311+
> [!IMPORTANT]
312+
> **Environment Variables:** Bun and Deno respect `HTTP_PROXY` and `HTTPS_PROXY` environment variables. Node.js requires setting `NODE_USE_ENV_PROXY=1` to enable [built-in proxy support](https://nodejs.org/api/http.html#http_built_in_proxy_support).
314313
315-
- `ProxyAgent`: A Proxy Agent class that implements the Agent API. It allows the connection through a proxy in a simple way. ([docs](https://undici.nodejs.org/#/docs/api/ProxyAgent))
316-
- `MockAgent`: A mocked Agent class that implements the Agent API. It allows one to intercept HTTP requests made through undici and return mocked responses instead. ([docs](https://undici.nodejs.org/#/docs/api/MockAgent))
317-
- `Agent`: Agent allows dispatching requests against multiple different origins. ([docs](https://undici.nodejs.org/#/docs/api/Agent))
314+
### Node.js
318315

319-
**Example:** Set a proxy agent for one request:
316+
In Node.js (>= 18), you can use the `dispatcher` option with [undici](https://undici.nodejs.org/)'s `ProxyAgent`.
320317

321318
```ts
322319
import { ProxyAgent } from "undici";
323-
import { ofetch } from "ofetch";
324-
325-
const proxyAgent = new ProxyAgent("http://localhost:3128");
326-
const data = await ofetch("https://icanhazip.com", { dispatcher: proxyAgent });
327-
```
328-
329-
**Example:** Create a custom fetch instance that has proxy enabled:
330-
331-
```ts
332-
import { ProxyAgent, setGlobalDispatcher } from "undici";
333-
import { ofetch } from "ofetch";
334320

335321
const proxyAgent = new ProxyAgent("http://localhost:3128");
336-
const fetchWithProxy = ofetch.create({ dispatcher: proxyAgent });
337322

338-
const data = await fetchWithProxy("https://icanhazip.com");
323+
await ofetch("https://icanhazip.com", { dispatcher: proxyAgent });
339324
```
340325

341-
**Example:** Set a proxy agent for all requests:
326+
**Example:** Set proxy globally for all requests:
342327

343328
```ts
344329
import { ProxyAgent, setGlobalDispatcher } from "undici";
345-
import { ofetch } from "ofetch";
346-
347-
const proxyAgent = new ProxyAgent("http://localhost:3128");
348-
setGlobalDispatcher(proxyAgent);
349330

350-
const data = await ofetch("https://icanhazip.com");
331+
setGlobalDispatcher(new ProxyAgent("http://localhost:3128"));
351332
```
352333

353334
**Example:** Allow self-signed certificates (USE AT YOUR OWN RISK!)
354335

355336
```ts
356337
import { Agent } from "undici";
357-
import { ofetch } from "ofetch";
358338

359339
// Note: This makes fetch unsecure against MITM attacks. USE AT YOUR OWN RISK!
360340
const unsecureAgent = new Agent({ connect: { rejectUnauthorized: false } });
361-
const unsecureFetch = ofetch.create({ dispatcher: unsecureAgent });
341+
await ofetch("https://self-signed.example.com/", { dispatcher: unsecureAgent });
342+
```
362343

363-
const data = await unsecureFetch("https://www.squid-cache.org/");
344+
### Bun and Deno
345+
346+
**Bun** supports the `proxy` option:
347+
348+
```ts
349+
await ofetch("https://icanhazip.com", {
350+
proxy: "http://localhost:3128",
351+
});
364352
```
365353

354+
**Deno** can also use undici with npm specifiers for programmatic configuration.
355+
366356
### 💪 Augment `FetchOptions` interface
367357

368358
You can augment the `FetchOptions` interface to add custom properties.

examples/proxy.mjs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
import { Agent } from "undici";
21
import { ofetch } from "ofetch";
32

4-
// Note: This makes fetch unsecure to MITM attacks. USE AT YOUR OWN RISK!
5-
const unsecureAgent = new Agent({ connect: { rejectUnauthorized: false } });
6-
const unsecureFetch = ofetch.create({ dispatcher: unsecureAgent });
7-
const data = await unsecureFetch("https://www.squid-cache.org/");
3+
// Node.js: Use undici ProxyAgent with dispatcher option
4+
// import { ProxyAgent } from "undici";
5+
// const proxyAgent = new ProxyAgent("http://localhost:3128");
6+
7+
// Bun: Use native proxy option
8+
// const proxy = "http://localhost:3128";
9+
10+
// Deno: Set HTTP_PROXY environment variable or use undici with npm specifier
11+
// HTTP_PROXY=http://localhost:3128 deno run --allow-net proxy.mjs
12+
13+
const data = await ofetch("https://icanhazip.com", {
14+
// dispatcher: proxyAgent, // Node.js
15+
// proxy, // Bun
16+
});
817

918
console.log(data);

0 commit comments

Comments
 (0)