@shgysk8zer0/slack
Version:
An npm package for sending messages in Slack
84 lines (67 loc) • 1.84 kB
JavaScript
;
var block = require('./block.cjs');
var interactive = require('../element/interactive.cjs');
var text = require('../element/text.cjs');
var plainText = require('../element/plain-text.cjs');
var functions = require('../functions.cjs');
class SlackSectionBlock extends block.SlackBlock {
#text;
#fields = [];
#accessory;
constructor(text, { id, accessory, fields } = {}) {
super({ id });
if (typeof text !== 'undefined') {
this.text = text;
}
if (typeof accessory !== 'undefined') {
this.accessory = accessory;
}
if (Array.isArray(fields) && fields.length !== 0) {
this.add(...fields);
}
}
get accessory() {
return this.#accessory;
}
set accessory(val) {
if (val instanceof interactive.SlackInteractiveElement) {
this.#accessory = val;
} else {
throw new TypeError('Accessory must be a SlackInteractiveElement.');
}
}
get text() {
return this.#text;
}
set text(val) {
if (val instanceof text.SlackTextElement) {
this.#text = val;
} else if (typeof val === 'string') {
this.#text = new plainText.SlackPlainTextElement(val);
} else {
throw new TypeError('text must be a plain SlackTextElement or a string.');
}
}
add(...fields) {
const before = this.#fields.length;
const count = this.#fields.push(...fields.filter(field => field instanceof text.SlackTextElement));
if (count - before !== fields.length) {
throw new Error('Error adding some fields.');
}
return this;
}
toJSON() {
return {
...super.toJSON(),
text: this.#text,
accessory: this.#accessory,
fields: this.#fields,
};
}
static get TYPE() {
return 'section';
}
}
const createSlackSectionBlock = functions.createFactory(SlackSectionBlock);
exports.SlackSectionBlock = SlackSectionBlock;
exports.createSlackSectionBlock = createSlackSectionBlock;