Function serviceType

  • A new type of service, named name, for services of type T. Make one per type of service, and share it between the steps that request it and the providers that supply it.

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

    Type Parameters

    • T

    Parameters

    • name: string

    Returns ServiceType<T>

    Error if the name is empty.