A first step
What you want to do
The shop charges an order its price. You want that business rule written with Itinera: the charge, and nothing else.
The code
In Itinera, a business rule is a step. In TypeScript, a step is a class extending Step, whose constructor takes the data the step needs, and whose run method does the work. The shop's bank is faked, so that it would print what it charges, once something runs the step.
/** Charges an order's price. */
@step()
class Charge extends Step {
constructor(@input("price", integer) private readonly price: number) {
super();
}
override run(): Outcome {
if (this.price <= 0) return this.fail("nothing-to-charge");
bankCharges(this.price);
return this.succeed();
}
}That is the whole step. The program declares it and does nothing else: nothing builds a Charge, and nothing calls its run, so it prints nothing.
How it works
- A step is a class extending
Step, marked@step().Stepcomes fromitinera/step, and so does the decorator@step(), which tells Itinera that the class is a step and records what it asks for. It does that once, when the class is defined; it builds noChargeand runs nothing. - A step asks for the data it needs. Each parameter of the constructor marked
@input(...)is an input:@input("price", integer)asks for a number under the nameprice. The constructor'sprivate readonly price: numberis TypeScript's shorthand for a parameter that is also a field, sorunreads it asthis.price. The step says what it needs, and nothing about where it comes from. - An input names the type of its value. The type the compiler records for the running program says only that
priceis anumber, so each input also names a type token, fromitinera/value, which Itinera checks the value against:integerfor a whole number, andstringfor a string. The parameter's type is what the compiler checks, and the token what the running program checks. - A step does its work in
run.overridesays that thisrunis the oneStepdeclares. Here it charges the bank, unless there is nothing to charge. - A step says how it went, and nothing more.
runreturns the step's outcome, of typeOutcome, made with a methodStepgives it:this.succeed()when the charge went through, orthis.fail("nothing-to-charge"), a failure with its reason, when the price is 0 or less. A step reports what happened, never what to do next. The charge doesn't know which order it is charging for, what the shop does before it or after it, or what happens to an order whose charge failed.
So the charge is written, and it can't charge anything. Nothing builds it with an order's price, nothing calls run, and nothing does anything with its outcome. A step on its own is a business rule waiting to be used. The shop still needs a way to run it for an order.
Outcomes
We have shown a business rule written as a step, which asks for its data and says how it went, and nothing else.
| The usual way | With Itinera |
|---|---|
| A charge function usually signals a failure by throwing an error of its own, or by returning a value of its own, so each caller learns to read it its own way. | Every step says how it went the same way: success, or a failure with a reason. |
| A function usually gets its data as parameters, from whichever caller calls it. | The step names each piece of data it needs as an input, in its own declaration, so what it reads is listed in one place. |