itinera - v0.1.0

itinera

The TypeScript implementation of Itinera, a workflow framework that keeps business rules separate from flow control. It implements the Itinera specification, whose vocabulary it uses.

A workflow is a list of steps. Each step holds one piece of business logic and says only how it ended: it succeeded, it failed with a reason, or it was skipped. Everything around the steps, such as retrying a failed attempt, passing data from one step to the next, lending a step the services it needs, and telling every reporter what happened, is the executor's work, done the same way for every workflow. Policies attached to a step or to the whole workflow change those decisions in one place, without touching the steps.

npm install itinera reflect-metadata

itinera runs on Node 22.12 and later, and works with import and require alike. It uses only the language and the web platform, so it also runs on Deno and Bun, and in browsers through a bundler, though CI tests it on Node alone; in a browser, the default journey IDs need a secure context.

Applications declare their workflows as classes marked with decorators, which need TypeScript 5.5 or later with strict, experimentalDecorators and emitDecoratorMetadata turned on, and a compiler that records decorator metadata, such as tsc or swc. An application whose compiler records none, such as esbuild, Vite or tsx, puts the same classes together with the builder instead, Orders.builder(), which needs only TypeScript 5.5 or later with strict.

The workflow below charges an order and then ships it. A failed charge is retried up to twice, and a charge of nothing fails, so the second journey fails once its retries are exhausted.

import assert from "node:assert/strict";
import { LocalExecutor } from "itinera/executor";
import { type Contributor, contributor } from "itinera/journey";
import { input, type Outcome, Step, step } from "itinera/step";
import { integer, string } from "itinera/value";
import { withStep, Workflow, workflow } from "itinera/workflow";

@step()
class Charge extends Step {
constructor(@input("amount", integer) private readonly amount: number) {
super();
}

override run(@contributor() contributor: Contributor): Outcome {
if (this.amount <= 0) return this.fail("nothing to charge");
contributor.contribute("receipt", `charged ${String(this.amount)}`);
return this.succeed();
}
}

@step()
class Ship extends Step {
constructor(@input("receipt", string) private readonly receipt: string) {
super();
}

override run(@contributor() contributor: Contributor): Outcome {
contributor.contribute("shipped", `after ${this.receipt}`);
return this.succeed();
}
}

@workflow({ name: "orders" })
@withStep(Charge, { name: "charge", retryBudget: 2 })
@withStep(Ship, { name: "ship" })
class Orders extends Workflow {}

export default (async () => {
const executor = new LocalExecutor();
const orders = Orders.build();

const paid = await executor.run(orders.instance({ amount: 40 }));
assert.ok(paid.isSucceeded());
assert.deepEqual(paid.output({ shipped: string }), {
shipped: "after charged 40",
});

const free = await executor.run(orders.instance({ amount: 0 }));
assert.ok(free.isFailed());
assert.equal(free.failure?.cause, "retries exhausted");
assert.deepEqual(free.failure.reason, { code: "nothing to charge" });
})();
  • A step holds one business rule: a class extending Step that requests the data it needs, which the workflow supplies, runs once per attempt, and reports an outcome: success, failure or skipped. Only an attempt that succeeds commits what the step contributed. A step may have a rescue step, which runs when the step fails or terminates abnormally and, once it succeeds, lets the step run again. See itinera/step.
  • A workflow lists its steps in the order they run, with their retry budgets and step policies, its workflow policies, its input adapters, its service providers, the roles it provides, its ID generator and its reporters. It is declared once, before any journey, and building a workflow whose declaration breaks a rule of the specification throws Violations. See itinera/workflow.
  • A step policy holds flow control for the steps it is attached to. Its hooks, on step success, on step failure, on step retry and on step abnormal termination, are called in response to a step's outcome, and may end the journey early. See itinera/policy.
  • A workflow policy acts when the journey ends: its hooks, on workflow success and on workflow failure, are called before the result is produced, and what they contribute is part of the journey's output. Any hook may use a role, an interface of operations the workflow provides, such as sending a notification. See itinera/policy.
  • What happens in a journey is recorded as events, which the journey's reporters receive, together with the journey's global metadata: entries of a key and a value, apart from the data bag, starting with the journey ID. See itinera/event and itinera/report.
  • A service is an object of operations a step uses to do its business, such as a payment gateway. The step requests it by key and service type, and a service provider of the workflow makes it, for the journey or for each attempt. See itinera/service.
  • A workflow instance is made for each journey, with its initial data, its reporters and any initial global metadata, and an executor runs it to a result: the journey succeeded, failed or was aborted. See itinera/instance, itinera/executor and itinera/journey.

The package root exports nothing of its own. Each concept is imported from a subpath of its own:

Subpath What it holds
itinera/workflow The base class every workflow extends, how its steps are declared, and the builder
itinera/step Steps, their attempts, their outcomes and the reasons they give
itinera/policy Step policies and workflow policies, their hooks, and the roles they request
itinera/service Services a workflow lends its steps, and the providers that make them
itinera/journey Journeys, their data, how they end, and the result they give
itinera/instance Workflow instances, one per journey
itinera/executor Executors, which run journeys, and what run rejects with when it refuses an instance
itinera/event Events, the record of what happens in a journey
itinera/closed-set The types of the closed sets of names, such as the kinds of events and the hooks
itinera/report Reporters, which receive the events, and dispatchers, which deliver them
itinera/value Values, the only data a journey holds, and the type tokens that check them
itinera/schema Zod, which the type tokens are built on, for an application's own schemas
itinera/option Option, Some and None
itinera/testing Test support, for an application's own tests
itinera/unstable API that may still change, empty while every part of itinera is stable

0.1.0, the fourth release and the first that is not a pre-release, implements tier 1 of specification 0.1.0, with the capabilities async, testing and value-conversions, and passes the conformance cases v0.1.0. Before 1.0, a change that breaks code written for a release raises the minor version. The changelog says what each release implements, and each GitHub release carries the report of the conformance cases it passes.

The specification lets an implementation make some rules impossible to express rather than check them while a workflow runs. In TypeScript, these mistakes do not compile:

  • a hook returning a lifecycle it may not return;
  • a policy needing a role the workflow does not provide;
  • a rescue step that skips;
  • a service provider returning anything but what this.provide(...) makes;
  • an input adapter writing to the data bag;
  • custom code returning what its call cannot return, such as a step's run returning something that is not an outcome;
  • an ID generator returning anything but a string.

The contributor guide says how work arrives in the repository, how itinera-ts is put together, how to build and test it and how to run the conformance cases. Itinera is built with AI under the terms of A manifesto for software engineering with AI; see how Itinera is built.

Licensed under either of the Apache License, Version 2.0 or the MIT license, at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.