26 lines
845 B
TypeScript
26 lines
845 B
TypeScript
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
import type { WebUiContext } from "../types/context.ts";
|
|
import { json, readBody } from "../http/request.ts";
|
|
|
|
export function handleExtensionUiRoute(
|
|
req: IncomingMessage,
|
|
res: ServerResponse,
|
|
ctx: WebUiContext,
|
|
pathname: string,
|
|
): boolean {
|
|
if (req.method !== "POST" || pathname !== "/api/extension-ui-response") {
|
|
return false;
|
|
}
|
|
|
|
void readBody(req)
|
|
.then(({ id, response }) => {
|
|
if (typeof id !== "string" || !id.trim()) throw new Error("缺少 extension UI 响应 id");
|
|
if (!response || typeof response !== "object") throw new Error("缺少 extension UI 响应内容");
|
|
ctx.rpc.sendExtensionUiResponse(id, response as Record<string, unknown>);
|
|
json(res, { ok: true });
|
|
})
|
|
.catch((err) => json(res, { error: err.message }, 500));
|
|
|
|
return true;
|
|
}
|