Interface PolicyDescriptor
interface PolicyDescriptor {
name: PolicyName;
build(): StepPolicy<unknown> | WorkflowPolicy<unknown>;
call(
policy: StepPolicy<unknown> | WorkflowPolicy<unknown>,
hook:
| "on step success"
| "on step failure"
| "on step retry"
| "on step abnormal termination"
| "on workflow success"
| "on workflow failure",
answers: readonly unknown[],
): unknown;
hooks(): readonly (
| "on step success"
| "on step failure"
| "on step retry"
| "on step abnormal termination"
| "on workflow success"
| "on workflow failure"
)[];
}
name: PolicyName;
build(): StepPolicy<unknown> | WorkflowPolicy<unknown>;
call(
policy: StepPolicy<unknown> | WorkflowPolicy<unknown>,
hook:
| "on step success"
| "on step failure"
| "on step retry"
| "on step abnormal termination"
| "on workflow success"
| "on workflow failure",
answers: readonly unknown[],
): unknown;
hooks(): readonly (
| "on step success"
| "on step failure"
| "on step retry"
| "on step abnormal termination"
| "on workflow success"
| "on workflow failure"
)[];
}
Methods
build
-
Builds the policy, as a new object: for every attempt of a step policy's step, or once per journey for a workflow policy.
Returns StepPolicy<unknown> | WorkflowPolicy<unknown>
call
-
call(
policy: StepPolicy<unknown> | WorkflowPolicy<unknown>,
hook:
| "on step success"
| "on step failure"
| "on step retry"
| "on step abnormal termination"
| "on workflow success"
| "on workflow failure",
answers: readonly unknown[],
): unknownCalls the hook
hookonpolicy, an instance PolicyDescriptor.build made: the method that implements it, withanswers, what its requests were answered with, in the order it declares them, and gives what the method returns.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);Parameters
- policy: StepPolicy<unknown> | WorkflowPolicy<unknown>
- hook:
| "on step success"
| "on step failure"
| "on step retry"
| "on step abnormal termination"
| "on workflow success"
| "on workflow failure" - answers: readonly unknown[]
Returns unknown
hooks
-
hooks(): readonly (
| "on step success"
| "on step failure"
| "on step retry"
| "on step abnormal termination"
| "on workflow success"
| "on workflow failure"
)[]The hooks the policy defines, in the order they were declared.
Returns readonly (
| "on step success"
| "on step failure"
| "on step retry"
| "on step abnormal termination"
| "on workflow success"
| "on workflow failure"
)[]
How a workflow describes one of its policies to the executor: its name, the hooks it defines, and how to build an instance of it. The executor reads it and never anything else about the policy.
Only itinera makes one, for each policy declared with
@stepPolicyor@workflowPolicy, or put together with a policy's builder: an object written by hand, with the same members, is not a policy descriptor and does not compile where one is expected.