Refactor globalFetch function to simplify and improve readability

This commit is contained in:
kwaroran
2024-03-25 18:58:05 +09:00
parent 7707e55c87
commit 6ac23c6054

View File

@@ -551,8 +551,9 @@ export async function globalFetch(url: string, arg: GlobalFetchArgs = {}): Promi
const urlHost = new URL(url).hostname const urlHost = new URL(url).hostname
const forcePlainFetch = (knownHostes.includes(urlHost) && !isTauri) || db.usePlainFetch || arg.plainFetchForce const forcePlainFetch = (knownHostes.includes(urlHost) && !isTauri) || db.usePlainFetch || arg.plainFetchForce
if (knownHostes.includes(urlHost) && !isTauri && !isNodeServer) if (knownHostes.includes(urlHost) && !isTauri && !isNodeServer){
return { ok: false, headers: {}, data: 'You are trying local request on web version. This is not allowed due to browser security policy. Use the desktop version instead, or use a tunneling service like ngrok and set the CORS to allow all.' } return { ok: false, headers: {}, data: 'You are trying local request on web version. This is not allowed due to browser security policy. Use the desktop version instead, or use a tunneling service like ngrok and set the CORS to allow all.' }
}
// Simplify the globalFetch function: Detach built-in functions // Simplify the globalFetch function: Detach built-in functions
if (forcePlainFetch) { if (forcePlainFetch) {
@@ -602,7 +603,7 @@ function addFetchLogInGlobalFetch(response:any, success:boolean, url:string, arg
async function fetchWithPlainFetch(url: string, arg: GlobalFetchArgs): Promise<GlobalFetchResult> { async function fetchWithPlainFetch(url: string, arg: GlobalFetchArgs): Promise<GlobalFetchResult> {
try { try {
const headers = { 'Content-Type': 'application/json', ...arg.headers }; const headers = { 'Content-Type': 'application/json', ...arg.headers };
const response = await fetch(new URL(url), { body: JSON.stringify(arg.body), headers, method: arg.method, signal: arg.abortSignal }); const response = await fetch(new URL(url), { body: JSON.stringify(arg.body), headers, method: arg.method ?? "POST", signal: arg.abortSignal });
const data = arg.rawResponse ? new Uint8Array(await response.arrayBuffer()) : await response.json(); const data = arg.rawResponse ? new Uint8Array(await response.arrayBuffer()) : await response.json();
const ok = response.ok && response.status >= 200 && response.status < 300; const ok = response.ok && response.status >= 200 && response.status < 300;
addFetchLogInGlobalFetch(data, ok, url, arg); addFetchLogInGlobalFetch(data, ok, url, arg);
@@ -618,7 +619,7 @@ async function fetchWithTauri(url: string, arg: GlobalFetchArgs): Promise<Global
const headers = arg.headers ?? {}; const headers = arg.headers ?? {};
const fetchPromise = TauriFetch(url, { const fetchPromise = TauriFetch(url, {
body, body,
method: arg.method, method: arg.method ?? 'POST',
headers, headers,
timeout: { secs: get(DataBase).timeOut, nanos: 0 }, timeout: { secs: get(DataBase).timeOut, nanos: 0 },
responseType: arg.rawResponse ? ResponseType.Binary : ResponseType.JSON, responseType: arg.rawResponse ? ResponseType.Binary : ResponseType.JSON,
@@ -647,7 +648,7 @@ async function fetchWithCapacitor(url: string, arg: GlobalFetchArgs): Promise<Gl
const { body, headers = {}, rawResponse } = arg; const { body, headers = {}, rawResponse } = arg;
headers["Content-Type"] = body instanceof URLSearchParams ? "application/x-www-form-urlencoded" : "application/json"; headers["Content-Type"] = body instanceof URLSearchParams ? "application/x-www-form-urlencoded" : "application/json";
const res = await CapacitorHttp.request({ url, method: arg.method, headers, data: body, responseType: rawResponse ? "arraybuffer" : "json" }); const res = await CapacitorHttp.request({ url, method: arg.method ?? "POST", headers, data: body, responseType: rawResponse ? "arraybuffer" : "json" });
addFetchLogInGlobalFetch(rawResponse ? "Uint8Array Response" : res.data, true, url, arg); addFetchLogInGlobalFetch(rawResponse ? "Uint8Array Response" : res.data, true, url, arg);