Alex2Node/src/messages/types.ts
David 46fa06728c messages: pure builders for every event the bridge sends
src/messages builds Response, StateReport, DeferredResponse, ErrorResponse, ChangeReport and the scene, doorbell
and simple events from plain values; messageId and times are parameters, so a test compares whole objects.
StateBuilder collects properties checked by the descriptors, AlexaError and AlexaErrors carry the payload fields
of an error type. AlexaStatusMessage and AlexaErrorResponse move to src/compat and are written on the builders.
On the wire, against 1.5.2: a DeferredResponse has no context key, a ChangeReport has no correlationToken, the
namespace of an ErrorResponse follows its type, and a ChangeReport without a changed property is not published:
send() resolves "" and the bridge reports an error that names the endpoint.
143 tests pass (108 before).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-28 19:28:05 +00:00

74 lines
2.2 KiB
TypeScript

// The messages the bridge publishes, as message-guide.html and alexa-response.html draw them.
/**
* Why a property changed or an event was raised (message-guide.html, "Cause object"). RULE_TRIGGER is not in that
* table; the ChangeReport examples of alexa-securitypanelcontroller.html and alexa-thermostatcontroller.html use it.
*/
export type ChangeCause = "APP_INTERACTION" | "PERIODIC_POLL" | "PHYSICAL_INTERACTION" | "RULE_TRIGGER" | "VOICE_INTERACTION";
export interface Header {
namespace: string;
name: string;
/** Alexa.SimpleEventSource: the instance that raised the event. */
instance?: string;
messageId: string;
/** The token of the directive that is answered. An event nobody asked for has none. */
correlationToken?: string;
payloadVersion: string;
}
export interface Endpoint {
endpointId: string;
}
/** One reported property (message-guide.html, "Property object"). */
export interface Property {
namespace: string;
/** The instance of a generic controller. */
instance?: string;
name: string;
value: unknown;
/** When the value was sampled or last changed, ISO 8601 in UTC. */
timeOfSample: string;
uncertaintyInMilliseconds: number;
}
export interface Event<P = Record<string, unknown>> {
header: Header;
endpoint: Endpoint;
payload: P;
}
export interface Context {
properties: Property[];
}
/** A Response, a StateReport or the response of an interface that has its own (Arm.Response). */
export interface ResponseMessage {
event: Event;
context: Context;
}
export interface DeferredResponseMessage {
event: Event<{ estimatedDeferralInSeconds?: number }>;
}
export interface ErrorResponseMessage {
event: Event<{ type: string; message: string } & Record<string, unknown>>;
}
export interface ChangeReportMessage {
event: Event<{ change: { cause: { type: ChangeCause }; properties: Property[] } }>;
context: Context;
}
/** ActivationStarted or DeactivationStarted of a scene. */
export interface SceneEventMessage {
event: Event<{ cause: { type: ChangeCause }; timestamp: string }>;
context: {};
}
/** An event a device raises by itself: DoorbellPress, the Event of Alexa.SimpleEventSource. */
export interface ProactiveEventMessage {
event: Event;
}