Skip to content

Commit 7698ba0

Browse files
authored
Merge pull request #396 from Secreto31126/v5-docs
V5 docs
2 parents 904b12c + 6dbddd7 commit 7698ba0

File tree

3 files changed

+109
-4
lines changed

3 files changed

+109
-4
lines changed

BREAKING.md

+105
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,110 @@
11
# Breaking changes
22

3+
## 5.0.0
4+
5+
### post() and get() errors
6+
7+
The webhook methods no longer throw simple numbers as errors, but rather
8+
custom classes extending the new WhatsAppAPIError class. This change was
9+
made to allow for more detailed error handling, as the new classes contain
10+
the error message, recommended status code and lots of docs to help.
11+
12+
```ts
13+
import { WhatsAppAPIError } from "whatsapp-api-js/errors";
14+
15+
const Whatsapp = new WhatsAppAPI({ token, secure: false });
16+
17+
// Assuming post is called on a POST request to your server
18+
async function post(e) {
19+
try {
20+
await Whatsapp.post(e.data);
21+
return 200;
22+
} catch (e) {
23+
console.error(e);
24+
25+
if (e instanceof WhatsAppAPIError) {
26+
console.log("For more info, check", e.docs);
27+
return e.httpStatus;
28+
}
29+
30+
return 500;
31+
}
32+
}
33+
```
34+
35+
As you might notice, the example above first checks if the error is an
36+
instance of WhatsAppAPIError, and returns 500 if it isn't. That's because...
37+
38+
### post() error handling
39+
40+
The post() method no longer catches errors thrown by the `message` or
41+
`status` emitters. This means that any error within the handlers will be
42+
propagated to the caller, which can then manage it as needed.
43+
44+
```ts
45+
import { WhatsAppAPIError } from "whatsapp-api-js/errors";
46+
47+
const Whatsapp = new WhatsAppAPI({ token, secure: false });
48+
49+
Whatsapp.on.message = () => {
50+
throw new Error("This is an error on my code");
51+
};
52+
53+
async function post(e) {
54+
try {
55+
await Whatsapp.post(e.data);
56+
} catch (e) {
57+
if (e instanceof WhatsAppAPIError) {
58+
console.log("This is a library error");
59+
} else {
60+
console.log("This is my faulty code");
61+
}
62+
}
63+
64+
return 418;
65+
}
66+
```
67+
68+
This change does NOT impact the middlewares, as they still catch
69+
the errors and asserts the return values are the documented ones.
70+
71+
### ActionProduct signature change
72+
73+
The ActionProduct class no longer takes the catalog ID and product as
74+
two separate arguments, but rather as a single CatalogProduct object.
75+
76+
```ts
77+
import {
78+
Interactive,
79+
ActionProduct,
80+
CatalogProduct
81+
} from "whatsapp-api-js/messages";
82+
83+
const interactive_single_product_message = new Interactive(
84+
new ActionProduct(new CatalogProduct("product_id", "catalog_id"))
85+
);
86+
```
87+
88+
### Drop support for CJS
89+
90+
Nine out of ten releases, there was a compatibility issue between ESM and CJS
91+
exports. Although the library was designed to support both, the complexity
92+
it brought didn't justify the effort, specially now as many other big libraries
93+
are dropping native CJS support too.
94+
95+
If you 100% need CJS in Node.js, you can now use the release candidate feature
96+
to synchonously require ESM modules available since v22. The library is fully
97+
synchonous (contains no top-level await), so it should work just fine.
98+
99+
https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require
100+
101+
In order to keep the library easier to use with CJS, the code will still not
102+
use default exports.
103+
104+
### Bumped API version
105+
106+
The default API version was bumped to `v21.0`.
107+
3108
## 4.0.0
4109

5110
### Emitters and post() signature change

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# whatsapp-api-js v4
1+
# whatsapp-api-js v5
22

33
[![npm](https://img.shields.io/npm/v/whatsapp-api-js?color=4ccc1c)](https://www.npmjs.com/package/whatsapp-api-js)
44
[![Contributors](https://img.shields.io/github/all-contributors/Secreto31126/whatsapp-api-js)](#contributors)
@@ -7,7 +7,7 @@ A TypeScript server agnostic Whatsapp's Official API framework.
77

88
## List of contents
99

10-
- [whatsapp-api-js v4](#whatsapp-api-js-v4)
10+
- [whatsapp-api-js v5](#whatsapp-api-js-v5)
1111
- [List of contents](#list-of-contents)
1212
- [Set up](#set-up)
1313
- [Examples and Tutorials](#examples-and-tutorials)

SECURITY.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
| Version | Supported |
66
| ------- | ------------------ |
7-
| 4 | :white_check_mark: |
8-
| < 4 | :x: |
7+
| 5 | :white_check_mark: |
8+
| < 5 | :x: |
99

1010
## Reporting a Vulnerability
1111

0 commit comments

Comments
 (0)