Function inputAdapter

  • Marks a method of a workflow class as an input adapter: asked for each input of the steps it is attached to while each is built, in the order the step declares them, with the workflow object as this. It returns this.supply(value), or this.fromDataBag() to read the input from the data bag, and fails by throwing.

    Its parameters request what it needs: @stepName(), @key(), @fromWorkflow(key, type), @journeyId() and @dataBag().

    import assert from "node:assert/strict";
    import { LocalExecutor } from "itinera/executor";
    import {
    type Contributor,
    contributor,
    type DataBag,
    dataBag,
    type JourneyId,
    journeyId,
    } from "itinera/journey";
    import {
    input,
    type Outcome,
    type StepName,
    stepName,
    Step,
    step,
    } from "itinera/step";
    import {
    type FromDataBag,
    fromWorkflow,
    inputAdapter,
    type InputAdapterOptions,
    key,
    type Supplied,
    type SuppliedValue,
    withStep,
    Workflow,
    workflow,
    } from "itinera/workflow";
    import { integer, string } from "itinera/value";

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

    override run(@contributor() contributor: Contributor): Outcome {
    contributor.contribute("charged", this.amount);
    return this.succeed();
    }
    }

    const adapting: InputAdapterOptions = { steps: ["charge"], name: "prices" };

    @workflow({ name: "orders" })
    @withStep(Charge, { name: "charge" })
    class Orders extends Workflow {
    readonly asked: string[] = [];

    constructor(readonly prices: ReadonlyMap<string, number>) {
    super();
    }

    @inputAdapter(adapting)
    price(
    @stepName() step: StepName,
    @key() key: string,
    @fromWorkflow("order", string) order: string,
    @journeyId() id: JourneyId,
    @dataBag() bag: DataBag,
    ): Supplied {
    this.asked.push(`${id}: ${step} ${key}`);
    const price = this.prices.get(order);
    return price === undefined || bag.read(key, integer).kind === "present"
    ? this.fromDataBag()
    : this.supply(price);
    }
    }

    export default (async () => {
    const orders = Orders.build(new Map([["A-1", 40]]));
    const supplying = orders.instance(new Map([["order", "A-1"]]));
    const reading = orders.instance(
    new Map<string, string | number>([
    ["order", "A-1"],
    ["amount", 25],
    ]),
    );

    const supplied = await new LocalExecutor().run(supplying);
    const read = await new LocalExecutor().run(reading);

    assert.ok(supplied.isSucceeded() && read.isSucceeded());
    assert.equal(charged(supplied.status.dataBag), 40);
    assert.equal(charged(read.status.dataBag), 25);
    assert.deepEqual(orders.asked, [
    `${supplying.journeyId}: charge amount`,
    `${reading.journeyId}: charge amount`,
    ]);
    const charge = "charge" as StepName;
    assert.deepEqual(
    orders.price(
    charge,
    "amount",
    "A-1",
    supplying.journeyId,
    supplied.status.dataBag,
    ),
    { kind: "supplied", value: 40 } satisfies SuppliedValue,
    );
    assert.deepEqual(
    orders.price(
    charge,
    "amount",
    "A-1",
    reading.journeyId,
    read.status.dataBag,
    ),
    { kind: "fromDataBag" } satisfies FromDataBag,
    );
    })();

    function charged(bag: DataBag): number {
    return bag.read("charged", integer).option().unwrap();
    }

    Parameters

    Returns InputAdapterDecorator

    Error, when the class is defined, if its name is empty, if a parameter has no request or one an adapter may not make, or if the compiler option emitDecoratorMetadata is off.