bun-types 1.3.4-canary.20251126T140720 → 1.3.4-canary.20251127T140647
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/guides/http/proxy.mdx +27 -3
- package/docs/runtime/networking/fetch.mdx +17 -1
- package/globals.d.ts +32 -2
- package/package.json +1 -1
|
@@ -9,18 +9,42 @@ In Bun, `fetch` supports sending requests through an HTTP or HTTPS proxy. This i
|
|
|
9
9
|
```ts proxy.ts icon="/icons/typescript.svg"
|
|
10
10
|
await fetch("https://example.com", {
|
|
11
11
|
// The URL of the proxy server
|
|
12
|
-
proxy: "https://
|
|
12
|
+
proxy: "https://username:password@proxy.example.com:8080",
|
|
13
13
|
});
|
|
14
14
|
```
|
|
15
15
|
|
|
16
16
|
---
|
|
17
17
|
|
|
18
|
-
The `proxy` option
|
|
18
|
+
The `proxy` option can be a URL string or an object with `url` and optional `headers`. The URL can include the username and password if the proxy requires authentication. It can be `http://` or `https://`.
|
|
19
19
|
|
|
20
20
|
---
|
|
21
21
|
|
|
22
|
+
## Custom proxy headers
|
|
23
|
+
|
|
24
|
+
To send custom headers to the proxy server (useful for proxy authentication tokens, custom routing, etc.), use the object format:
|
|
25
|
+
|
|
26
|
+
```ts proxy-headers.ts icon="/icons/typescript.svg"
|
|
27
|
+
await fetch("https://example.com", {
|
|
28
|
+
proxy: {
|
|
29
|
+
url: "https://proxy.example.com:8080",
|
|
30
|
+
headers: {
|
|
31
|
+
"Proxy-Authorization": "Bearer my-token",
|
|
32
|
+
"X-Proxy-Region": "us-east-1",
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The `headers` property accepts a plain object or a `Headers` instance. These headers are sent directly to the proxy server in `CONNECT` requests (for HTTPS targets) or in the proxy request (for HTTP targets).
|
|
39
|
+
|
|
40
|
+
If you provide a `Proxy-Authorization` header, it will override any credentials specified in the proxy URL.
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Environment variables
|
|
45
|
+
|
|
22
46
|
You can also set the `$HTTP_PROXY` or `$HTTPS_PROXY` environment variable to the proxy URL. This is useful when you want to use the same proxy for all requests.
|
|
23
47
|
|
|
24
48
|
```sh terminal icon="terminal"
|
|
25
|
-
HTTPS_PROXY=https://
|
|
49
|
+
HTTPS_PROXY=https://username:password@proxy.example.com:8080 bun run index.ts
|
|
26
50
|
```
|
|
@@ -51,7 +51,7 @@ const response = await fetch("http://example.com", {
|
|
|
51
51
|
|
|
52
52
|
### Proxying requests
|
|
53
53
|
|
|
54
|
-
To proxy a request, pass an object with the `proxy` property set to a URL
|
|
54
|
+
To proxy a request, pass an object with the `proxy` property set to a URL string:
|
|
55
55
|
|
|
56
56
|
```ts
|
|
57
57
|
const response = await fetch("http://example.com", {
|
|
@@ -59,6 +59,22 @@ const response = await fetch("http://example.com", {
|
|
|
59
59
|
});
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
+
You can also use an object format to send custom headers to the proxy server:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
const response = await fetch("http://example.com", {
|
|
66
|
+
proxy: {
|
|
67
|
+
url: "http://proxy.com",
|
|
68
|
+
headers: {
|
|
69
|
+
"Proxy-Authorization": "Bearer my-token",
|
|
70
|
+
"X-Custom-Proxy-Header": "value",
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
The `headers` are sent directly to the proxy in `CONNECT` requests (for HTTPS targets) or in the proxy request (for HTTP targets). If you provide a `Proxy-Authorization` header, it overrides any credentials in the proxy URL.
|
|
77
|
+
|
|
62
78
|
### Custom headers
|
|
63
79
|
|
|
64
80
|
To set custom headers, pass an object with the `headers` property set to an object.
|
package/globals.d.ts
CHANGED
|
@@ -1920,14 +1920,44 @@ interface BunFetchRequestInit extends RequestInit {
|
|
|
1920
1920
|
* Override http_proxy or HTTPS_PROXY
|
|
1921
1921
|
* This is a custom property that is not part of the Fetch API specification.
|
|
1922
1922
|
*
|
|
1923
|
+
* Can be a string URL or an object with `url` and optional `headers`.
|
|
1924
|
+
*
|
|
1923
1925
|
* @example
|
|
1924
1926
|
* ```js
|
|
1927
|
+
* // String format
|
|
1925
1928
|
* const response = await fetch("http://example.com", {
|
|
1926
1929
|
* proxy: "https://username:password@127.0.0.1:8080"
|
|
1927
1930
|
* });
|
|
1931
|
+
*
|
|
1932
|
+
* // Object format with custom headers sent to the proxy
|
|
1933
|
+
* const response = await fetch("http://example.com", {
|
|
1934
|
+
* proxy: {
|
|
1935
|
+
* url: "https://127.0.0.1:8080",
|
|
1936
|
+
* headers: {
|
|
1937
|
+
* "Proxy-Authorization": "Bearer token",
|
|
1938
|
+
* "X-Custom-Proxy-Header": "value"
|
|
1939
|
+
* }
|
|
1940
|
+
* }
|
|
1941
|
+
* });
|
|
1928
1942
|
* ```
|
|
1929
|
-
|
|
1930
|
-
|
|
1943
|
+
*
|
|
1944
|
+
* If a `Proxy-Authorization` header is provided in `proxy.headers`, it takes
|
|
1945
|
+
* precedence over credentials parsed from the proxy URL.
|
|
1946
|
+
*/
|
|
1947
|
+
proxy?:
|
|
1948
|
+
| string
|
|
1949
|
+
| {
|
|
1950
|
+
/**
|
|
1951
|
+
* The proxy URL
|
|
1952
|
+
*/
|
|
1953
|
+
url: string;
|
|
1954
|
+
/**
|
|
1955
|
+
* Custom headers to send to the proxy server.
|
|
1956
|
+
* These headers are sent in the CONNECT request (for HTTPS targets)
|
|
1957
|
+
* or in the proxy request (for HTTP targets).
|
|
1958
|
+
*/
|
|
1959
|
+
headers?: Bun.HeadersInit;
|
|
1960
|
+
};
|
|
1931
1961
|
|
|
1932
1962
|
/**
|
|
1933
1963
|
* Override the default S3 options
|
package/package.json
CHANGED