基本实现完成GM API

This commit is contained in:
2025-04-12 02:05:10 +08:00
parent 5c0d4a2560
commit d697928fb0
20 changed files with 631 additions and 99 deletions

View File

@ -6,9 +6,9 @@ export async function sendMessage(msg: MessageSend, action: string, data?: any):
LoggerCore.getInstance().logger().trace("sendMessage", { action, data, response: res });
if (res && res.code) {
console.error(res);
return Promise.reject(res.message);
throw res.message;
} else {
return Promise.resolve(res.data);
return res.data;
}
}

View File

@ -138,20 +138,7 @@ export function forwardMessage(
to: MessageSend,
middleware?: ApiFunctionSync
) {
from.on(path, (params, fromCon) => {
if (middleware) {
// 此处是为了处理CustomEventMessage的同步消息情况
const resp = middleware(params, fromCon) as any;
if (resp instanceof Promise) {
return resp.then((data) => {
if (data !== false) {
return data;
}
});
} else if (resp !== false) {
return resp;
}
}
const handler = (params: any, fromCon: GetSender) => {
const fromConnect = fromCon.getConnect();
if (fromConnect) {
connect(to, prefix + "/" + path, params).then((toCon) => {
@ -171,5 +158,22 @@ export function forwardMessage(
} else {
return sendMessage(to, prefix + "/" + path, params);
}
};
from.on(path, (params, sender) => {
if (middleware) {
// 此处是为了处理CustomEventMessage的同步消息情况
const resp = middleware(params, sender) as any;
if (resp instanceof Promise) {
return resp.then((data) => {
if (data !== false) {
return data;
}
return handler(params, sender);
});
} else if (resp !== false) {
return resp;
}
return handler(params, sender);
}
});
}