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>
70 lines
3.4 KiB
JavaScript
70 lines
3.4 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.StateBuilder = void 0;
|
|
const EndpointHealth_js_1 = require("../registry/interfaces/EndpointHealth.js");
|
|
const schema_js_1 = require("../registry/schema.js");
|
|
const property_js_1 = require("./property.js");
|
|
/**
|
|
* Collects the properties of one message. For a Response or a StateReport they are its context. For a ChangeReport
|
|
* changed() and unchanged() say which of the two lists the properties that follow belong to.
|
|
*/
|
|
class StateBuilder {
|
|
constructor(options = {}) {
|
|
this.lists = { context: [], change: [] };
|
|
this.target = options.target ?? "context";
|
|
this.now = options.now ?? (() => new Date());
|
|
}
|
|
/**
|
|
* A property of an interface the library describes, its value checked: set(PowerController, "powerState", "ON").
|
|
* Given a capability of a device in place of the interface, the property carries the instance of the capability.
|
|
* Throws SchemaError for a value Alexa would not take.
|
|
*/
|
|
set(source, name, value, options = {}) {
|
|
const descriptor = "descriptor" in source ? source.descriptor : source;
|
|
const instance = options.instance ?? ("descriptor" in source ? source.instance : "");
|
|
const where = `${descriptor.namespace}${instance ? ` "${instance}"` : ""}: ${name}`;
|
|
const described = descriptor.properties[name];
|
|
if (!described) {
|
|
const names = Object.keys(descriptor.properties);
|
|
throw new schema_js_1.SchemaError(where, names.length > 0
|
|
? `not a property of the interface, which has ${names.join(", ")}`
|
|
: "the library knows no property of the interface, report it with setRaw()");
|
|
}
|
|
if (descriptor.instanced && !instance) {
|
|
throw new schema_js_1.SchemaError(where, "the interface has instances, pass the one that is reported: { instance: \"Blind.Lift\" }");
|
|
}
|
|
return this.setRaw(descriptor.namespace, described.name, described.value.parse(value, where), { ...options, instance });
|
|
}
|
|
/** A property as given, nothing checked: for an interface or a value the library does not describe. */
|
|
setRaw(namespace, name, value, options = {}) {
|
|
return this.add((0, property_js_1.property)(namespace, name, value, { ...options, timeOfSample: options.timeOfSample ?? this.now() }));
|
|
}
|
|
/** The connectivity of Alexa.EndpointHealth, which belongs in every report of an endpoint that declares it. */
|
|
health(value, reason, options = {}) {
|
|
return this.set(EndpointHealth_js_1.EndpointHealth, "connectivity", reason ? { value, reason } : { value }, options);
|
|
}
|
|
/** A property built elsewhere, to the list that is filled now or to the one named. */
|
|
add(built, target = this.target) {
|
|
this.lists[target].push(built);
|
|
return this;
|
|
}
|
|
/** The properties that follow are the ones that changed. */
|
|
changed() {
|
|
this.target = "change";
|
|
return this;
|
|
}
|
|
/** The properties that follow did not change: a ChangeReport lists them in its context. */
|
|
unchanged() {
|
|
this.target = "context";
|
|
return this;
|
|
}
|
|
/** The properties for the context of the message, in the order they were set. */
|
|
get context() {
|
|
return [...this.lists.context];
|
|
}
|
|
/** The properties for payload.change of a ChangeReport. */
|
|
get change() {
|
|
return [...this.lists.change];
|
|
}
|
|
}
|
|
exports.StateBuilder = StateBuilder;
|