@smj0x/homebridge-shortcuts-buttons
Version:
Run any Apple Shortcut with just the tap of a button, and execute a custom unix command (or another shortcut!) after completion to handle its success/failure, using x-callback-url.
60 lines • 1.88 kB
JavaScript
import { HSBUtils } from './utils.js';
export var HSBShortcutStatus;
(function (HSBShortcutStatus) {
HSBShortcutStatus["SUCCESS"] = "success";
HSBShortcutStatus["ERROR"] = "error";
HSBShortcutStatus["CANCEL"] = "cancel";
})(HSBShortcutStatus || (HSBShortcutStatus = {}));
export class HSBShortcut {
name;
server;
utils;
input;
constructor(name, server, utils, input) {
this.name = name;
this.server = server;
this.utils = utils;
this.input = input;
this.name = encodeURIComponent(name);
}
async run() {
return this.utils.execAsync(`open -gj ${this.shortcutUrl}`);
}
get isWithXCallbackUrl() {
return this.server !== null && typeof this.input === 'undefined';
}
get isWithTextInput() {
return HSBUtils.isNonEmptyString(this.input);
}
get shortcutUrl() {
let url = 'shortcuts://';
if (this.isWithXCallbackUrl === true) {
url += 'x-callback-url/';
}
url += `run-shortcut\\?name=${this.name}`;
if (this.isWithXCallbackUrl === true) {
url += `\\&${this.callbackXParams}`;
}
if (this.isWithTextInput === true) {
url += `\\&${this.textInputParams}`;
}
return url;
}
get textInputParams() {
return `input=text\\&text=${this.input}`;
}
get callbackXParams() {
return Object.values(HSBShortcutStatus)
.map((status) => this.getCallbackXParam(status))
.join('\\&');
}
getCallbackXParam(status) {
return (
// eslint-disable-next-line no-useless-escape
`x-${status}="${this.server?.baseUrl}\?` +
`shortcut=${this.name}%26` +
`status=${status}%26` +
`token=${this.server?.issueToken()}"`);
}
}
//# sourceMappingURL=shortcut.js.map