Types, disabling of templates and example update

This commit is contained in:
purpshell
2022-08-23 20:43:15 +03:00
parent c32348d3cc
commit dfb862614c
4 changed files with 58 additions and 33 deletions

View File

@@ -7,8 +7,8 @@ const MessageMedia = require('./MessageMedia');
* @typedef {Object} ButtonSpec
* @property {string} body - The text to show on the button.
* @property {string=} id - Custom ID to set on the button. A random one will be generated if one is not passed.
* @property {string=} url - Custom URL to set on the button. Optional and will change the type of the button
* @property {string=} number - Custom URL to set on the button. Optional and will change the type of the button
* @ property {string=} url - Custom URL to set on the button. Optional and will change the type of the button
* @ property {string=} number - Custom URL to set on the button. Optional and will change the type of the button
*/
/**
@@ -78,21 +78,23 @@ class Buttons {
return buttons.map((button, index) => {
if (button.url && button.number && button.id) throw 'Only pick one of the following (url/number/id)';
if (button.number) {
return {
throw 'number buttons are not supported yet';
/* return {
index,
callButton: {
displayText: button.body,
phoneNumber: button.number || ''
}
};
}; */
} else if (button.url) {
return {
throw 'URL buttons are not supported yet';
/* return {
index,
urlButton: {
displayText: button.body,
url: button.url || ''
}
};
}; */
} else {
return {
index,

View File

@@ -2,16 +2,38 @@
const Util = require('../util/Util');
/**
* Section spec used in List constructor
* @typedef {Object} SectionSpec
* @property {string=} title - The title of the section, can be empty on the first section only.
* @property {RowSpec[]} rows - The rows of the section.
*/
/**
* Row spec used in List constructor
* @typedef {Object} RowSpec
* @property {string} title - The text to show on the row.
* @property {string=} id - Custom ID to set on the row. A random one will be generated if one is not passed.
* @property {string=} description - Custom description for the row, will appear after clicked in the list response message (appended)
*/
/**
* Formatted section spec
* @typedef {Object} FormattedSectionSpec
* @property {string} title
* @property {{rowId: string; title: string; description: string}[]} rows
*/
/**
* Message type List
*/
class List {
/**
* @param {string} body
* @param {string} buttonText
* @param {Array<any>} sections
* @param {string?} title
* @param {string?} footer
* @param {string} body - A text body, no media.
* @param {string} buttonText - The text to put on the click to open button.
* @param {Array<SectionSpec>} sections - The sections of the list
* @param {string?} title - Custom boldfaced title property
* @param {string?} footer - Custom footer added in a small font to the end of the message
*/
constructor(body, buttonText, sections, title, footer) {
/**
@@ -49,23 +71,28 @@ class List {
/**
* Creates section array from simple array
* @param {Array<any>} sections
* @returns {Array<any>}
* @example
* Input: [{title:'sectionTitle',rows:[{id:'customId', title:'ListItem2', description: 'desc'},{title:'ListItem2'}]}}]
* Returns: [{'title':'sectionTitle','rows':[{'rowId':'customId','title':'ListItem1','description':'desc'},{'rowId':'oGSRoD','title':'ListItem2','description':''}]}]
* @param {Array<SectionSpec>} sections
* @returns {Array<FormattedSectionSpec>}
*/
_format(sections){
if(!sections.length){throw '[LT02] List without sections';}
if(sections.length > 1 && sections.filter(s => typeof s.title == 'undefined').length > 1){throw '[LT05] You can\'t have more than one empty title.';}
return sections.map( (section) =>{
if(!section.rows.length){throw '[LT03] Section without rows';}
_format(sections) {
if(!sections.length) {
throw '[LT02] List without sections';
}
if(sections.length > 1 && sections.filter(section => (typeof section.title == 'undefined' )|| section.title == '' ).length > 1) {
throw '[LT05] You can\'t have more than one empty title.';
}
return sections.map((section, index) => {
if(!section.rows.length) {
throw '[LT03] Section without rows';
}
return {
title: section.title ? section.title : undefined,
rows: section.rows.map( (row) => {
if(!row.title){throw '[LT04] Row without title';}
rows: section.rows.map((row, rowIndex) => {
if (!row.title) {
throw `[LT04] Row without title at section index ${index} and row index ${rowIndex}`;
}
return {
rowId: row.id ? row.id : Util.generateHash(6),
rowId: row.id ? row.id : Util.generateHash(8),
title: row.title,
description: row.description ? row.description : ''
};