whatsapp-api-js
Advanced tools
Comparing version 0.5.0 to 0.6.1-beta.0
52
index.js
@@ -18,2 +18,3 @@ // Most of these imports are here only for types checks | ||
* @property {String} v The API version to use | ||
* @property {Boolean} parsed If truthy, API operations will return the fetch promise instead. Intended for low level debugging. | ||
*/ | ||
@@ -26,8 +27,10 @@ class WhatsAppAPI { | ||
* @param {String} v The version of the API, defaults to v14.0 | ||
* @param {Boolean} parsed Whether to return a pre-processed response from the API or the raw fetch response. Intended for low level debugging. | ||
* @throws {Error} If token is not specified | ||
*/ | ||
constructor(token, v = "v14.0") { | ||
constructor(token, v = "v14.0", parsed = true) { | ||
if (!token) throw new Error("Token must be specified"); | ||
this.token = token; | ||
this.v = v; | ||
this.parsed = !!parsed; | ||
} | ||
@@ -42,3 +45,5 @@ | ||
* @param {(Text|Audio|Document|Image|Sticker|Video|Location|Contacts|Interactive|Template)} object The message object | ||
* @param {Request} raw The raw body sent to the server | ||
* @param {Request} request The object sent to the server | ||
* @param {(String|Void)} id The message id, undefined if parsed is set to false | ||
* @param {(Object|Void)} response The parsed response from the server, undefined if parsed is set to false | ||
*/ | ||
@@ -66,3 +71,3 @@ | ||
* @param {String} [context] The message ID of the message to reply to | ||
* @returns {Promise} The fetch promise | ||
* @returns {Promise} The server response | ||
* @throws {Error} If phoneID is not specified | ||
@@ -78,4 +83,16 @@ * @throws {Error} If to is not specified | ||
const { request, promise } = api.sendMessage(this.token, this.v, phoneID, to, object, context); | ||
if (this._register) this._register(phoneID, request.to, JSON.parse(request[request.type]), request); | ||
return promise; | ||
const response = this.parsed ? promise.then(e => e.json()) : undefined; | ||
if (this._register) { | ||
if (response) { | ||
response.then(data => { | ||
this._register(phoneID, request.to, JSON.parse(request[request.type]), request, data?.messages?.at()?.id, data); | ||
}); | ||
} else { | ||
this._register(phoneID, request.to, JSON.parse(request[request.type]), request); | ||
} | ||
} | ||
return response ?? promise; | ||
} | ||
@@ -88,3 +105,3 @@ | ||
* @param {String} messageId The message ID | ||
* @returns {Promise} The fetch promise | ||
* @returns {Promise} The server response | ||
* @throws {Error} If phoneID is not specified | ||
@@ -96,3 +113,4 @@ * @throws {Error} If messageId is not specified | ||
if (!messageId) throw new Error("To must be specified"); | ||
return api.readMessage(this.token, this.v, phoneID, messageId); | ||
const promise = api.readMessage(this.token, this.v, phoneID, messageId); | ||
return this.parsed ? promise.then(e => e.json()) : promise; | ||
} | ||
@@ -106,3 +124,3 @@ | ||
* @param {String} format The format of the QR code (png or svn) | ||
* @returns {Promise} The fetch promise | ||
* @returns {Promise} The server response | ||
* @throws {Error} If phoneID is not specified | ||
@@ -116,3 +134,4 @@ * @throws {Error} If message is not specified | ||
if (!["png", "svg"].includes(format)) throw new Error("Format must be either 'png' or 'svg'"); | ||
return api.makeQR(this.token, this.v, phoneID, message, format); | ||
const promise = api.makeQR(this.token, this.v, phoneID, message, format); | ||
return this.parsed ? promise.then(e => e.json()) : promise; | ||
} | ||
@@ -125,3 +144,3 @@ | ||
* @param {String} [id] The QR's id to find. If not specified, all QRs will be returned | ||
* @returns {Promise} The fetch promise | ||
* @returns {Promise} The server response | ||
* @throws {Error} If phoneID is not specified | ||
@@ -131,3 +150,4 @@ */ | ||
if (!phoneID) throw new Error("Phone ID must be specified"); | ||
return api.getQR(this.token, this.v, phoneID, id); | ||
const promise = api.getQR(this.token, this.v, phoneID, id); | ||
return this.parsed ? promise.then(e => e.json()) : promise; | ||
} | ||
@@ -141,3 +161,3 @@ | ||
* @param {String} message The new quick message for the QR code | ||
* @returns {Promise} The fetch promise | ||
* @returns {Promise} The server response | ||
* @throws {Error} If phoneID is not specified | ||
@@ -151,3 +171,4 @@ * @throws {Error} If id is not specified | ||
if (!message) throw new Error("Message must be specified"); | ||
return api.updateQR(this.token, this.v, phoneID, id, message); | ||
const promise = api.updateQR(this.token, this.v, phoneID, id, message); | ||
return this.parsed ? promise.then(e => e.json()) : promise; | ||
} | ||
@@ -160,3 +181,3 @@ | ||
* @param {String} id The QR's id to delete | ||
* @returns {Promise} The fetch promise | ||
* @returns {Promise} The server response | ||
* @throws {Error} If phoneID is not specified | ||
@@ -168,3 +189,4 @@ * @throws {Error} If id is not specified | ||
if (!id) throw new Error("ID must be specified"); | ||
return api.deleteQR(this.token, this.v, phoneID, id); | ||
const promise = api.deleteQR(this.token, this.v, phoneID, id); | ||
return this.parsed ? promise.then(e => e.json()) : promise; | ||
} | ||
@@ -171,0 +193,0 @@ } |
{ | ||
"name": "whatsapp-api-js", | ||
"version": "0.5.0", | ||
"version": "0.6.1-beta.0", | ||
"author": "Secreto31126", | ||
@@ -9,5 +9,5 @@ "description": "A Whatsapp Official API helper for Node.js", | ||
"scripts": { | ||
"test": "mocha ./tests/", | ||
"coverage": "nyc --reporter=text mocha ./tests/ --reporter min", | ||
"debug-with-tests": "nodemon -x npm run test", | ||
"test": "mocha", | ||
"coverage": "nyc --reporter=text mocha --reporter min", | ||
"debug-with-tests": "mocha --reporter min --watch", | ||
"document": "jsdoc -d ./docs -t ./node_modules/docdash -R ./README.md . ./types" | ||
@@ -14,0 +14,0 @@ }, |
@@ -65,3 +65,3 @@ # whatsapp-api-js | ||
if (promise) promise.then(res => res.json()).then(console.log); | ||
if (promise) promise.then(console.log); | ||
@@ -100,3 +100,3 @@ Whatsapp.markAsRead(phoneID, message.id); | ||
This will allow the same script to be run in many different enviroments, such as a web browser, Deno, | ||
and maybe even TypeScript, idk about this last one ¯\\\_(ツ)\_/¯. | ||
Bun, and maybe even TypeScript, idk about this last one ¯\\\_(ツ)\_/¯. | ||
@@ -120,2 +120,15 @@ Personal suggestion, use [esm.sh](https://esm.sh/) to import the code directly from npm, works flawlessly with Deno. | ||
## Breaking changes in 0.6.0 | ||
Since 0.6.0, the module will no longer return the raw fetch request, now it's internally parsed and returned. | ||
This change was made in order to improve the logSentMessages function, as it can now log the server response too. | ||
To get the raw request as before, you can use the `parsed` property of the main object as follows. | ||
```js | ||
const parsed = false; | ||
const Whatsapp = new WhatsAppAPI("YOUR_TOKEN", undefined, parsed); | ||
// All the API operations, like sendMessage, will now return the raw request. | ||
// Keep in mind, now when using logSentMessage the id and response parameters will be undefined. | ||
``` | ||
## Documentation | ||
@@ -122,0 +135,0 @@ |
100928
2221
147
17