whatsapp-api-js
Advanced tools
Comparing version 5.0.0-beta.0 to 5.0.0-beta.1
@@ -1,4 +0,4 @@ | ||
import type { ClientMessage, ClientMessageRequest, ServerMessage, ServerMessageResponse, ServerConversation, ServerPricing, ServerError, PostData } from "./types"; | ||
import type { WhatsAppAPI } from "./index"; | ||
import type { MaybePromise } from "./utils"; | ||
import type { ClientMessage, ClientMessageRequest, ServerMessage, ServerMessageResponse, ServerConversation, ServerPricing, ServerError, PostData } from "./types.d.ts"; | ||
import type { WhatsAppAPI } from "./index.d.ts"; | ||
import type { MaybePromise } from "./utils.d.ts"; | ||
/** | ||
@@ -5,0 +5,0 @@ * Callback for "sent" event |
@@ -117,3 +117,4 @@ /** | ||
* However, it isn't available in all environments. If your environment doesn't support it, | ||
* you can provide a ponyfill for it in the `crypto` option of the `WhatsAppAPI` constructor. | ||
* you can provide a ponyfill for it in the `ponyfill.subtle` option of the `WhatsAppAPI` | ||
* constructor. | ||
* | ||
@@ -120,0 +121,0 @@ * @example |
/** @module WhatsAppAPI */ | ||
import { ClientMessage, type WhatsAppAPIConstructorArguments, type PostData, type GetParams, type ServerMessageResponse, type ServerMarkAsReadResponse, type ServerCreateQRResponse, type ServerRetrieveQRResponse, type ServerUpdateQRResponse, type ServerDeleteQRResponse, type ServerMediaRetrieveResponse, type ServerMediaUploadResponse, type ServerMediaDeleteResponse } from "./types.js"; | ||
import type { OnMessage, OnSent, OnStatus } from "./emitters"; | ||
import type { OnMessage, OnSent, OnStatus } from "./emitters.d.ts"; | ||
/** | ||
* The main API Class | ||
* | ||
* @template EmittersReturnType - The return type of the emitters | ||
* ({@link OnMessage}, {@link OnStatus}) | ||
*/ | ||
@@ -84,2 +87,5 @@ export declare class WhatsAppAPI<EmittersReturnType = void> { | ||
* | ||
* @template EmittersReturnType - The return type of the emitters | ||
* ({@link OnMessage}, {@link OnStatus}) | ||
* | ||
* @throws If fetch is not defined in the enviroment and the provided ponyfill isn't a function | ||
@@ -339,2 +345,4 @@ * @throws If secure is true, crypto.subtle is not defined in the enviroment and the provided ponyfill isn't an object | ||
* | ||
* raw_body and signature are required when secure is `true` on initialization (default). | ||
* | ||
* @example | ||
@@ -386,15 +394,33 @@ * ```ts | ||
* @param raw_body - The raw body of the POST request | ||
* @param signature - The x-hub-signature-256 (all lowercase) header signature sent by Whatsapp | ||
* @param signature - The x-hub-signature-256 header signature sent by Whatsapp | ||
* @returns The emitter's return value, undefined if the corresponding emitter isn't set | ||
* @throws WhatsAppAPIMissingRawBodyError if secure and the raw body is missing | ||
* @throws WhatsAppAPIMissingSignatureError if secure and the signature is missing | ||
* @throws WhatsAppAPIMissingAppSecretError if secure and the appSecret isn't defined | ||
* @throws WhatsAppAPIMissingCryptoSubtleError if secure and crypto.subtle or ponyfill isn't available | ||
* @throws WhatsAppAPIFailedToVerifyError if secure and the signature doesn't match the hash | ||
* @throws WhatsAppAPIUnexpectedError if the POSTed data is not a valid Whatsapp API request | ||
* @throws Class {@link WhatsAppAPIMissingRawBodyError} if the raw body is missing | ||
* @throws Class {@link WhatsAppAPIMissingSignatureError} if the signature is missing | ||
* @throws Class {@link WhatsAppAPIMissingAppSecretError} if the appSecret isn't defined | ||
* @throws Class {@link WhatsAppAPIMissingCryptoSubtleError} if crypto.subtle or ponyfill isn't available | ||
* @throws Class {@link WhatsAppAPIFailedToVerifyError} if the signature doesn't match the hash | ||
* @throws Class {@link WhatsAppAPIUnexpectedError} if the POSTed data is not a valid Whatsapp API request | ||
* @throws Any error generated within the user's callbacks | ||
* @throws WhatsAppAPIUnexpectedError if the POSTed data is valid but not a message or status update (ignored) | ||
* @throws Class {@link WhatsAppAPIUnexpectedError} if the POSTed data is valid but not a message or status update (ignored) | ||
*/ | ||
post(data: PostData, raw_body?: string, signature?: string): Promise<EmittersReturnType | undefined>; | ||
post(data: PostData, raw_body: string, signature: string): Promise<EmittersReturnType | undefined>; | ||
/** | ||
* POST helper, must be called inside the post function of your code. | ||
* When setting up the webhook, only subscribe to messages. Other subscritions support might be added later. | ||
* | ||
* raw_body and signature are NOT required when secure is `false` on initialization. | ||
* | ||
* @deprecated The method isn't deprecated, but it's strongly discouraged to use | ||
* the server without signature verification. It's a security risk. | ||
* | ||
* Provide an `appSecret` and set `secure` to true on {@link WhatsAppAPI} initialization. | ||
* | ||
* @param data - The POSTed data object sent by Whatsapp | ||
* @returns The emitter's return value, undefined if the corresponding emitter isn't set | ||
* @throws Class {@link WhatsAppAPIUnexpectedError} if the POSTed data is not a valid Whatsapp API request | ||
* @throws Any error generated within the user's callbacks | ||
* @throws Class {@link WhatsAppAPIUnexpectedError} if the POSTed data is valid but not a message or status update (ignored) | ||
*/ | ||
post(data: PostData): Promise<EmittersReturnType | undefined>; | ||
/** | ||
* GET helper, must be called inside the get function of your code. | ||
@@ -438,5 +464,5 @@ * Used once at the first webhook setup. | ||
* @returns The challenge string, it must be the http response body | ||
* @throws WhatsAppAPIMissingVerifyTokenError if webhookVerifyToken is not specified | ||
* @throws WhatsAppAPIMissingSearchParamsError if the request is missing data | ||
* @throws WhatsAppAPIFailedToVerifyTokenError if the verification tokens don't match | ||
* @throws Class {@link WhatsAppAPIMissingVerifyTokenError} if webhookVerifyToken is not specified | ||
* @throws Class {@link WhatsAppAPIMissingSearchParamsError} if the request is missing data | ||
* @throws Class {@link WhatsAppAPIFailedToVerifyTokenError} if the verification tokens don't match | ||
*/ | ||
@@ -462,4 +488,4 @@ get(params: GetParams): string; | ||
* @returns If the signature is valid | ||
* @throws WhatsAppAPIMissingAppSecretError if the appSecret isn't defined | ||
* @throws WhatsAppAPIMissingCryptoSubtleError if crypto.subtle or ponyfill isn't available | ||
* @throws Class {@link WhatsAppAPIMissingAppSecretError} if the appSecret isn't defined | ||
* @throws Class {@link WhatsAppAPIMissingCryptoSubtleError} if crypto.subtle or ponyfill isn't available | ||
*/ | ||
@@ -466,0 +492,0 @@ verifyRequestSignature(raw_body: string, signature: string): Promise<boolean>; |
@@ -92,2 +92,5 @@ import { | ||
* | ||
* @template EmittersReturnType - The return type of the emitters | ||
* ({@link OnMessage}, {@link OnStatus}) | ||
* | ||
* @throws If fetch is not defined in the enviroment and the provided ponyfill isn't a function | ||
@@ -506,65 +509,2 @@ * @throws If secure is true, crypto.subtle is not defined in the enviroment and the provided ponyfill isn't an object | ||
} | ||
// #endregion | ||
// #region Webhooks | ||
/** | ||
* POST helper, must be called inside the post function of your code. | ||
* When setting up the webhook, only subscribe to messages. Other subscritions support might be added later. | ||
* | ||
* @example | ||
* ```ts | ||
* // author arivanbastos on issue #114 | ||
* // Simple http example implementation with Whatsapp.post() on Node@^19 | ||
* import { WhatsAppAPI } from "whatsapp-api-js"; | ||
* import { WhatsAppAPIError } from "whatsapp-api-js/errors"; | ||
* import { NodeNext } from "whatsapp-api-js/setup/node"; | ||
* | ||
* import { createServer } from "http"; | ||
* | ||
* const token = "token"; | ||
* const appSecret = "appSecret"; | ||
* const Whatsapp = new WhatsAppAPI<number>(NodeNext({ token, appSecret })); | ||
* | ||
* function handler(req, res) { | ||
* if (req.method == "POST") { | ||
* const chunks = []; | ||
* req.on("data", (chunk) => chunks.push(chunk)); | ||
* | ||
* req.on("end", async () => { | ||
* const body = Buffer.concat(chunks).toString(); | ||
* | ||
* try { | ||
* const response = await Whatsapp.post(JSON.parse(body), body, req.headers["x-hub-signature-256"]); | ||
* res.writeHead(response); | ||
* } catch (err) { | ||
* res.writeHead(err instanceof WhatsAppAPIError ? err.httpStatus : 500); | ||
* } | ||
* | ||
* res.end(); | ||
* }); | ||
* } else res.writeHead(501).end(); | ||
* }; | ||
* | ||
* Whatsapp.on.message = ({ phoneID, from, message, name, reply, offload }) => { | ||
* console.log(`User ${name} (${from}) sent to bot ${phoneID} a(n) ${message.type}`); | ||
* offload(() => reply(new Text("Hello!"))); | ||
* return 202; | ||
* }; | ||
* | ||
* const server = createServer(handler); | ||
* server.listen(3000); | ||
* ``` | ||
* | ||
* @param data - The POSTed data object sent by Whatsapp | ||
* @param raw_body - The raw body of the POST request | ||
* @param signature - The x-hub-signature-256 (all lowercase) header signature sent by Whatsapp | ||
* @returns The emitter's return value, undefined if the corresponding emitter isn't set | ||
* @throws WhatsAppAPIMissingRawBodyError if secure and the raw body is missing | ||
* @throws WhatsAppAPIMissingSignatureError if secure and the signature is missing | ||
* @throws WhatsAppAPIMissingAppSecretError if secure and the appSecret isn't defined | ||
* @throws WhatsAppAPIMissingCryptoSubtleError if secure and crypto.subtle or ponyfill isn't available | ||
* @throws WhatsAppAPIFailedToVerifyError if secure and the signature doesn't match the hash | ||
* @throws WhatsAppAPIUnexpectedError if the POSTed data is not a valid Whatsapp API request | ||
* @throws Any error generated within the user's callbacks | ||
* @throws WhatsAppAPIUnexpectedError if the POSTed data is valid but not a message or status update (ignored) | ||
*/ | ||
async post(data, raw_body, signature) { | ||
@@ -672,5 +612,5 @@ if (this.secure) { | ||
* @returns The challenge string, it must be the http response body | ||
* @throws WhatsAppAPIMissingVerifyTokenError if webhookVerifyToken is not specified | ||
* @throws WhatsAppAPIMissingSearchParamsError if the request is missing data | ||
* @throws WhatsAppAPIFailedToVerifyTokenError if the verification tokens don't match | ||
* @throws Class {@link WhatsAppAPIMissingVerifyTokenError} if webhookVerifyToken is not specified | ||
* @throws Class {@link WhatsAppAPIMissingSearchParamsError} if the request is missing data | ||
* @throws Class {@link WhatsAppAPIFailedToVerifyTokenError} if the verification tokens don't match | ||
*/ | ||
@@ -721,4 +661,4 @@ get(params) { | ||
* @returns If the signature is valid | ||
* @throws WhatsAppAPIMissingAppSecretError if the appSecret isn't defined | ||
* @throws WhatsAppAPIMissingCryptoSubtleError if crypto.subtle or ponyfill isn't available | ||
* @throws Class {@link WhatsAppAPIMissingAppSecretError} if the appSecret isn't defined | ||
* @throws Class {@link WhatsAppAPIMissingCryptoSubtleError} if crypto.subtle or ponyfill isn't available | ||
*/ | ||
@@ -725,0 +665,0 @@ async verifyRequestSignature(raw_body, signature) { |
import { ClientMessage, type ContactComponent, ContactUniqueComponent, ContactMultipleComponent } from "../types.js"; | ||
import type { AtLeastOne } from "../utils"; | ||
import type { AtLeastOne } from "../utils.d.ts"; | ||
/** | ||
@@ -4,0 +4,0 @@ * @group Contacts |
import { Section, type ClientTypedMessageComponent } from "../types.js"; | ||
import type { AtLeastOne } from "../utils"; | ||
import type { AtLeastOne } from "../utils.d.ts"; | ||
/** | ||
@@ -4,0 +4,0 @@ * TS knowledge intensifies |
import { ClientMessage, ClientLimitedMessageComponent, Section, type InteractiveAction } from "../types.js"; | ||
import type { AtLeastOne } from "../utils"; | ||
import type { Document, Image, Video } from "./media"; | ||
import type { AtLeastOne } from "../utils.d.ts"; | ||
import type { Document, Image, Video } from "./media.d.ts"; | ||
import { CatalogProduct, Product, ProductSection } from "./globals.js"; | ||
@@ -5,0 +5,0 @@ /** |
import { ClientMessage, ClientLimitedMessageComponent, type ClientBuildableMessageComponent, type ClientTypedMessageComponent, type TemplateComponent } from "../types.js"; | ||
import type { AtLeastOne, XOR } from "../utils"; | ||
import type { Location } from "./location"; | ||
import type { Document, Image, Video } from "./media"; | ||
import type { AtLeastOne, XOR } from "../utils.d.ts"; | ||
import type { Location } from "./location.d.ts"; | ||
import type { Document, Image, Video } from "./media.d.ts"; | ||
import { CatalogProduct, Product, ProductSection } from "./globals.js"; | ||
@@ -6,0 +6,0 @@ /** |
@@ -42,3 +42,3 @@ import { WhatsAppAPIMiddleware } from "./globals.js"; | ||
req.body, | ||
req.header("x-hub-signature-256") | ||
req.header("x-hub-signature-256") ?? "" | ||
); | ||
@@ -45,0 +45,0 @@ return 200; |
@@ -1,2 +0,2 @@ | ||
import type { WhatsAppAPIConstructorArguments } from "../types"; | ||
import type { WhatsAppAPIConstructorArguments } from "../types.d.ts"; | ||
/** | ||
@@ -3,0 +3,0 @@ * A Bun quick setup for the WhatsAppAPI |
@@ -1,2 +0,2 @@ | ||
import type { WhatsAppAPIConstructorArguments } from "../types"; | ||
import type { WhatsAppAPIConstructorArguments } from "../types.d.ts"; | ||
/** | ||
@@ -3,0 +3,0 @@ * A Deno quick setup for the WhatsAppAPI |
@@ -1,2 +0,2 @@ | ||
import type { WhatsAppAPIConstructorArguments } from "../types"; | ||
import type { WhatsAppAPIConstructorArguments } from "../types.d.ts"; | ||
/** | ||
@@ -3,0 +3,0 @@ * A Node\@^19 quick setup for the WhatsAppAPI |
@@ -1,2 +0,2 @@ | ||
import type { WhatsAppAPIConstructorArguments } from "../types"; | ||
import type { WhatsAppAPIConstructorArguments } from "../types.d.ts"; | ||
/** | ||
@@ -3,0 +3,0 @@ * A Web quick setup for the WhatsAppAPI |
@@ -9,4 +9,4 @@ /** | ||
*/ | ||
import type { Text, Audio, Document, Image, Sticker, Video, Location, Interactive, Template, Reaction, Contacts } from "./messages"; | ||
import type { AtLeastOne } from "./utils"; | ||
import type { Text, Audio, Document, Image, Sticker, Video, Location, Interactive, Template, Reaction, Contacts } from "./messages/index.d.ts"; | ||
import type { AtLeastOne } from "./utils.d.ts"; | ||
export declare const DEFAULT_API_VERSION = "v21.0"; | ||
@@ -13,0 +13,0 @@ /** |
{ | ||
"name": "whatsapp-api-js", | ||
"version": "5.0.0-beta.0", | ||
"version": "5.0.0-beta.1", | ||
"author": "Secreto31126", | ||
@@ -5,0 +5,0 @@ "description": "A TypeScript server agnostic Whatsapp's Official API framework", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
528692
2
8189