王一之 8ae6fcc8f9
Some checks failed
test / Run tests (push) Failing after 15s
build / Build (push) Failing after 22s
0201
2025-02-01 14:39:57 +08:00

32 lines
807 B
TypeScript

import { Message, MessageConnect } from "./server";
export async function sendMessage(msg: Message, action: string, data?: any): Promise<any> {
const res = await msg.sendMessage({ action, data });
console.log(action, data, res);
if (res && res.code) {
console.error(res);
return Promise.reject(res.message);
} else {
return Promise.resolve(res.data);
}
}
export function connect(msg: Message, action: string, data?: any): Promise<MessageConnect> {
return msg.connect({ action, data });
}
export class Client {
constructor(
private msg: Message,
private prefix: string
) {
if (!this.prefix.endsWith("/")) {
this.prefix += "/";
}
}
do(action: string, params?: any): Promise<any> {
return sendMessage(this.msg, this.prefix + action, params);
}
}