feat(http): expose proxy configuration (#824)

* feat(http): add proxy config

* chore: build iife api

* chore: allow `too_many_arguments`

* improvement

* refactor: restructure code

* improvement

* format
This commit is contained in:
阿良仔
2023-12-20 16:14:12 +02:00
committed by GitHub
parent c2115d8d21
commit bfa87da848
4 changed files with 165 additions and 12 deletions
+54 -6
View File
@@ -26,6 +26,45 @@
import { invoke } from "@tauri-apps/api/core";
/**
* Configuration of a proxy that a Client should pass requests to.
*
* @since 2.0.0
*/
export type Proxy = {
/**
* Proxy all traffic to the passed URL.
*/
all?: string | ProxyConfig;
/**
* Proxy all HTTP traffic to the passed URL.
*/
http?: string | ProxyConfig;
/**
* Proxy all HTTPS traffic to the passed URL.
*/
https?: string | ProxyConfig;
};
export interface ProxyConfig {
/**
* The URL of the proxy server.
*/
url: string;
/**
* Set the `Proxy-Authorization` header using Basic auth.
*/
basicAuth?: {
username: string;
password: string;
};
/**
* A configuration for filtering out requests that shouldnt be proxied.
* Entries are expected to be comma-separated (whitespace between entries is ignored)
*/
noProxy?: string;
}
/**
* Options to configure the Rust client used to make fetch requests
*
@@ -39,6 +78,10 @@ export interface ClientOptions {
maxRedirections?: number;
/** Timeout in milliseconds */
connectTimeout?: number;
/**
* Configuration of a proxy that a Client should pass requests to.
*/
proxy?: Proxy;
}
/**
@@ -61,11 +104,13 @@ export async function fetch(
): Promise<Response> {
const maxRedirections = init?.maxRedirections;
const connectTimeout = init?.maxRedirections;
const proxy = init?.proxy;
// Remove these fields before creating the request
if (init) {
delete init.maxRedirections;
delete init.connectTimeout;
delete init.proxy;
}
const req = new Request(input, init);
@@ -73,12 +118,15 @@ export async function fetch(
const reqData = buffer.byteLength ? Array.from(new Uint8Array(buffer)) : null;
const rid = await invoke<number>("plugin:http|fetch", {
method: req.method,
url: req.url,
headers: Array.from(req.headers.entries()),
data: reqData,
maxRedirections,
connectTimeout,
clientConfig: {
method: req.method,
url: req.url,
headers: Array.from(req.headers.entries()),
data: reqData,
maxRedirections,
connectTimeout,
proxy,
},
});
req.signal.addEventListener("abort", () => {