Skip to content

Commit 8205037

Browse files
authored
Merge pull request #13 from Secreto31126:error-improvements
Thrown errors improvements
2 parents aa6b25a + 9d9c938 commit 8205037

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "whatsapp-api-js",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "A Whatsapp Official API helper for Node.js",
55
"main": "index.js",
66
"scripts": {

types/interactive.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,20 @@ class Header {
9595
* @param {(Document|Image|Text|Video)} object The message object for the header
9696
* @throws {Error} If object is not provided
9797
* @throws {Error} If object is not a Document, Image, Text, or Video
98+
* @throws {Error} If object is a Text and is over 60 characters
9899
*/
99100
constructor(object) {
100101
if (!object) throw new Error("Header must have an object");
101102
if (!["text", "video", "image", "document"].includes(object._)) throw new Error(`Header object must be either Text, Video, Image or Document.`);
102103

103104
this.type = object._;
104105
delete object._;
106+
105107
// Text type can go to hell
106-
this[this.type] = this.type === "text" ? object.body : object;
108+
if (this.type === "text") {
109+
if (object.body > 60) throw new Error("Header text must be 60 characters or less");
110+
this[this.type] = object.body;
111+
} else this[this.type] = object;
107112
}
108113
}
109114

@@ -124,11 +129,13 @@ class ActionList {
124129
* @throws {Error} If button is not provided
125130
* @throws {Error} If button is over 20 characters
126131
* @throws {Error} If no sections are provided or are over 10
132+
* @throws {Error} If more than 1 section is provided and at least one doesn't have a title
127133
*/
128134
constructor(button, ...sections) {
129135
if (!button) throw new Error("Action must have a button content");
130136
if (button.length > 20) throw new Error("Button content must be 20 characters or less");
131137
if (!sections?.length || sections.length > 10) throw new Error("Action must have between 1 and 10 sections");
138+
if (sections.length > 1) sections.forEach(s => { if (!s.title) throw new Error("Sections must have a title if more than 1 section is provided") });
132139

133140
this._ = "list";
134141
this.button = button;
@@ -146,18 +153,16 @@ class Section {
146153
/**
147154
* Builds a section component for ActionList
148155
*
149-
* @param {String} title Title of the section
156+
* @param {String} title Title of the section, only required if there are more than one section
150157
* @param {...Row} rows Rows of the section
151-
* @throws {Error} If title is not provided
152-
* @throws {Error} If title is over 24 characters
158+
* @throws {Error} If title is over 24 characters if provided
153159
* @throws {Error} If no rows are provided or are over 10
154160
*/
155161
constructor(title, ...rows) {
156-
if (!title) throw new Error("Section must have a title");
157-
if (title.length > 24) throw new Error("Section title must be 24 characters or less");
162+
if (title && title.length > 24) throw new Error("Section title must be 24 characters or less");
158163
if (!rows?.length || rows.length > 10) throw new Error("Section must have between 1 and 10 rows");
159164

160-
this.title = title;
165+
if (title) this.title = title;
161166
this.rows = rows;
162167
}
163168
}

types/template.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,9 @@ class Template {
2323
if (!name) throw new Error("Template must have a name");
2424
if (!language) throw new Error("Template must have a language");
2525

26-
const temp = [];
27-
for (let component of components) {
28-
if (component instanceof ButtonComponent) temp.push(...component.build());
29-
else temp.push(component);
30-
}
31-
components = temp;
32-
3326
this.name = name;
3427
this.language = language instanceof Language ? language : new Language(language);
35-
if (components) this.components = components;
28+
if (components) this.components = components.map(c => c.build ? c.build() : c).flat();;
3629

3730
this._ = "template";
3831
}
@@ -131,7 +124,7 @@ class ButtonComponent {
131124
*/
132125
constructor(sub_type, ...parameters) {
133126
if (!["url", "quick_reply"].includes(sub_type)) throw new Error("ButtonComponent sub_type must be either 'url' or 'quick_reply'");
134-
if (!parameters?.length) throw new Error("ButtonComponent must have a parameter at least 1 parameter");
127+
if (!parameters?.length) throw new Error("ButtonComponent must have at least 1 parameter");
135128
if (parameters.length > 3) throw new Error("ButtonComponent can only have up to 3 parameters");
136129

137130
const buttonType = sub_type === "url" ? "text" : "payload";
@@ -193,7 +186,7 @@ class HeaderComponent {
193186
*/
194187
constructor(...parameters) {
195188
this.type = "header";
196-
if (parameters) this.parameters = parameters.map(e => e instanceof Parameter ? e : new Parameter(e));
189+
if (parameters) this.parameters = parameters.map(e => e instanceof Parameter ? e : new Parameter(e, "header"));
197190
}
198191
}
199192

@@ -211,7 +204,7 @@ class BodyComponent {
211204
*/
212205
constructor(...parameters) {
213206
this.type = "body";
214-
if (parameters) this.parameters = parameters.map(e => e instanceof Parameter ? e : new Parameter(e));
207+
if (parameters) this.parameters = parameters.map(e => e instanceof Parameter ? e : new Parameter(e, "body"));
215208
}
216209
}
217210

@@ -233,14 +226,22 @@ class Parameter {
233226
* For Document parameter, only PDF documents are supported for document-based message templates.
234227
*
235228
* @param {(Text|Currency|DateTime|Image|Document|Video)} parameter The parameter to be used in the template
229+
* @param {String} whoami The parent component, used to check if a Text object is too long. Can be either 'header' or 'body'
236230
* @throws {Error} If parameter is not provided
231+
* @throws {Error} If parameter is a Text and the parent component (whoami) is "header" and the text over 60 characters
232+
* @throws {Error} If parameter is a Text and the parent component (whoami) is "body" and the text over 1024 characters
237233
*/
238-
constructor(parameter) {
239-
if (!parameter) throw new Error("Parameter must have a parameter");
234+
constructor(parameter, whoami) {
235+
if (!parameter) throw new Error("Parameter object must have a parameter parameter");
240236
this.type = parameter._;
241237
delete parameter._;
238+
242239
// Text type can go to hell
243-
if (this.type === "text") this.text = parameter.body; else this[this.type] = parameter;
240+
if (this.type === "text") {
241+
if (whoami === "header" && object.body > 60) throw new Error("Header text must be 60 characters or less");
242+
if (whoami === "body" && object.body > 1024) throw new Error("Body text must be 1024 characters or less");
243+
this[this.type] = object.body;
244+
} else this[this.type] = object;
244245
}
245246
}
246247

types/text.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Text API object
33
*
4-
* @property {String} body Body of the message. Maximum length: 1024 characters.
4+
* @property {String} body Body of the message. Maximum length: 4096 characters.
55
* @property {Boolean} preview_url Whether to enable preview for the text message
66
* @property {String} _ The type of the object, for internal use only
77
*/
@@ -12,11 +12,11 @@
1212
* @param {String} body The text of the text message which can contain formatting and URLs which begin with http:// or https://
1313
* @param {Boolean} preview_url By default, WhatsApp recognizes URLs and makes them clickable, but you can also include a preview box with more information about the link. Set this field to true if you want to include a URL preview box. Defaults to false.
1414
* @throws {Error} If body is not provided
15-
* @throws {Error} If body is over 1024 characters
15+
* @throws {Error} If body is over 4096 characters
1616
*/
1717
constructor(body, preview_url = false) {
1818
if (!body) throw new Error("Text must have a body object");
19-
if (body.length > 1024) throw new Error("Text body must be less than 1024 characters");
19+
if (body.length > 4096) throw new Error("Text body must be less than 4096 characters");
2020
this.body = body;
2121
if (preview_url) this.preview_url = preview_url;
2222
this._ = "text";

0 commit comments

Comments
 (0)