Interface InputAdapterOptions

What @inputAdapter says about an input adapter.

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();
}
interface InputAdapterOptions {
    name?: string;
    steps: readonly [string, string];
}

Index

name?: string

Its name, unique among the workflow's adapters: the method's name when not given.

steps: readonly [string, string]

The steps it adapts the inputs of, by name, and <step name>_rescue for a rescue step.