fix: use safeFetchNoThrow in BrevilabsClient to bypass CORS errors (#2213)

Replace native fetch() with safeFetchNoThrow() in makeRequest() so API
calls route through Obsidian's requestUrl (Electron/Node layer) instead
of Chromium's network stack, eliminating intermittent CORS failures.

makeFormDataRequest retains native fetch() because safeFetch hardcodes
Content-Type: application/json and calls body.toString(), which breaks
FormData payloads.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Logan Yang 2026-02-23 20:34:43 -08:00 committed by GitHub
parent 9b9bf151c1
commit f086ad76dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,6 +4,7 @@ import { MissingPlusLicenseError } from "@/error";
import { logInfo } from "@/logger";
import { turnOffPlus, turnOnPlus } from "@/plusUtils";
import { getSettings } from "@/settings/model";
import { safeFetchNoThrow } from "@/utils";
import { arrayBufferToBase64 } from "@/utils/base64";
export interface RerankResponse {
@ -115,15 +116,16 @@ export class BrevilabsClient {
url.searchParams.append(key, value as string);
});
}
const response = await fetch(url.toString(), {
const headers: Record<string, string> = {
"Content-Type": "application/json",
"X-Client-Version": this.pluginVersion,
};
if (!excludeAuthHeader) {
headers.Authorization = `Bearer ${await getDecryptedKey(getSettings().plusLicenseKey)}`;
}
const response = await safeFetchNoThrow(url.toString(), {
method,
headers: {
"Content-Type": "application/json",
...(!excludeAuthHeader && {
Authorization: `Bearer ${await getDecryptedKey(getSettings().plusLicenseKey)}`,
}),
"X-Client-Version": this.pluginVersion,
},
headers,
...(method === "POST" && { body: JSON.stringify(body) }),
});
const data = await response.json();