Skip to content

Commit

Permalink
Merge pull request #314 from Secreto31126/v-warn
Browse files Browse the repository at this point in the history
Added version pinning recommendation
  • Loading branch information
Secreto31126 authored Feb 29, 2024
2 parents 2a21eca + 0f3a7c5 commit c1104f9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
} from "./emitters";

import { escapeUnicode } from "./utils.js";
import { DEFAULT_API_VERSION } from "./types.js";

/**
* The main API Class
Expand Down Expand Up @@ -112,7 +113,7 @@ export class WhatsAppAPI {
token,
appSecret,
webhookVerifyToken,
v = "v19.0",
v,
parsed = true,
offload_functions = true,
secure = true,
Expand Down Expand Up @@ -156,7 +157,13 @@ export class WhatsAppAPI {
// Let's hope the user is using a valid ponyfill
this.fetch = ponyfill.fetch || fetch;

this.v = v;
if (v) this.v = v;
else {
console.warn(
`[whatsapp-api-js] Cloud API version not defined. In production, it's strongly recommended pinning it to the desired version with the "v" argument. Defaulting to "${DEFAULT_API_VERSION}".`
);
this.v = DEFAULT_API_VERSION;
}

this.parsed = !!parsed;
this.offload_functions = !!offload_functions;
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import type { AtLeastOne } from "./utils";

export const DEFAULT_API_VERSION = "v19.0";

/**
* The main constructor arguments for the API
*/
Expand Down Expand Up @@ -60,7 +62,7 @@ export type TheBasicConstructorArguments = {
*/
webhookVerifyToken?: string;
/**
* The version of the API, defaults to v19.0
* The version of the API, defaults to {@link DEFAULT_API_VERSION}.
*/
v?: string;
/**
Expand Down
27 changes: 23 additions & 4 deletions test/index.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { spy: sinon_spy, assert: sinon_assert } = require("sinon");

// Import the module
const { WhatsAppAPI } = require("../lib/cjs/middleware/node-http");
const { DEFAULT_API_VERSION } = require("../lib/cjs/types");
const { Text } = require("../lib/cjs/messages/text");

// Mock the https requests
Expand All @@ -27,13 +28,15 @@ const { subtle } = require("node:crypto").webcrypto; // Assert availability in n
setGlobalDispatcher(agent);

describe("WhatsAppAPI", function () {
const v = "v13.0";
const token = "YOUR_ACCESS_TOKEN";
const appSecret = "YOUR_APP_SECRET";
const webhookVerifyToken = "YOUR_WEBHOOK_VERIFY_TOKEN";

describe("Token", function () {
it("should create a WhatsAppAPI object with the token", function () {
const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
ponyfill: {
Expand All @@ -48,6 +51,7 @@ describe("WhatsAppAPI", function () {
describe("App secret", function () {
it("should create a WhatsAppAPI object with the appSecret", function () {
const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
ponyfill: {
Expand All @@ -62,6 +66,7 @@ describe("WhatsAppAPI", function () {
describe("Webhook verify token", function () {
it("should work with any specified webhook verify token", function () {
const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
webhookVerifyToken,
Expand All @@ -75,7 +80,7 @@ describe("WhatsAppAPI", function () {
});

describe("Version", function () {
it("should work with v19.0 as default", function () {
it("should work with DEFAULT_API_VERSION as default (and log warning)", function () {
const Whatsapp = new WhatsAppAPI({
token,
appSecret,
Expand All @@ -84,20 +89,20 @@ describe("WhatsAppAPI", function () {
subtle
}
});
equal(Whatsapp.v, "v19.0");
equal(Whatsapp.v, DEFAULT_API_VERSION);
});

it("should work with any specified version", function () {
const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
v: "v13.0",
ponyfill: {
fetch: undici_fetch,
subtle
}
});
equal(Whatsapp.v, "v13.0");
equal(Whatsapp.v, v);
});
});

Expand All @@ -109,6 +114,7 @@ describe("WhatsAppAPI", function () {
}

const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
ponyfill: {
Expand All @@ -122,6 +128,7 @@ describe("WhatsAppAPI", function () {
it("should work with any specified ponyfill", function () {
const spy = sinon_spy();
const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
ponyfill: {
Expand All @@ -145,6 +152,7 @@ describe("WhatsAppAPI", function () {
}

const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
ponyfill: {
Expand All @@ -158,6 +166,7 @@ describe("WhatsAppAPI", function () {
it("should work with any specified ponyfill", function () {
const spy = subtle;
const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
ponyfill: {
Expand All @@ -174,6 +183,7 @@ describe("WhatsAppAPI", function () {
describe("Parsed", function () {
it("should set parsed to true by default", function () {
const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
ponyfill: {
Expand All @@ -186,6 +196,7 @@ describe("WhatsAppAPI", function () {

it("should be able to set parsed to true", function () {
const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
parsed: true,
Expand All @@ -199,6 +210,7 @@ describe("WhatsAppAPI", function () {

it("should be able to set parsed to false", function () {
const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
parsed: false,
Expand Down Expand Up @@ -245,6 +257,7 @@ describe("WhatsAppAPI", function () {
let spy_on_sent;
this.beforeEach(function () {
Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
ponyfill: {
Expand Down Expand Up @@ -428,6 +441,7 @@ describe("WhatsAppAPI", function () {
};

const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
ponyfill: {
Expand Down Expand Up @@ -658,6 +672,7 @@ describe("WhatsAppAPI", function () {
const code = "something_random";

const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
ponyfill: {
Expand Down Expand Up @@ -1000,6 +1015,7 @@ describe("WhatsAppAPI", function () {
const id = "2";

const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
ponyfill: {
Expand Down Expand Up @@ -1465,6 +1481,7 @@ describe("WhatsAppAPI", function () {
};

const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
webhookVerifyToken,
Expand Down Expand Up @@ -1568,6 +1585,7 @@ describe("WhatsAppAPI", function () {
);

const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
ponyfill: {
Expand Down Expand Up @@ -1922,6 +1940,7 @@ describe("WhatsAppAPI", function () {

describe("_authenicatedRequest", function () {
const Whatsapp = new WhatsAppAPI({
v,
token,
appSecret,
ponyfill: {
Expand Down

0 comments on commit c1104f9

Please sign in to comment.