Usecases
Named operations that own the rules and return a Result.
package @smitejs/domain
A usecase is a named operation that turns an input into a Result. It is the single place a business rule lives in the request path: it validates the input, checks the specifications that govern the operation, calls the injected ports, and hands back a success value or a reason — never a thrown error.
domain.usecase({ name, input, deps, handle }) builds one:
inputis a zod schema; invalid input short-circuits to adomain.validationfailure instead of reaching your logic.depslists the ports the operation needs; a missing dependency fails fast withdomain.depsrather than exploding later.handle(deps, input)is the functional core. It returns aResultand does all I/O through the injected ports.
usecase.run(input, deps) returns a TaskResult (a lazy, composable Result). usecase.with(deps) closes over the dependencies so you can hand a pure (input) => TaskResult to a transport.
The rule is one way in, one way out: validate, check, act, respond. Everything a usecase does is visible in its body, which is exactly what makes it auditable.
const greet = usecase({
name: "greet",
input: z.object({ name: z.string() }),
handle: (_deps, input) => Result.ok(`hello ${input.name}`),
});
const greeting = await greet.run({ name: "smite" }, {}).run();"hello smite"const notes = { save: async () => Result.ok(undefined) };
const saveNote = usecase({
name: "saveNote",
deps: ["notes"],
handle: (deps, input) => deps.notes.save(input),
});
const runner = saveNote.with({ notes });
const result = await runner({ text: "hi" }).run();true