import { DeclarationError } from "../registry/types.js"; /** * A semantics action mapping of 1.x: the phrases "open", "close", "raise", "lower" for one directive of the * capability it is added to. * * toggle.addActionMapping(new ActionMapping([AlexaActions.Close], "TurnOff")); * lift.addActionMapping(new ActionMapping([AlexaActions.Open], "SetRangeValue", { rangeValue: 100 })); */ export class ActionMapping { /** * directivePayload is the payload object of the directive (alexa-discovery-objects.html, "ActionMappings object"). * 1.x took a string and put it into discovery as one. A string that holds a JSON object is parsed; any other * string throws a DeclarationError. */ constructor(actions, directiveName, directivePayload) { this.type = "ActionsToDirective"; this.actions = actions; this.directive = { name: directiveName }; if (typeof directivePayload === "string") { if (directivePayload === "") return; this.directive.payload = objectIn(directivePayload, directiveName); this.deprecation = `the payload of the action mapping for ${directiveName} is a JSON string, pass the object`; } else if (directivePayload) { this.directive.payload = directivePayload; } } toJSON() { return { "@type": this.type, actions: this.actions, directive: this.directive }; } } function objectIn(text, directiveName) { let value; try { value = JSON.parse(text); } catch { value = undefined; } if (typeof value !== "object" || value === null || Array.isArray(value)) { throw new DeclarationError({}, `the payload of the action mapping for ${directiveName} is an object, got the string ${JSON.stringify(text)}`); } return value; }