This commit is contained in:
linyuchen
2024-02-08 11:32:17 +08:00
parent 2c55d84b9f
commit 5e64df0eaa
2 changed files with 13 additions and 5 deletions

View File

@@ -38,11 +38,15 @@ export function hookNTQQApiReceive(window: BrowserWindow) {
if (args?.[1] instanceof Array) { if (args?.[1] instanceof Array) {
for (let receiveData of args?.[1]) { for (let receiveData of args?.[1]) {
const ntQQApiMethodName = receiveData.cmdName; const ntQQApiMethodName = receiveData.cmdName;
log(`received ntqq api message: ${channel} ${ntQQApiMethodName}`, JSON.stringify(receiveData)) // log(`received ntqq api message: ${channel} ${ntQQApiMethodName}`, JSON.stringify(receiveData))
for (let hook of receiveHooks) { for (let hook of receiveHooks) {
if (hook.method === ntQQApiMethodName) { if (hook.method === ntQQApiMethodName) {
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
hook.hookFunc(receiveData.payload); try {
hook.hookFunc(receiveData.payload);
}catch (e) {
log("hook error", e, receiveData.payload)
}
}).then() }).then()
} }
} }

View File

@@ -1,7 +1,7 @@
import {getConfigUtil, log} from "../common/utils"; import {getConfigUtil, log} from "../common/utils";
// const express = require("express"); // const express = require("express");
import express from "express"; import express = require("express");
import {Request} from 'express'; import {Request} from 'express';
import {Response} from 'express'; import {Response} from 'express';
@@ -180,14 +180,18 @@ export function startExpress(port: number) {
}); });
async function registerRouter<PayloadType, ReturnDataType>(action: OB11ApiName, handle: (payload: PayloadType) => Promise<OB11Return<ReturnDataType>>) { async function registerRouter<PayloadType, ReturnDataType>(action: OB11ApiName, handle: (payload: PayloadType) => Promise<OB11Return<ReturnDataType>>) {
let url = action.toString()
if (!action.startsWith("/")){
url = "/" + action
}
async function _handle(res: Response, payload: PayloadType) { async function _handle(res: Response, payload: PayloadType) {
res.send(await handle(payload)) res.send(await handle(payload))
} }
app.post('/' + action, (req: Request, res: Response) => { app.post(url, (req: Request, res: Response) => {
_handle(res, req.body).then() _handle(res, req.body).then()
}); });
app.get('/' + action, (req: Request, res: Response) => { app.get(url, (req: Request, res: Response) => {
_handle(res, req.query as any).then() _handle(res, req.query as any).then()
}); });
} }