diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 323a3cf0..26872853 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -66,6 +66,7 @@ async fn native_request(url: String, body: String, header: String, method: Strin Ok(resp) => { let headers = resp.headers(); let header_json = header_map_to_json(headers); + let status = resp.status().as_u16().to_string(); let bytes = match resp.bytes().await { Ok(b) => b, Err(e) => return format!(r#"{{"success":false,"body":"{}"}}"#, e.to_string()), @@ -73,11 +74,12 @@ async fn native_request(url: String, body: String, header: String, method: Strin let encoded = general_purpose::STANDARD.encode(&bytes); format!( - r#"{{"success":true,"body":"{}","headers":{}}}"#, - encoded, header_json + // r#"{{"success":true,"body":"{}","headers":{}}}"#, + r#"{{"success":true,"body":"{}","headers":{},"status":{}}}"#, + encoded, header_json, status ) } - Err(e) => format!(r#"{{"success":false,"body":"{}"}}"#, e.to_string()), + Err(e) => format!(r#"{{"success":false,"body":"{}","status":400}}"#, e.to_string()), } } diff --git a/src/ts/globalApi.svelte.ts b/src/ts/globalApi.svelte.ts index a0a3104c..168dafe1 100644 --- a/src/ts/globalApi.svelte.ts +++ b/src/ts/globalApi.svelte.ts @@ -756,6 +756,7 @@ interface GlobalFetchResult { ok: boolean; data: any; headers: { [key: string]: string }; + status: number; } /** @@ -806,13 +807,13 @@ export async function globalFetch(url: string, arg: GlobalFetchArgs = {}): Promi const method = arg.method ?? "POST"; db.requestmet = "normal"; - if (arg.abortSignal?.aborted) { return { ok: false, data: 'aborted', headers: {} }; } + if (arg.abortSignal?.aborted) { return { ok: false, data: 'aborted', headers: {}, status: 400 }; } const urlHost = new URL(url).hostname const forcePlainFetch = ((knownHostes.includes(urlHost) && !isTauri) || db.usePlainFetch || arg.plainFetchForce) && !arg.plainFetchDeforce 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: {}, status:400, 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.' }; } if (forcePlainFetch) { @@ -832,7 +833,7 @@ export async function globalFetch(url: string, arg: GlobalFetchArgs = {}): Promi } catch (error) { console.error(error); - return { ok: false, data: `${error}`, headers: {} }; + return { ok: false, data: `${error}`, headers: {}, status: 400 }; } } @@ -887,9 +888,9 @@ async function fetchWithPlainFetch(url: string, arg: GlobalFetchArgs): Promise= 200 && response.status < 300; addFetchLogInGlobalFetch(data, ok, url, arg); - return { ok, data, headers: Object.fromEntries(response.headers) }; + return { ok, data, headers: Object.fromEntries(response.headers), status: response.status }; } catch (error) { - return { ok: false, data: `${error}`, headers: {} }; + return { ok: false, data: `${error}`, headers: {}, status: 400 }; } } @@ -907,9 +908,9 @@ async function fetchWithUSFetch(url: string, arg: GlobalFetchArgs): Promise= 200 && response.status < 300; addFetchLogInGlobalFetch(data, ok, url, arg); - return { ok, data, headers: Object.fromEntries(response.headers) }; + return { ok, data, headers: Object.fromEntries(response.headers), status: response.status }; } catch (error) { - return { ok: false, data: `${error}`, headers: {} }; + return { ok: false, data: `${error}`, headers: {}, status: 400 }; } } @@ -927,7 +928,7 @@ async function fetchWithTauri(url: string, arg: GlobalFetchArgs): Promise= 200 && response.status < 300; addFetchLogInGlobalFetch(data, ok, url, arg); - return { ok, data, headers: Object.fromEntries(response.headers) }; + return { ok, data, headers: Object.fromEntries(response.headers), status: response.status }; } catch (error) { } @@ -946,6 +947,7 @@ async function fetchWithCapacitor(url: string, arg: GlobalFetchArgs): Promise