Commands are the core calling convention between frontend and backend: the
frontend invokes a command name, the handler registered in the backend
executes and returns the result. Ztron's protocol aligns with Tauri v2 desktop
(JSON + callback/error id + Channel).
Declare with defineCommand in src/commands.ts (recognized by
ztron codegen, which auto-generates typed frontend bindings):
Register inside the AppBuilder setup callback in src/main.ts. Use
commandDef for typed commands, or register inline when no types are needed:
The frontend simply invokes; the generic parameter is the return type:
ztron codegen scans defineCommand declarations in the source and generates
the ztron-commands.ts typed bindings under src/. The frontend can then
call in a fully typed way:
The source of truth for types lives on the TS side (the opposite of Tauri's
Rust source of truth); codegen keeps frontend and backend types from drifting
(CODEGEN_OK verified in the hello example).
Commands are not "register and anyone may call": every command belongs to a
plugin or core, and whether the frontend may call it is decided jointly by the
permission string in a capability (e.g. fs:allow-read-file) and the scope —
unauthorized calls are rejected in the backend (verification anchor
ACL_DENY_OK). See the Security Model.
适用版本:ztron 0.3.1