Interface HookReporter

What a hook reports through during one call: its own events, journey_info, journey_warning and journey_error, each with a message and, optionally, data, and global metadata. Each event names the policy and the hook, and, for a step hook, the step and its attempt. itinera makes one for each call and closes it when the call ends.

Each call settles once every reporter has been given what it reports, so await it; itinera keeps the order of calls a hook does not await.

import assert from "node:assert/strict";
import {
type HookReporter,
hookReporter,
onWorkflowSuccess,
WorkflowPolicy,
workflowPolicy,
} from "itinera/policy";

@workflowPolicy({ name: "notify" })
class Notify extends WorkflowPolicy {
@onWorkflowSuccess()
async paid(@hookReporter() reporter: HookReporter): Promise<void> {
await reporter.info("customer told", { channel: "email" });
await reporter.setGlobalMetadata("channel", "email");
}
}

class Reported implements HookReporter {
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();
}
}

const reporter = new Reported();

export default (async () => {
await new Notify().paid(reporter);
assert.deepEqual(reporter.lines, [
"info: customer told",
"metadata: channel",
]);
})();
interface HookReporter {
    error<T>(message: string, data?: T & AsValue<T>): Promise<void>;
    info<T>(message: string, data?: T & AsValue<T>): Promise<void>;
    setGlobalMetadata<T>(key: string, value: T & AsValue<T>): Promise<void>;
    warning<T>(message: string, data?: T & AsValue<T>): Promise<void>;
}

Index

  • Emits journey_error, with message and, if given, data.

    Type Parameters

    • T

    Parameters

    • message: string
    • Optionaldata: T & AsValue<T>

    Returns Promise<void>

  • Emits journey_info, with message and, if given, data.

    Type Parameters

    • T

    Parameters

    • message: string
    • Optionaldata: T & AsValue<T>

    Returns Promise<void>

  • Sets the global metadata key to value on every reporter.

    Type Parameters

    • T

    Parameters

    Returns Promise<void>

  • Emits journey_warning, with message and, if given, data.

    Type Parameters

    • T

    Parameters

    • message: string
    • Optionaldata: T & AsValue<T>

    Returns Promise<void>