Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added version pinning recommendation #314

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading