Interface ValueRequestDecorator<T, P>

A decorator that marks a parameter with a request for a value, whose type is T, or, when T is undefined, the parameter's own type, read from the metadata the compiler records. It compiles only on a parameter in one of the places P names, whose declared type accepts what the request gives, as Answer says; with no type written, only on one whose type the metadata can say, as ReadFromMetadata says.

import assert from "node:assert/strict";
import { None, type Option, Some } from "itinera/option";
import {
input,
type Outcome,
Step,
step,
type StepClass,
stepReporter,
type StepReporter,
} from "itinera/step";
import type {
Answer,
KeyIn,
ParameterOf,
Place,
ReadFromMetadata,
RequestDecorator,
ValueRequestDecorator,
} from "itinera/workflow";
import { integer, optional, string } from "itinera/value";

@step()
class Charge extends Step {
constructor(
@input("amount", integer) readonly amount: number,
@input("coupon", optional(string)) readonly coupon: Option<string>,
@input("note") readonly note: string,
) {
super();
}

override run(): Outcome {
return this.succeed();
}
}

type InConstructor = Place<StepClass, undefined>;
const amount: ParameterOf<typeof Charge, undefined, 0> = 40;
const given: Answer<typeof integer> = amount;
const coupon: Answer<ReturnType<typeof optional<typeof string>>> =
Some("WELCOME");
const noted: ReadFromMetadata<string> = true;
const key: KeyIn<InConstructor, typeof Charge> = undefined;
const amountDecorator: ValueRequestDecorator<typeof integer, InConstructor> =
input("amount", integer);
const reporterDecorator: RequestDecorator<
StepReporter,
Place<Step, "run">
> = stepReporter();

const charge = new Charge(given, None, "first order");
assert.equal(charge.coupon.unwrapOr("none"), "none");
assert.equal(coupon.unwrapOr("none"), "WELCOME");
assert.ok(noted);
assert.equal(key, undefined);
assert.equal(typeof amountDecorator, "function");
assert.equal(typeof reporterDecorator, "function");
interface ValueRequestDecorator<
    T extends RequestType
    | undefined,
    P extends Place,
> {
    "[token]": T extends RequestType
        ? Given<Answer<T>, P, true>
        : Given<unknown, P, false>;
    <
        X extends unknown,
        K extends string | symbol | undefined,
        I extends number,
    >(
        target: X,
        key: K,
        index: T extends RequestType
            ? [Answer<T>] extends [ParameterOf<X, K, I>]
                ? I
                : "the parameter's type does not accept what the request gives"
            : ReadFromMetadata<ParameterOf<X, K, I>> extends true
                ? I
                : "write the request's type: the metadata cannot say this parameter's",
    ): void;
}

Type Parameters

  • Marks the parameter at position index of the constructor of target, when key is undefined, or of its method key.

    Type Parameters

    • X extends unknown
    • K extends string | symbol | undefined
    • I extends number

    Parameters

    • target: X
    • key: K
    • index: T extends RequestType
          ? [Answer<T>] extends [ParameterOf<X, K, I>]
              ? I
              : "the parameter's type does not accept what the request gives"
          : ReadFromMetadata<ParameterOf<X, K, I>> extends true
              ? I
              : "write the request's type: the metadata cannot say this parameter's"

    Returns void

Index
"[token]": T extends RequestType
    ? Given<Answer<T>, P, true>
    : Given<unknown, P, false>

What the request gives and where it may be made, for a builder that is given it by position, which takes it only when its type is written. It exists only in the types.