fox-slack-block-builder
Version:
Maintainable code for interactive Slack messages, modals, home tabs, and workflow steps. A must-have for the Slack Block Kit framework.
46 lines (45 loc) • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Builder = void 0;
const exception_1 = require("../exception");
class Builder {
constructor(params) {
this.props = params ? { ...params } : {};
Object.keys(this.props)
.forEach((prop) => this.props[prop] === undefined
&& delete this.props[prop]);
Object.seal(this);
}
set(value, prop) {
if (this.props[prop] !== undefined) {
throw new exception_1.BlockBuilderError(`Property ${prop} can only be assigned once.`);
}
if (value !== undefined) {
this.props[prop] = value;
}
return this;
}
append(value, prop) {
const prunedValue = Builder.pruneUndefinedFromArray(value);
if (prunedValue.length > 0) {
this.props[prop] = this.props[prop] === undefined
? prunedValue
: this.props[prop].concat(prunedValue);
}
return this;
}
getResult(Clazz, overrideProps) {
const result = new Clazz({ ...this.props, ...overrideProps });
return Object.freeze(result);
}
/** @internal */
// eslint-disable-next-line max-len
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars, @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
build(params) {
throw new exception_1.BlockBuilderError('Builder must have a declared \'build\' method');
}
static pruneUndefinedFromArray(array) {
return array.filter((value) => (value !== undefined ? value : false));
}
}
exports.Builder = Builder;