Interface ServiceType<T>

A type of service, T, as a token that exists while the program runs, since an interface does not. A step requests a service by its key and its service type, and the provider serving it declares the same service type. Two service types are the same only when they are the same object.

import assert from "node:assert/strict";
import { service, serviceType, type ServiceType } from "itinera/service";
import { type Outcome, Step, step } from "itinera/step";

interface PaymentGateway {
take(amount: number): string;
}

const PAYMENT_GATEWAY: ServiceType<PaymentGateway> =
serviceType<PaymentGateway>("PaymentGateway");

@step()
class Charge extends Step {
override run(
@service("payments", PAYMENT_GATEWAY) payments: PaymentGateway,
): Outcome {
payments.take(40);
return this.succeed();
}
}

const taken: number[] = [];
const gateway: PaymentGateway = {
take: (amount) => {
taken.push(amount);
return `${String(amount)} paid`;
},
};

new Charge().run(gateway);
assert.equal(PAYMENT_GATEWAY.name, "PaymentGateway");
assert.deepEqual(taken, [40]);
interface ServiceType<T> {
    "[served]": T;
    name: string;
}

Type Parameters

  • T

    The type of the service.

Index

"[served]": T

Marks the token as one for services of type T.

name: string

The name of the type, for people, such as "PaymentGateway".