fix(http): include browser headers if not set by user (#1354)

* fix(http): include browser automatic headers if not set by user

close #1349

* fmt

* Update browser-headers.md

* lint

* generate api
This commit is contained in:
Amr Bashir
2024-05-21 12:43:26 +02:00
committed by GitHub
parent 200f61c99d
commit 9d7ae45b0e
4 changed files with 40 additions and 20 deletions
+29 -14
View File
@@ -117,30 +117,45 @@ export async function fetch(
const headers = init?.headers
? init.headers instanceof Headers
? Array.from(init.headers.entries())
: Array.isArray(init.headers)
? init.headers
: Object.entries(init.headers)
: [];
const mappedHeaders: Array<[string, string]> = headers.map(([name, val]) => [
name,
// we need to ensure we have all values as strings
// eslint-disable-next-line
typeof val === "string" ? val : (val as any).toString(),
]);
? init.headers
: new Headers(init.headers)
: new Headers();
const req = new Request(input, init);
const buffer = await req.arrayBuffer();
const reqData =
const data =
buffer.byteLength !== 0 ? Array.from(new Uint8Array(buffer)) : null;
// append new headers created by the browser `Request` implementation,
// if not already declared by the caller of this function
for (const [key, value] of req.headers) {
if (!headers.get(key)) {
headers.set(key, value);
}
}
const headersArray =
headers instanceof Headers
? Array.from(headers.entries())
: Array.isArray(headers)
? headers
: Object.entries(headers);
const mappedHeaders: Array<[string, string]> = headersArray.map(
([name, val]) => [
name,
// we need to ensure we have all header values as strings
// eslint-disable-next-line
typeof val === "string" ? val : (val as any).toString(),
],
);
const rid = await invoke<number>("plugin:http|fetch", {
clientConfig: {
method: req.method,
url: req.url,
headers: mappedHeaders,
data: reqData,
data,
maxRedirections,
connectTimeout,
proxy,