UNPKG

@shgysk8zer0/slack

Version:

An npm package for sending messages in Slack

135 lines (111 loc) 2.68 kB
'use strict'; var interactive = require('./interactive.cjs'); var plainText = require('./plain-text.cjs'); var styles = require('./styles.cjs'); var validation = require('../validation.cjs'); var functions = require('../functions.cjs'); class SlackButtonElement extends interactive.SlackInteractiveElement { #text; #url; #value; #style; // #confirm; #accessibilityLabel; constructor(text, { id, action, url, value, style = styles.SLACK_DEFAULT, accessibilityLabel, // confirm, } = {}) { super({ id, action }); this.text = text; if (typeof accessibilityLabel !== 'undefined') { this.accessibilityLabel = accessibilityLabel; } if (typeof style !== 'undefined') { this.style = style; } if (typeof url !== 'undefined') { this.url = url; } if (typeof value !== 'undefined') { this.value = value; } } get accessibilityLabel() { return this.#accessibilityLabel; } set accessibilityLabel(val) { if (typeof val === 'string' && val.length !== 0) { this.#accessibilityLabel = val; } else { throw new TypeError('accessibilityLabel must be a non-empty string.'); } } get style() { return this.#style; } set style(val) { if (typeof val !== 'string' || val.length === 0) { throw new TypeError('style must be a non-empty string.'); } else if (! SlackButtonElement.STYLES.includes(val)) { throw new Error(`Invalid style: ${val}.`); } else { this.#style = val; } } get text() { return this.#text; } set text(val) { if (val instanceof plainText.SlackPlainTextElement) { this.#text = val; } else if (typeof val === 'string') { this.#text = new plainText.SlackPlainTextElement(val); } else { throw new TypeError('text must be a plain SlackPlainTextElement or a string.'); } } get url() { return this.#url; } set url(val) { if (! validation.isURL(val)) { throw new TypeError(`${val} is not a URL.`); } else { this.#url = val; } } get value() { return this.#value; } set value(val) { if (typeof val === 'string' && val.length !== 0) { this.#value = val; } else { throw new TypeError('Value must be a non-empty string.'); } } toJSON() { return { ...super.toJSON(), text: this.#text, style: this.#style, url: this.#url, value: this.#value, accessibility_label: this.#accessibilityLabel, }; } static get TYPE() { return 'button'; } static get STYLES() { return [styles.SLACK_PRIMARY, styles.SLACK_DANGER]; } } const createSlackButtonElement = functions.createFactory(SlackButtonElement); exports.SlackButtonElement = SlackButtonElement; exports.createSlackButtonElement = createSlackButtonElement;