Skip to content

Commit

Permalink
feat: introduce CloudAPI.createVerifyToken()
Browse files Browse the repository at this point in the history
  • Loading branch information
domwebber committed Dec 16, 2023
1 parent dc74271 commit 90ac8fd
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/CloudAPI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,33 @@
import AbstractAPI, { AbstractAPIParams } from "../API/AbstractAPI";
import CloudAPIMessage from "./CloudAPIMessage";
import CloudAPIWebhook from "./CloudAPIWebhook";
import { randomBytes } from "crypto";

export interface WhatsAppAPICreateVerifyTokenParams {
/**
* The length of the verify token.
*
* @since 5.1.0
* @default 16
*/
length?: number;

/**
* The encoding of the verify token.
*
* @since 5.1.0
* @default hex
*/
encoding?: BufferEncoding;

/**
* Random Bytes Generation.
*
* @since 5.1.0
* @default crypto.randomBytes
*/
random?: (length: number) => Buffer;
}

export interface WhatsAppAPIParams extends AbstractAPIParams {}

Expand All @@ -22,6 +49,20 @@ export interface WhatsAppAPIParams extends AbstractAPIParams {}
* const sdk = new CloudWhatsAppAPI("123456")
*/
export default class CloudAPI extends AbstractAPI {
/**
* Default Verify Token Length.
*
* @since 5.6.0
*/
public static DEFAULT_VERIFY_TOKEN_LENGTH = 16;

/**
* Default Verify Token Encoding.
*
* @since 5.6.0
*/
public static DEFAULT_VERIFY_TOKEN_ENCODING: BufferEncoding = "hex";

/**
* Message API.
*
Expand All @@ -36,6 +77,7 @@ export default class CloudAPI extends AbstractAPI {

/**
* Webhook API.
* Receive and handle messages from WhatsApp via WebHook.
*
* @since 4.0.0
*/
Expand All @@ -46,4 +88,20 @@ export default class CloudAPI extends AbstractAPI {
this.message = new CloudAPIMessage(params);
this.webhook = new CloudAPIWebhook(params);
}

/**
* Create a new Verify Token.
* This is a random string that is used to verify that the request is coming
* from WhatsApp. This method **only** creates the value, it's usage is up to
* the implementer.
*
* @since 5.6.0
*/
public static createVerifyToken({
length = this.DEFAULT_VERIFY_TOKEN_LENGTH,
encoding = this.DEFAULT_VERIFY_TOKEN_ENCODING,
random = randomBytes,
}: WhatsAppAPICreateVerifyTokenParams): string {
return random(length).toString(encoding);
}
}

0 comments on commit 90ac8fd

Please sign in to comment.