|
1 | 1 | # Breaking changes
|
2 | 2 |
|
| 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 | + |
3 | 108 | ## 4.0.0
|
4 | 109 |
|
5 | 110 | ### Emitters and post() signature change
|
|
0 commit comments