2022-05-26 15:18:53 +08:00
|
|
|
|
import {Constants} from "../constants";
|
|
|
|
|
|
/// #if !BROWSER
|
|
|
|
|
|
import {ipcRenderer} from "electron";
|
|
|
|
|
|
/// #endif
|
|
|
|
|
|
import {processMessage} from "./processMessage";
|
|
|
|
|
|
import {kernelError} from "../dialog/processSystem";
|
|
|
|
|
|
|
|
|
|
|
|
export const fetchPost = (url: string, data?: any, cb?: (response: IWebSocketData) => void, headers?: IObject) => {
|
|
|
|
|
|
const init: RequestInit = {
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
};
|
|
|
|
|
|
if (data) {
|
|
|
|
|
|
if (["/api/search/searchRefBlock", "/api/graph/getGraph", "/api/graph/getLocalGraph"].includes(url)) {
|
|
|
|
|
|
window.siyuan.reqIds[url] = new Date().getTime();
|
2022-11-03 22:47:19 +08:00
|
|
|
|
if (data.type === "local" && url === "/api/graph/getLocalGraph") {
|
|
|
|
|
|
// 当打开文档A的关系图、关系图、文档A后刷新,由于防止请求重复处理,文档A关系图无法渲染。
|
|
|
|
|
|
} else {
|
|
|
|
|
|
data.reqId = window.siyuan.reqIds[url];
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (data instanceof FormData) {
|
|
|
|
|
|
init.body = data;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
init.body = JSON.stringify(data);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (headers) {
|
|
|
|
|
|
init.headers = headers;
|
|
|
|
|
|
}
|
|
|
|
|
|
fetch(url, init).then((response) => {
|
2023-05-08 21:00:41 +08:00
|
|
|
|
if (response.status === 404) {
|
2023-05-16 00:04:25 +08:00
|
|
|
|
return {
|
2023-05-08 21:00:41 +08:00
|
|
|
|
data: null,
|
|
|
|
|
|
msg: response.statusText,
|
|
|
|
|
|
code: response.status,
|
2023-05-16 00:04:25 +08:00
|
|
|
|
};
|
2023-05-08 21:00:41 +08:00
|
|
|
|
} else {
|
2023-05-23 10:03:56 +08:00
|
|
|
|
if (response.headers.get("content-type")?.indexOf("application/json") > -1) {
|
2023-05-08 21:00:41 +08:00
|
|
|
|
return response.json();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return response.text();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}).then((response: IWebSocketData) => {
|
2023-05-15 22:26:11 +08:00
|
|
|
|
if (typeof response === "string") {
|
|
|
|
|
|
if (cb) {
|
|
|
|
|
|
cb(response);
|
|
|
|
|
|
}
|
2023-05-08 21:00:41 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-05-16 22:18:33 +08:00
|
|
|
|
if (typeof response === "object" && typeof response.msg === "string" && typeof response.code === "number") {
|
|
|
|
|
|
if (processMessage(response) && cb) {
|
|
|
|
|
|
cb(response);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (cb) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
cb(response);
|
|
|
|
|
|
}
|
|
|
|
|
|
}).catch((e) => {
|
|
|
|
|
|
console.warn("fetch post error", e);
|
2022-09-17 00:10:32 +08:00
|
|
|
|
if (url === "/api/transactions" && (e.message === "Failed to fetch" || e.message === "Unexpected end of JSON input")) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
kernelError();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
/// #if !BROWSER
|
|
|
|
|
|
if (url === "/api/system/exit" || url === "/api/system/setWorkspaceDir" || (
|
2023-03-17 23:25:01 +08:00
|
|
|
|
["/api/system/setUILayout"].includes(url) && data.errorExit // 内核中断,点关闭处理
|
2022-05-26 15:18:53 +08:00
|
|
|
|
)) {
|
2023-03-19 11:57:22 +08:00
|
|
|
|
ipcRenderer.send(Constants.SIYUAN_QUIT, location.port);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
/// #endif
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const fetchSyncPost = async (url: string, data?: any) => {
|
|
|
|
|
|
const init: RequestInit = {
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
};
|
|
|
|
|
|
if (data) {
|
|
|
|
|
|
init.body = JSON.stringify(data);
|
|
|
|
|
|
}
|
|
|
|
|
|
const res = await fetch(url, init);
|
|
|
|
|
|
const res2 = await res.json() as IWebSocketData;
|
|
|
|
|
|
processMessage(res2);
|
|
|
|
|
|
return res2;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const fetchGet = (url: string, cb: (response: IWebSocketData | IEmoji[]) => void) => {
|
|
|
|
|
|
fetch(url).then((response) => {
|
|
|
|
|
|
return response.json();
|
|
|
|
|
|
}).then((response: IWebSocketData) => {
|
|
|
|
|
|
cb(response);
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|