Type Alias Provides<W, R>

Provides: [W] extends [R]
    ? unknown
    : "the workflow class does not implement the roles its policies request"

Nothing more, unknown, when a workflow whose objects are of type W implements the roles R; otherwise a message saying it does not, which nothing given fits, so that a policy whose roles the workflow does not implement does not compile where the workflow's builder is given it.

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);

Type Parameters

  • W
  • R