whatsapp-api-js
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -11,3 +11,4 @@ const { Contacts } = require('./types/contacts'); | ||
/** | ||
* @typedef {Object} WhatsAppAPI The main API object | ||
* The main API object | ||
* | ||
* @property {String} token The API token | ||
@@ -14,0 +15,0 @@ * @property {String} v The API version to use |
{ | ||
"name": "whatsapp-api-js", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "A Whatsapp Official API helper for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -69,3 +69,3 @@ /** | ||
// Check if the message is a message | ||
// Check if the message is a message or a status update | ||
if (value.messages) { | ||
@@ -95,3 +95,3 @@ const contact = value.contacts[0]; | ||
} else { | ||
// Return a "404 Not Found" if event is not from a whatsApp API | ||
// Throw "400 Bad Request" if data is not a valid WhatsApp API request | ||
throw 400; | ||
@@ -98,0 +98,0 @@ } |
@@ -98,2 +98,3 @@ const Text = require("./text"); | ||
* @throws {Error} If object is not a Document, Image, Text, or Video | ||
* @throws {Error} If object is a Text and is over 60 characters | ||
*/ | ||
@@ -106,4 +107,8 @@ constructor(object) { | ||
delete object._; | ||
// Text type can go to hell | ||
this[this.type] = this.type === "text" ? object.body : object; | ||
if (this.type === "text") { | ||
if (object.body > 60) throw new Error("Header text must be 60 characters or less"); | ||
this[this.type] = object.body; | ||
} else this[this.type] = object; | ||
} | ||
@@ -129,2 +134,3 @@ } | ||
* @throws {Error} If no sections are provided or are over 10 | ||
* @throws {Error} If more than 1 section is provided and at least one doesn't have a title | ||
*/ | ||
@@ -135,2 +141,3 @@ constructor(button, ...sections) { | ||
if (!sections?.length || sections.length > 10) throw new Error("Action must have between 1 and 10 sections"); | ||
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") }); | ||
@@ -153,14 +160,12 @@ this._ = "list"; | ||
* | ||
* @param {String} title Title of the section | ||
* @param {String} title Title of the section, only required if there are more than one section | ||
* @param {...Row} rows Rows of the section | ||
* @throws {Error} If title is not provided | ||
* @throws {Error} If title is over 24 characters | ||
* @throws {Error} If title is over 24 characters if provided | ||
* @throws {Error} If no rows are provided or are over 10 | ||
*/ | ||
constructor(title, ...rows) { | ||
if (!title) throw new Error("Section must have a title"); | ||
if (title.length > 24) throw new Error("Section title must be 24 characters or less"); | ||
if (title && title.length > 24) throw new Error("Section title must be 24 characters or less"); | ||
if (!rows?.length || rows.length > 10) throw new Error("Section must have between 1 and 10 rows"); | ||
this.title = title; | ||
if (title) this.title = title; | ||
this.rows = rows; | ||
@@ -167,0 +172,0 @@ } |
@@ -122,3 +122,3 @@ /** | ||
* @param {Boolean} isItAnID Whether video is an id (true) or a link (false) | ||
* @param {String} [caption] Describes the specified image media | ||
* @param {String} [caption] Describes the specified video media | ||
*/ | ||
@@ -125,0 +125,0 @@ constructor(video, isItAnID = false, caption) { |
@@ -26,12 +26,5 @@ const Text = require("./text"); | ||
const temp = []; | ||
for (let component of components) { | ||
if (component instanceof ButtonComponent) temp.push(...component.build()); | ||
else temp.push(component); | ||
} | ||
components = temp; | ||
this.name = name; | ||
this.language = language instanceof Language ? language : new Language(language); | ||
if (components) this.components = components; | ||
if (components) this.components = components.map(c => c.build ? c.build() : c).flat();; | ||
@@ -135,3 +128,3 @@ this._ = "template"; | ||
if (!["url", "quick_reply"].includes(sub_type)) throw new Error("ButtonComponent sub_type must be either 'url' or 'quick_reply'"); | ||
if (!parameters?.length) throw new Error("ButtonComponent must have a parameter at least 1 parameter"); | ||
if (!parameters?.length) throw new Error("ButtonComponent must have at least 1 parameter"); | ||
if (parameters.length > 3) throw new Error("ButtonComponent can only have up to 3 parameters"); | ||
@@ -198,3 +191,3 @@ | ||
this.type = "header"; | ||
if (parameters) this.parameters = parameters.map(e => e instanceof Parameter ? e : new Parameter(e)); | ||
if (parameters) this.parameters = parameters.map(e => e instanceof Parameter ? e : new Parameter(e, "header")); | ||
} | ||
@@ -217,3 +210,3 @@ } | ||
this.type = "body"; | ||
if (parameters) this.parameters = parameters.map(e => e instanceof Parameter ? e : new Parameter(e)); | ||
if (parameters) this.parameters = parameters.map(e => e instanceof Parameter ? e : new Parameter(e, "body")); | ||
} | ||
@@ -240,10 +233,18 @@ } | ||
* @param {(Text|Currency|DateTime|Image|Document|Video)} parameter The parameter to be used in the template | ||
* @param {String} whoami The parent component, used to check if a Text object is too long. Can be either 'header' or 'body' | ||
* @throws {Error} If parameter is not provided | ||
* @throws {Error} If parameter is a Text and the parent component (whoami) is "header" and the text over 60 characters | ||
* @throws {Error} If parameter is a Text and the parent component (whoami) is "body" and the text over 1024 characters | ||
*/ | ||
constructor(parameter) { | ||
if (!parameter) throw new Error("Parameter must have a parameter"); | ||
constructor(parameter, whoami) { | ||
if (!parameter) throw new Error("Parameter object must have a parameter parameter"); | ||
this.type = parameter._; | ||
delete parameter._; | ||
// Text type can go to hell | ||
if (this.type === "text") this.text = parameter.body; else this[this.type] = parameter; | ||
if (this.type === "text") { | ||
if (whoami === "header" && object.body > 60) throw new Error("Header text must be 60 characters or less"); | ||
if (whoami === "body" && object.body > 1024) throw new Error("Body text must be 1024 characters or less"); | ||
this[this.type] = object.body; | ||
} else this[this.type] = object; | ||
} | ||
@@ -250,0 +251,0 @@ } |
/** | ||
* Text API object | ||
* | ||
* @property {String} body Body of the message. Maximum length: 1024 characters. | ||
* @property {String} body Body of the message. Maximum length: 4096 characters. | ||
* @property {Boolean} preview_url Whether to enable preview for the text message | ||
@@ -15,7 +15,7 @@ * @property {String} _ The type of the object, for internal use only | ||
* @throws {Error} If body is not provided | ||
* @throws {Error} If body is over 1024 characters | ||
* @throws {Error} If body is over 4096 characters | ||
*/ | ||
constructor(body, preview_url = false) { | ||
if (!body) throw new Error("Text must have a body object"); | ||
if (body.length > 1024) throw new Error("Text body must be less than 1024 characters"); | ||
if (body.length > 4096) throw new Error("Text body must be less than 4096 characters"); | ||
this.body = body; | ||
@@ -22,0 +22,0 @@ if (preview_url) this.preview_url = preview_url; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54407
1166