Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] api result isn't show devTools'NetWork #7882

Closed
myGitTemp617 opened this issue Sep 21, 2023 · 4 comments
Closed

[bug] api result isn't show devTools'NetWork #7882

myGitTemp617 opened this issue Sep 21, 2023 · 4 comments
Labels
status: needs triage This issue needs to triage, applied to new issues type: bug

Comments

@myGitTemp617
Copy link

myGitTemp617 commented Sep 21, 2023

Describe the bug

Api result isn't show devTools'NetWork .

import { Body, HttpVerb } from "@tauri-apps/api/http";
import { IResponse, TauriData } from "./types";
import { fetch } from '@tauri-apps/api/http';
import { useGetToken, useGetUploadToken, useSetToken } from "../../golbalFn/cookieFn";
import { useShowStatus4Action, useShowStatus5Action } from "../../golbalFn/requestFn";
import { useAppStore } from "@/store";
import { ElMessage } from "element-plus";

export class TauriFetch {
    baseURL: string
    tauriData: TauriData

    constructor(baseURL: string, tauriData: TauriData) {
        this.baseURL = baseURL;
        this.tauriData = tauriData
    }

    static paramsToQueryString(params?: Record<string, string>): Record<string, string> | undefined {
        if (!params) return undefined
        const paramsObj = params
        Object.keys(params).forEach((element: string) => {
            if (typeof (params[element]) != 'string') {
                paramsObj[element] = JSON.stringify(params[element])
            }
        })
        return paramsObj
    }
    static dataToBodyJson(data?: Record<string, string>): Body | undefined {

        // eslint-disable-next-line no-extra-boolean-cast
        return !!data ? Body.json({ ...data }) : undefined
    }
    static methodToHttpVerb(method: string): HttpVerb {

        switch (method.toLocaleUpperCase().trim()) {
            case 'GET':
                return "GET";
            case 'POST':
                return "POST";
            case 'PUT':
                return "PUT";
            case 'DELETE':
                return "DELETE";
            case 'PATCH':
                return "PATCH";
            case 'HEAD':
                return "HEAD";
            case 'OPTIONS':
                return "OPTIONS";
            case 'CONNECT':
                return "CONNECT";
            case 'TRACE':
                return "TRACE";
            default:
                return "GET";
        }

    }
    static headersAuthorization(url: string) {
        const token = useGetToken() || "";
        const uploadToken = useGetUploadToken() || "";
        if (!!uploadToken && url === "/file/upload") {
            return `Bearer ${uploadToken}`;
        }
        if (!!token && url !== "/account/login") {
            return `Bearer ${token}`;
        }
        return ""
    }
    //response
    response(responseData: IResponse<any>): IResponse<any> {
        console.log(responseData.status)
        if (/[/]account[/]login/i.test(responseData.url)) {
            // eslint-disable-next-line no-extra-boolean-cast
            if (!!useAppStore().isTokenTime) {
                useSetToken(responseData.data.token, 7);
            } else { useSetToken(responseData.data.token); }
        }
        const httpSatus = Math.floor(responseData.status / 100)
        switch (httpSatus) {
            case 1:
                return responseData;
            case 2:
                return responseData
            case 3:
                return responseData
            case 4:
                useShowStatus4Action(responseData.status, responseData.data)
                return responseData
            case 5:
                useShowStatus5Action(responseData.status)
                return responseData
            default:
                ElMessage({
                    message: `${responseData.status} !`,
                    type: "error",
                    duration: 5 * 1000,
                })

                return responseData;
        }

    }
 // request
    async request() {
        const responseData: IResponse<any> = await fetch(`${this.baseURL}${this.tauriData.url}`, {
            headers: {
                Authorization: TauriFetch.headersAuthorization(this.tauriData.url)
            },
            method: TauriFetch.methodToHttpVerb(this.tauriData.method),
           
            body: TauriFetch.dataToBodyJson(this.tauriData.data),
            query: TauriFetch.paramsToQueryString(this.tauriData.params)
        });

        console.log(`apiUrl:${this.baseURL}${this.tauriData.url}`, {
            "Authorization": TauriFetch.headersAuthorization(this.tauriData.url),
            "body": TauriFetch.dataToBodyJson(this.tauriData.data),
            "query": TauriFetch.paramsToQueryString(this.tauriData.params),
            "apiResult": responseData
        })
        return this.response(responseData)

    }

}

Reproduction

No response

Expected behavior

No response

Platform and versions

Environment
  › OS: Windows 10.0.22621 X64
  › Webview2: 117.0.2045.31
  › MSVC: 
      - Visual Studio ���ɹ��� 2022
  › Node.js: 16.15.1
  › npm: 8.11.0
  › pnpm: Not installed!
  › yarn: Not installed!
  › rustup: 1.26.0
  › rustc: 1.69.0
  › cargo: 1.69.0
  › Rust toolchain: 1.69-x86_64-pc-windows-msvc 

Packages
  › @tauri-apps/cli [NPM]: 1.2.1 (outdated, latest: 1.4.0)
  › @tauri-apps/api [NPM]: 1.2.0 (outdated, latest: 1.4.0)
  › tauri [RUST]: 1.4.1,
  › tauri-build [RUST]: 1.4.0,
  › tao [RUST]: 0.16.2,
  › wry [RUST]: 0.24.3,

App
  › build-type: bundle
  › CSP: unset
  › distDir: ../dist
  › devPath: http://localhost:8080/
  › framework: Vue.js

App directory structure
  ├─ .git
  ├─ .tauri
  ├─ .vscode
  ├─ node_modules
  ├─ public
  ├─ script
  ├─ src
  ├─ src-tauri
  └─ types

Stack trace

No response

Additional context

No response

@myGitTemp617 myGitTemp617 added status: needs triage This issue needs to triage, applied to new issues type: bug labels Sep 21, 2023
@FabianLars
Copy link
Member

This is expected behavior. Tauri's api methods don't actually run in the browser but on the Rust side and afk there's no way to inject custom stuff into the devtools network tab since the communication with the rust side doesn't involve any network/http apis.

In v2 this may change a bit since the new API does use the browser's fetch to call the rust side so it should show at least something in the network tab.

@FabianLars FabianLars closed this as not planned Won't fix, can't repro, duplicate, stale Sep 21, 2023
@myGitTemp617
Copy link
Author

how to use axios to request api?

@FabianLars
Copy link
Member

i don't know, i never used axios. But it should work in tauri the same as shown in their docs as long as you don't have to request http urls but only https (http urls will throw mixed content errors, see #3007)

@myGitTemp617
Copy link
Author

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage This issue needs to triage, applied to new issues type: bug
Projects
None yet
Development

No branches or pull requests

2 participants