mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-30 17:48:50 +02:00
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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user