Class StepAbstract

The base class of every step: one unit of work in a workflow, built anew for each attempt, which reports how the attempt went by the outcome its run returns, made with the helpers this class gives it.

A step reports something abnormal by throwing, or by returning a rejected promise: that is an abnormal termination of the attempt, which its workflow may retry. Only a class extending Step is a step, whether it is declared with step or given to a workflow's builder.

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

@step()
class Ship extends Step {
override run(): Outcome {
return this.succeed();
}
}

assert.deepEqual(new Ship().run(), { kind: "success" });

Hierarchy (View Summary)

Index

  • The outcome of an attempt that could not do its work, for a reason, or only its code. The reason's details are captured now, so changing them afterwards changes nothing; details that are not a value abort the journey with not a value once the attempt ends.

    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

    • T

    Parameters

    Returns Fail

  • Runs one attempt of the step, and reports how it went. It may be synchronous or asynchronous: itinera waits for the outcome either way.

    Parameters

    • ...requests: never[]

    Returns Outcome | Promise<Outcome>

  • The outcome of a step that decided not to do its work: it does not run again in the journey, and what it contributed is discarded.

    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 },
    },
    });

    Returns Skip

  • The outcome of a step that decided not to do its work, saying why, with a reason or only its code, captured as Step.fail captures it.

    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

    • T

    Parameters

    Returns Skip

  • Starts putting a step of this class together for a workflow's builder, named name, as Charge.builder("charge").

    import assert from "node:assert/strict";
    import type { Event } from "itinera/event";
    import { type DataBag, dataBag } from "itinera/journey";
    import type { Reporter } from "itinera/report";
    import { type Outcome, Step } from "itinera/step";
    import { Workflow } from "itinera/workflow";
    import { integer } from "itinera/value";

    class Charge extends Step {
    override run(): Outcome {
    return this.succeed();
    }
    }

    class Ship extends Step {
    override run(): Outcome {
    return this.succeed();
    }
    }

    class Audit implements Reporter {
    readonly kinds: string[] = [];

    report(event: Event): void {
    this.kinds.push(event.kind);
    }
    }

    class Orders extends Workflow {
    constructor(readonly shop: string) {
    super();
    }

    orderId(data: DataBag): string {
    const number = data.read("number", integer).option();
    return `${this.shop}-${number.map(String).unwrapOr("unnumbered")}`;
    }
    }

    const orders = Orders.builder()
    .name("orders")
    .step(Charge.builder("charge").retryBudget(1).noRetryOnAbnormalTermination())
    .step(Ship.builder("ship"))
    .reporter(Audit)
    .idGenerator("orderId", dataBag())
    .constructWith("corner")
    .build();

    const instance = orders.instance({ number: 7 });
    assert.equal(instance.descriptor.name, "orders");
    assert.equal(instance.journeyId, "corner-7");
    assert.deepEqual(
    instance.descriptor.steps().map((described) => described.name),
    ["charge", "ship"],
    );
    assert.equal(instance.descriptor.steps()[0]?.retryBudget, 1);
    assert.equal(instance.descriptor.steps()[0]?.retryOnAbnormalTermination, false);

    A step keeps no static data, refused as WithoutStaticData says: at compile time, or else when the builder starts.

    Type Parameters

    Parameters

    Returns StepBuilder<C>

    Error if the class keeps static data, or if the name is empty.