Cotton code
26 lines · 1.2 KB · TypeScriptRaw
/** * `run_act { act, args }`: one act through the object's own `/v1/act` * dispatch, as the caller (an owner token as the owner, any other as staff). * The answer is the act's own `{ version, changed, made?, notices, token? }`; * a refusal is the dispatch's sentence, its code beside it. */import { isAct, ACTS } from "../../acts.ts";import { Refused } from "../../http.ts";import { fail, requireString, type Data, type Handler, type ToolContext } from "../context.ts";async function runAct(ctx: ToolContext, args: Data): Promise<Data> {  const names = Object.keys(ACTS).join(", ");  const act = requireString(args, "act", `act is required: one of ${names}`);  if (!isAct(act)) fail(`there is no act ${JSON.stringify(act)}: the acts are ${names}`, { code: "unknown_act" });  const given = args.args;  if (given !== undefined && given !== null && (typeof given !== "object" || Array.isArray(given))) fail("args must be an object: the act's own arguments");  const actArgs = (given as Data | null | undefined) ?? {};  try {    return await ctx.act(act, actArgs);  } catch (error) {    if (error instanceof Refused) fail(error.message, { code: error.code });    throw error;  }}export const ACTS_HANDLERS: Record<string, Handler> = { run_act: runAct };