Interface WorkflowDescriptor

How a workflow is described to the executor: its name, its steps, its policies, its reporters, how it asks an input adapter for an input, how it asks a service provider for a service, how it makes a journey ID, and its listing. The executor reads this and nothing else about the workflow, and passes the workflow object to the calls that need it.

Only itinera makes one, when a class declared with @workflow is defined or when a workflow's builder builds it, after checking it, and it never changes afterwards: an object written by hand, with the same members, is not a workflow descriptor and does not compile where one is expected. It is the executor's contract, not a way to declare a workflow.

import assert from "node:assert/strict";
import { type Provided, provider, service, serviceType } from "itinera/service";
import {
type Outcome,
Step,
step,
type StepName,
stepName,
} from "itinera/step";
import {
inputAdapter,
key,
type Supplied,
withStep,
Workflow,
workflow,
} from "itinera/workflow";

const RECEIPTS = serviceType<string[]>("Receipts");

@step()
class Charge extends Step {
override run(@service("receipts", RECEIPTS) receipts: string[]): Outcome {
receipts.push("charged");
return this.succeed();
}
}

@workflow({ name: "payments" })
@withStep(Charge, {
name: "charge",
retryBudget: 2,
noRetryOnAbnormalTermination: true,
})
class Payments extends Workflow {
@inputAdapter({ steps: ["charge"] })
price(@key() key: string): Supplied {
return this.supply(key.length);
}

@provider(RECEIPTS, { perAttempt: true })
receipts(@stepName() step: StepName): Provided<string[]> {
return this.provide([`receipts of ${step}`]);
}
}

const { descriptor, workflow: payments } = Payments.build().instance();
const [charge] = descriptor.steps();
assert.ok(charge);

assert.equal(descriptor.name, "payments");
assert.equal(charge.name, "charge");
assert.equal(charge.retryBudget, 2);
assert.equal(charge.retryOnAbnormalTermination, false);
assert.ok(charge.build() instanceof Charge);
assert.deepEqual(descriptor.reporters(), []);
assert.deepEqual(descriptor.adapt(payments, charge.name, ["amount"]), {
kind: "supplied",
value: 6,
});
assert.deepEqual(
descriptor.provide(payments, charge.name, "receipts", [charge.name]),
{ instance: ["receipts of charge"] },
);
interface WorkflowDescriptor {
    name: WorkflowName;
    adapt(
        workflow: Workflow,
        step: StepName,
        answers: readonly unknown[],
    ): unknown;
    generateJourneyId(
        workflow: Workflow,
        initialData: ReadonlyMap<string, Value>,
    ): JourneyId;
    listing(): readonly ListedStep[];
    policies(): readonly PolicyDescriptor[];
    provide(
        workflow: Workflow,
        step: StepName,
        key: string,
        answers: readonly unknown[],
    ): unknown;
    reporters(): readonly Reporter[];
    steps(): readonly StepDescriptor[];
}

Index

The workflow's name, which every event of its journeys carries.

  • Asks the input adapter attached to the step step for one of the step's inputs: calls the adapter's method on the workflow object given, with answers, what its requests were answered with, in the order the adapter declares them, and gives what the method returns.

    import assert from "node:assert/strict";
    import { type Provided, provider, service, serviceType } from "itinera/service";
    import {
    type Outcome,
    Step,
    step,
    type StepName,
    stepName,
    } from "itinera/step";
    import {
    inputAdapter,
    key,
    type Supplied,
    withStep,
    Workflow,
    workflow,
    } from "itinera/workflow";

    const RECEIPTS = serviceType<string[]>("Receipts");

    @step()
    class Charge extends Step {
    override run(@service("receipts", RECEIPTS) receipts: string[]): Outcome {
    receipts.push("charged");
    return this.succeed();
    }
    }

    @workflow({ name: "payments" })
    @withStep(Charge, {
    name: "charge",
    retryBudget: 2,
    noRetryOnAbnormalTermination: true,
    })
    class Payments extends Workflow {
    @inputAdapter({ steps: ["charge"] })
    price(@key() key: string): Supplied {
    return this.supply(key.length);
    }

    @provider(RECEIPTS, { perAttempt: true })
    receipts(@stepName() step: StepName): Provided<string[]> {
    return this.provide([`receipts of ${step}`]);
    }
    }

    const { descriptor, workflow: payments } = Payments.build().instance();
    const [charge] = descriptor.steps();
    assert.ok(charge);

    assert.equal(descriptor.name, "payments");
    assert.equal(charge.name, "charge");
    assert.equal(charge.retryBudget, 2);
    assert.equal(charge.retryOnAbnormalTermination, false);
    assert.ok(charge.build() instanceof Charge);
    assert.deepEqual(descriptor.reporters(), []);
    assert.deepEqual(descriptor.adapt(payments, charge.name, ["amount"]), {
    kind: "supplied",
    value: 6,
    });
    assert.deepEqual(
    descriptor.provide(payments, charge.name, "receipts", [charge.name]),
    { instance: ["receipts of charge"] },
    );

    Parameters

    Returns unknown

    TypeError if no input adapter is attached to the step.

    Whatever the adapter's method throws.

  • Makes the journey ID of a new instance of the workflow object given, holding initialData, with the workflow's ID generator or as a random UUID.

    Parameters

    Returns JourneyId

    TypeError if the ID generator returns what is not a string, or an empty string.

    Whatever the ID generator throws.

  • Lists the workflow's steps in order, without running anything: each step's name, its position, the names of its policies, the name of its input adapter, if it has one, the services it requests, each with the provider that serves it, and a rescue section for a step with a rescue step.

    import assert from "node:assert/strict";
    import {
    type OnSuccess,
    onStepSuccess,
    StepPolicy,
    stepPolicy,
    } from "itinera/policy";
    import { type Provided, provider, service, serviceType } from "itinera/service";
    import {
    type Outcome,
    RescueStep,
    rescueStep,
    type RescueOutcome,
    Step,
    step,
    } from "itinera/step";
    import {
    inputAdapter,
    key,
    type ListedStep,
    type Supplied,
    withStep,
    Workflow,
    workflow,
    } from "itinera/workflow";

    interface PaymentGateway {
    take(amount: number): string;
    }

    const PAYMENT_GATEWAY = serviceType<PaymentGateway>("PaymentGateway");

    @step()
    class Charge extends Step {
    override run(
    @service("payments", PAYMENT_GATEWAY) payments: PaymentGateway,
    ): Outcome {
    payments.take(40);
    return this.succeed();
    }
    }

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

    @rescueStep()
    class Refund extends RescueStep {
    override run(): RescueOutcome {
    return this.succeed();
    }
    }

    @stepPolicy({ name: "audit" })
    class Audit extends StepPolicy {
    @onStepSuccess()
    audited(): OnSuccess {
    return this.keepDefault();
    }
    }

    @workflow({ name: "orders" })
    @withStep(Charge, { name: "charge", policies: [Audit], rescue: Refund })
    @withStep(Ship, { name: "ship" })
    class Orders extends Workflow {
    @inputAdapter({ steps: ["charge"] })
    pricing(@key() inputKey: string): Supplied {
    return this.supply(inputKey.length);
    }

    @provider(PAYMENT_GATEWAY, { name: "gateway" })
    payments(): Provided<PaymentGateway> {
    return this.provide({ take: (amount) => `${String(amount)} paid` });
    }
    }

    const listing: readonly ListedStep[] = Orders.listing();

    assert.deepEqual(listing, [
    {
    step: "charge",
    position: 1,
    policies: ["audit"],
    adapter: "pricing",
    services: [{ key: "payments", provider: "gateway" }],
    rescue: { step: "charge_rescue", services: [] },
    },
    { step: "ship", position: 2, policies: [], services: [] },
    ]);
    assert.deepEqual(
    Orders.builder()
    .step(Ship.builder("return"))
    .listing()
    .map((listed) => listed.step),
    ["charge", "ship", "return"],
    );

    Returns readonly ListedStep[]

  • Asks the service provider serving the step step for the service under key: calls the provider's method on the workflow object given, with answers, what its requests were answered with, in the order the provider declares them, and gives what the method returns.

    import assert from "node:assert/strict";
    import { type Provided, provider, service, serviceType } from "itinera/service";
    import {
    type Outcome,
    Step,
    step,
    type StepName,
    stepName,
    } from "itinera/step";
    import {
    inputAdapter,
    key,
    type Supplied,
    withStep,
    Workflow,
    workflow,
    } from "itinera/workflow";

    const RECEIPTS = serviceType<string[]>("Receipts");

    @step()
    class Charge extends Step {
    override run(@service("receipts", RECEIPTS) receipts: string[]): Outcome {
    receipts.push("charged");
    return this.succeed();
    }
    }

    @workflow({ name: "payments" })
    @withStep(Charge, {
    name: "charge",
    retryBudget: 2,
    noRetryOnAbnormalTermination: true,
    })
    class Payments extends Workflow {
    @inputAdapter({ steps: ["charge"] })
    price(@key() key: string): Supplied {
    return this.supply(key.length);
    }

    @provider(RECEIPTS, { perAttempt: true })
    receipts(@stepName() step: StepName): Provided<string[]> {
    return this.provide([`receipts of ${step}`]);
    }
    }

    const { descriptor, workflow: payments } = Payments.build().instance();
    const [charge] = descriptor.steps();
    assert.ok(charge);

    assert.equal(descriptor.name, "payments");
    assert.equal(charge.name, "charge");
    assert.equal(charge.retryBudget, 2);
    assert.equal(charge.retryOnAbnormalTermination, false);
    assert.ok(charge.build() instanceof Charge);
    assert.deepEqual(descriptor.reporters(), []);
    assert.deepEqual(descriptor.adapt(payments, charge.name, ["amount"]), {
    kind: "supplied",
    value: 6,
    });
    assert.deepEqual(
    descriptor.provide(payments, charge.name, "receipts", [charge.name]),
    { instance: ["receipts of charge"] },
    );

    Parameters

    Returns unknown

    TypeError if no service provider serves the step under the key.

    Whatever the provider's method throws.