Function idGenerator

  • Marks the method of a workflow class that makes the journey ID of each of its instances, in place of a random UUID. It is called with the workflow object as this, while the instance is created, and returns the ID synchronously, as a string that is not empty: an empty one makes instance() throw a TypeError. Its one parameter, when it has one, requests the data bag with @dataBag(), holding the instance's initial data, from which it may derive the ID.

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

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

    @workflow({ name: "orders" })
    @withStep(Ship, { name: "ship" })
    class Orders extends Workflow {
    constructor(readonly shop: string) {
    super();
    }

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

    const orders = Orders.build("corner");

    assert.equal(orders.instance({ number: 7 }).journeyId, "corner-7");
    assert.equal(orders.instance().journeyId, "corner-unnumbered");

    Returns IdGeneratorDecorator

    Error, when the class is defined, if a parameter has no request or one an ID generator may not make, if the class has another ID generator, or if the compiler option emitDecoratorMetadata is off.