Interface ProviderOptions

What @provider says about a service provider.

import assert from "node:assert/strict";
import { type JourneyId, journeyId } from "itinera/journey";
import {
type Provided,
provider,
type ProviderDecorator,
type ProviderOptions,
providerReporter,
type ProviderReporter,
service,
serviceType,
} from "itinera/service";
import {
Attempt,
attempt,
type Outcome,
Step,
step,
type StepName,
stepName,
} from "itinera/step";
import { 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();
}
}

const perAttempt: ProviderOptions = {
key: "payments",
perAttempt: true,
steps: ["charge"],
name: "fresh gateway",
};
const journeyLong: ProviderDecorator<PaymentGateway> =
provider(PAYMENT_GATEWAY);

@workflow({ name: "orders" })
@withStep(Charge, { name: "charge" })
class Orders extends Workflow {
@journeyLong
async payments(
@journeyId() id: JourneyId,
@providerReporter() reporter: ProviderReporter,
): Promise<Provided<PaymentGateway>> {
await reporter.info("gateway opened");
return this.provide(
{ take: (amount) => `${id}: ${String(amount)}` },
{ region: "eu" },
);
}

@provider(PAYMENT_GATEWAY, perAttempt)
fresh(
@stepName() step: StepName,
@attempt() attempt: Attempt,
): Provided<PaymentGateway> {
return this.provide({
take: (amount) => `${step} ${String(attempt.number)}: ${String(amount)}`,
});
}
}

class Reported implements ProviderReporter {
readonly lines: string[] = [];

info(message: string): Promise<void> {
return this.record(`info: ${message}`);
}

warning(message: string): Promise<void> {
return this.record(`warning: ${message}`);
}

error(message: string): Promise<void> {
return this.record(`error: ${message}`);
}

setGlobalMetadata(key: string): Promise<void> {
return this.record(`metadata: ${key}`);
}

private record(line: string): Promise<void> {
this.lines.push(line);
return Promise.resolve();
}
}

export default (async () => {
const orders = Orders.build();
const reporter = new Reported();

const lent = await orders.payments("R-1" as JourneyId, reporter);
const fresh = orders.fresh("charge" as StepName, Attempt.first);

assert.equal(lent.instance.take(40), "R-1: 40");
assert.deepEqual(lent.data, { region: "eu" });
assert.deepEqual(reporter.lines, ["info: gateway opened"]);
assert.equal(fresh.instance.take(40), "charge 1: 40");
assert.equal(fresh.data, undefined);
})();
interface ProviderOptions {
    key?: string;
    name?: string;
    perAttempt?: boolean;
    steps?: readonly [string, string];
}

Index

key?: string

The key of the service it supplies: the method's name when not given.

name?: string

Its name, unique among the workflow's providers: the method's name when not given.

perAttempt?: boolean

true to make an instance for every attempt of every step it serves: a provider of attempt scope. Otherwise it is of journey scope, called once per journey, for the first attempt that requests its key.

steps?: readonly [string, string]

The steps it serves, by name, and <step name>_rescue for a rescue step. When not given, it is attached to no step and serves every step that requests its key and has no provider of its own for it.