Interface WorkflowPolicyBuilder<C>

Puts a workflow policy of the class C together for a workflow's builder, which takes it in its policy: started by the policy class's static builder(name), for a class declared without decorators. Each method names the method of the class that implements a hook, and gives the requests of its parameters by position.

import assert from "node:assert/strict";
import { type Contributor, contributor } from "itinera/journey";
import {
failureCause,
fromStep,
type OnFailure,
type OnSuccess,
type PolicyDescriptor,
role,
type StepFailureCause,
StepPolicy,
type StepPolicyBuilder,
WorkflowPolicy,
type WorkflowPolicyBuilder,
} from "itinera/policy";
import { Step, type Outcome } from "itinera/step";
import { Workflow } from "itinera/workflow";
import { integer } from "itinera/value";

/** A role the shop provides: telling a customer something. */
interface Messages {
tell(message: string): void;
}

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

class Audit extends StepPolicy {
audited(charged: number, contributing: Contributor): OnSuccess {
contributing.contribute("audit", `charged ${String(charged)}`);
return this.keepDefault();
}

failed(cause: StepFailureCause): OnFailure {
return this.failWorkflow(cause);
}
}

class Notify extends WorkflowPolicy<Messages> {
paid(messages: Messages): void {
messages.tell("paid");
}
}

class Orders extends Workflow implements Messages {
readonly told: string[] = [];

tell(message: string): void {
this.told.push(message);
}
}

const audit: StepPolicyBuilder<typeof Audit> = Audit.builder("audit")
.onStepSuccess("audited", fromStep("charged", integer), contributor())
.onStepFailure("failed", failureCause());
const notify: WorkflowPolicyBuilder<typeof Notify> = Notify.builder(
"notify",
).onWorkflowSuccess("paid", role<Messages>());

const orders = Orders.builder()
.name("orders")
.step(Charge.builder("charge").policy(audit))
.policy(notify)
.build();

const descriptor = orders.instance().descriptor;
const attached: readonly PolicyDescriptor[] =
descriptor.steps()[0]?.policies() ?? [];

assert.deepEqual(
attached.map((policy) => policy.name),
["audit"],
);
assert.deepEqual(attached[0]?.hooks(), ["on step success", "on step failure"]);
const built = attached[0].build();
assert.ok(built instanceof Audit);
assert.deepEqual(attached[0].call(built, "on step failure", ["failure"]), {
kind: "FailWorkflow",
reason: { code: "failure" },
});
assert.deepEqual(
descriptor.policies().map((policy) => policy.name),
["notify"],
);

class Counter extends Workflow {}

// @ts-expect-error Counter does not implement Messages, which notify requests
Counter.builder().policy(notify);
interface WorkflowPolicyBuilder<C extends WorkflowPolicyClass> {
    onWorkflowFailure<K extends string | symbol>(
        key: K,
        ...requests: Tokens<
            ParametersOf<InstanceType<C>, K>,
            Place<InstanceType<C>, K>,
        >,
    ): this;
    onWorkflowSuccess<K extends string | symbol>(
        key: K,
        ...requests: Tokens<
            ParametersOf<InstanceType<C>, K>,
            Place<InstanceType<C>, K>,
        >,
    ): this;
}

Type Parameters

Index