slack-rest-api-types
Version:
TypeScript types and utilities for Slack Web API REST endpoints
230 lines (229 loc) • 5.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isBlock = isBlock;
exports.isKnownBlock = isKnownBlock;
exports.isSectionBlock = isSectionBlock;
exports.isActionsBlock = isActionsBlock;
exports.isDividerBlock = isDividerBlock;
exports.isHeaderBlock = isHeaderBlock;
exports.isImageBlock = isImageBlock;
exports.isContextBlock = isContextBlock;
exports.isInputBlock = isInputBlock;
exports.validateBlocks = validateBlocks;
exports.filterKnownBlocks = filterKnownBlocks;
/**
* Type guard to check if a value is a valid Slack Block.
*
* @param value - The value to check
* @returns True if the value is a Block
*
* @example
* ```typescript
* const maybeBlock = { type: 'section', text: { type: 'mrkdwn', text: 'Hello' } };
*
* if (isBlock(maybeBlock)) {
* console.log('Valid block:', maybeBlock.type);
* }
* ```
*/
function isBlock(value) {
return (typeof value === 'object' &&
value !== null &&
'type' in value &&
typeof value.type === 'string');
}
/**
* Type guard to check if a value is a KnownBlock (one of the official Slack block types).
*
* @param value - The value to check
* @returns True if the value is a KnownBlock
*
* @example
* ```typescript
* const block = { type: 'section', text: { type: 'mrkdwn', text: 'Hello' } };
*
* if (isKnownBlock(block)) {
* // TypeScript knows this is a known block type
* console.log('Known block type:', block.type);
* }
* ```
*/
function isKnownBlock(value) {
if (!isBlock(value))
return false;
const knownBlockTypes = [
'actions',
'context',
'divider',
'file',
'header',
'image',
'input',
'rich_text',
'section',
'video',
];
return knownBlockTypes.includes(value.type);
}
/**
* Type guard to check if a block is a section block.
*
* @param block - The block to check
* @returns True if the block is a section block
*
* @example
* ```typescript
* const block = { type: 'section', text: { type: 'mrkdwn', text: 'Hello' } };
*
* if (isSectionBlock(block)) {
* console.log('Section text:', block.text?.text);
* }
* ```
*/
function isSectionBlock(block) {
return block.type === 'section';
}
/**
* Type guard to check if a block is an actions block.
*
* @param block - The block to check
* @returns True if the block is an actions block
*
* @example
* ```typescript
* const block = { type: 'actions', elements: [...] };
*
* if (isActionsBlock(block)) {
* console.log('Number of actions:', block.elements.length);
* }
* ```
*/
function isActionsBlock(block) {
return block.type === 'actions';
}
/**
* Type guard to check if a block is a divider block.
*
* @param block - The block to check
* @returns True if the block is a divider block
*
* @example
* ```typescript
* const block = { type: 'divider' };
*
* if (isDividerBlock(block)) {
* console.log('This is a divider');
* }
* ```
*/
function isDividerBlock(block) {
return block.type === 'divider';
}
/**
* Type guard to check if a block is a header block.
*
* @param block - The block to check
* @returns True if the block is a header block
*
* @example
* ```typescript
* const block = { type: 'header', text: { type: 'plain_text', text: 'Title' } };
*
* if (isHeaderBlock(block)) {
* console.log('Header text:', block.text.text);
* }
* ```
*/
function isHeaderBlock(block) {
return block.type === 'header';
}
/**
* Type guard to check if a block is an image block.
*
* @param block - The block to check
* @returns True if the block is an image block
*
* @example
* ```typescript
* const block = { type: 'image', image_url: 'https://...', alt_text: 'Image' };
*
* if (isImageBlock(block)) {
* console.log('Image URL:', block.image_url);
* }
* ```
*/
function isImageBlock(block) {
return block.type === 'image';
}
/**
* Type guard to check if a block is a context block.
*
* @param block - The block to check
* @returns True if the block is a context block
*
* @example
* ```typescript
* const block = { type: 'context', elements: [...] };
*
* if (isContextBlock(block)) {
* console.log('Context elements:', block.elements.length);
* }
* ```
*/
function isContextBlock(block) {
return block.type === 'context';
}
/**
* Type guard to check if a block is an input block.
*
* @param block - The block to check
* @returns True if the block is an input block
*
* @example
* ```typescript
* const block = { type: 'input', label: {...}, element: {...} };
*
* if (isInputBlock(block)) {
* console.log('Input label:', block.label.text);
* }
* ```
*/
function isInputBlock(block) {
return block.type === 'input';
}
/**
* Validates that an array contains only valid blocks.
*
* @param blocks - Array of potential blocks
* @returns True if all items in the array are valid blocks
*
* @example
* ```typescript
* const blocks = [
* { type: 'section', text: { type: 'mrkdwn', text: 'Hello' } },
* { type: 'divider' }
* ];
*
* if (validateBlocks(blocks)) {
* // Safe to use as blocks
* }
* ```
*/
function validateBlocks(blocks) {
return blocks.every((block) => isBlock(block));
}
/**
* Filters an array to only include known block types.
*
* @param blocks - Array of blocks to filter
* @returns Array containing only known block types
*
* @example
* ```typescript
* const blocks = [...];
* const knownBlocks = filterKnownBlocks(blocks);
* ```
*/
function filterKnownBlocks(blocks) {
return blocks.filter(isKnownBlock);
}