Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

6.8 KB
Raw Blame
import {fetchWithAppId} from "./fetchWithAppId";
import {Constants} from "../constants";
/// #if !BROWSER
import {ipcRenderer} from "electron";
/// #endif
import {processMessage} from "./processMessage";
import {kernelError} from "./kernelFault";
import {withFetchTimeout} from "./fetchTimeout";
import type {FetchGet, FetchPost, FetchSyncPost} from "../types/api";
import {trackSettingRequest} from "../config/setting/pending";
import {trackEditorSaveRequest} from "../protyle/util/editorSave";
import {showMessage} from "../dialog/message";

const reportTransactionFailure = (url: string, error: Error) => {
    if (url !== "/api/transactions" || error?.name === "AbortError") {
        return;
    }
    // 保留当前编辑内容并提示核对保存结果,避免重发已在内核执行的事务。
    showMessage(window.siyuan.languages.transactionSaveUnconfirmed, 0, "error", "transaction-save-unconfirmed");
    void kernelError();
};

export const fetchPost = ((
    url: string,
    data?: any,
    cb?: (response: IWebSocketData) => void,
    headers?: Record<string, string>,
    failCallback?: (response: IWebSocketData) => void,
    signal?: AbortSignal,
    timeout = url === "/api/system/exit" || url === "/api/system/setWorkspaceDir" ||
        (url === "/api/system/setUILayout" && data?.errorExit) ? 30000 : 0) => {
    const init: RequestInit = {
        method: "POST",
    };
    if (data) {
        if (["/api/search/searchRefBlock", "/api/graph/getGraph", "/api/graph/getLocalGraph"].includes(url)) {
            window.siyuan.reqIds[url] = Date.now();
            if (data.type === "local" && url === "/api/graph/getLocalGraph") {
                // 当打开文档A的关系图、关系图、文档A后刷新,由于防止请求重复处理,文档A关系图无法渲染。
            } else {
                data.reqId = window.siyuan.reqIds[url];
            }
        }
        // 并发导出后端接受顺序不一致
        if (url === "/api/transactions") {
            data.reqId = Date.now();
        }
        if (data instanceof FormData) {
            init.body = data;
        } else {
            init.body = JSON.stringify(data);
        }
    }
    if (headers) {
        init.headers = headers;
    }
    if (signal) {
        init.signal = signal;
    }
    let isGetFile202 = false;
    return trackEditorSaveRequest(url, data, trackSettingRequest(url, withFetchTimeout((requestSignal) => fetchWithAppId(url, {...init, signal: requestSignal}).then((response): Promise<IWebSocketData | string> | IWebSocketData => {
        switch (response.status) {
            case 403:
            case 404:
                return {
                    data: null,
                    msg: response.statusText,
                    code: -response.status,
                };
            case 401:
                // 返回鉴权失败的话直接刷新页面,避免用户在当前页面操作 https://github.com/siyuan-note/siyuan/issues/15163
                setTimeout(() => {
                    window.location.reload();
                }, 3000);
                return {
                    data: null,
                    msg: response.statusText,
                    code: -response.status,
                };
            default:
                // /api/file/getFile 接口返回202时表示文件没有正常读取
                if (response.status === 202 && url === "/api/file/getFile") {
                    isGetFile202 = true;
                }
                if (response.headers.get("content-type")?.indexOf("application/json") > -1) {
                    return response.json();
                } else {
                    return response.text();
                }
        }
    }), signal, timeout), url === "/api/storage/setLocalStorageVal" && data?.key === Constants.LOCAL_SLASH_USAGE)).catch(error => {
        reportTransactionFailure(url, error);
        throw error;
    }).then((response: IWebSocketData) => {
        if (failCallback && url === "/api/file/getFile" && isGetFile202) {
            failCallback(response);
            return;
        }
        if (typeof response === "string") {
            if (cb) {
                cb(response);
            }
            return;
        }
        if (["/api/search/searchRefBlock", "/api/graph/getGraph", "/api/graph/getLocalGraph"].includes(url)) {
            if (response.data.reqId && window.siyuan.reqIds[url] && window.siyuan.reqIds[url] > response.data.reqId) {
                return;
            }
        }
        if (typeof response === "object" && typeof response.msg === "string" && typeof response.code === "number") {
            if (processMessage(response) && cb) {
                cb(response);
            }
        } else if (cb) {
            cb(response);
        }
    }).catch((e) => {
        if (e?.name === "AbortError") {
            return;
        }
        if (failCallback) {
            failCallback({
                data: null,
                msg: e.message,
                code: 400,
            });
            return;
        }
        console.warn("fetch post failed [" + e + "], url [" + url + "]");
        /// #if !BROWSER
        if (url === "/api/system/setWorkspaceDir" || (
            ["/api/system/setUILayout"].includes(url) && data.errorExit // 内核中断,点关闭处理
        )) {
            ipcRenderer.send(Constants.SIYUAN_QUIT, location.port);
        }
        /// #endif
    });
}) as FetchPost<IWebSocketData>;

export const fetchSyncPost = (async (url: string, data?: any, headers?: Record<string, string>, process = true,
                                    signal?: AbortSignal) => {
    const init: RequestInit = {
        method: "POST",
        signal,
    };
    if (headers) {
        init.headers = headers;
    }
    if (data) {
        if (data instanceof FormData) {
            init.body = data;
        } else {
            init.body = JSON.stringify(data);
        }
    }
    return trackSettingRequest(url, trackEditorSaveRequest(url, data,
        fetchWithAppId(url, init).then(res => res.json() as Promise<IWebSocketData>)).catch(error => {
        reportTransactionFailure(url, error);
        throw error;
    }).then(res2 => {
        if (process) {
            processMessage(res2);
        }
        return res2;
    }), url === "/api/storage/setLocalStorageVal" && data?.key === Constants.LOCAL_SLASH_USAGE);
}) as FetchSyncPost<IWebSocketData>;

export const fetchGet = ((url: string, cb: (response: IWebSocketData | IObject | string) => void) => {
    fetchWithAppId(url, {cache: "no-store"}).then((response) => {
        if (response.headers.get("content-type")?.indexOf("application/json") > -1) {
            return response.json();
        } else {
            return response.text();
        }
    }).then((response) => {
        cb(response);
    });
}) as FetchGet<IWebSocketData | IObject | string>;