Type Alias GivenReason<T>

GivenReason:
    | string
    | { code: string; details?: T & AsValue<T>; message?: string }

A reason as a step or a test gives it: its code alone, or a code with a message and details. The details may be any value, an object that serialises itself included, and are captured when the reason is given.

import assert from "node:assert/strict";
import { input, type Outcome, Step, step } from "itinera/step";

@step()
class Charge extends Step {
constructor(@input("amount") private readonly amount: number) {
super();
}

override run(): Outcome {
if (this.amount === 0) {
return this.skip();
}
if (this.amount < 0) {
return this.skipBecause("refunds are not charged");
}
if (this.amount > 100) {
return this.fail({
code: "declined",
message: "over the limit",
details: { limit: 100 },
});
}
return this.succeed();
}
}

assert.deepEqual(new Charge(40).run(), { kind: "success" });
assert.deepEqual(new Charge(0).run(), { kind: "skipped" });
assert.deepEqual(new Charge(-5).run(), {
kind: "skipped",
reason: { code: "refunds are not charged" },
});
assert.deepEqual(new Charge(140).run(), {
kind: "failure",
reason: {
code: "declined",
message: "over the limit",
details: { limit: 100 },
},
});

Type Parameters

Type Declaration

  • string
  • { code: string; details?: T & AsValue<T>; message?: string }
    • Readonlycode: string

      A short code a program can compare, such as "declined".

    • Optional Readonlydetails?: T & AsValue<T>

      Anything else the reason says, as a value.

    • Optional Readonlymessage?: string

      A message for people.