Skip to content

Commit

Permalink
feat: add response transformation callbacks to .accept and .reject me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
domwebber committed Dec 4, 2023
1 parent 8696d20 commit 12fa51a
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/CloudAPI/CloudAPIWebhook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import CloudAPIWebhookError from "./CloudWebhookAPIError";
import { IncomingMessage, ServerResponse } from "http";
import { createHmac } from "node:crypto";

export type ServerResponseTransform = (
response: ServerResponse,
) => ServerResponse;

export interface WebhookAPIRegisterReturn {
/**
* The verify token sent by WhatsApp.
Expand Down Expand Up @@ -43,7 +47,7 @@ export interface WebhookAPIRegisterReturn {
* // Within your request handler:
* return reg.accept();
*/
accept: () => ServerResponse;
accept: (transform?: ServerResponseTransform) => ServerResponse;

/**
* Reject the Webhook Registration.
Expand All @@ -56,7 +60,7 @@ export interface WebhookAPIRegisterReturn {
* // Within your request handler:
* return reg.reject();
*/
reject: () => ServerResponse;
reject: (transform?: ServerResponseTransform) => ServerResponse;
}

export interface WebhookAPIEventNotificationReturn {
Expand All @@ -72,7 +76,7 @@ export interface WebhookAPIEventNotificationReturn {
* // Within your request handler:
* return event.accept();
*/
accept: () => ServerResponse;
accept: (transform?: ServerResponseTransform) => ServerResponse;

/**
* Reject the Incoming Webhook.
Expand All @@ -85,7 +89,7 @@ export interface WebhookAPIEventNotificationReturn {
* // Within your request handler:
* return event.reject();
*/
reject: () => ServerResponse;
reject: (transform?: ServerResponseTransform) => ServerResponse;

/**
* Check the integrity of the request body.
Expand Down Expand Up @@ -165,11 +169,11 @@ export default class CloudAPIWebhook extends AbstractAPI {
return {
verifyToken: hubVerifyToken,
challenge: hubChallenge,
accept: () => {
return res.end(hubChallenge);
accept: (transform) => {
return (transform?.(res) ?? res).end(hubChallenge);
},
reject: () => {
return res.end();
reject: (transform) => {
return (transform?.(res) ?? res).end();
},
};
}
Expand Down Expand Up @@ -244,11 +248,11 @@ export default class CloudAPIWebhook extends AbstractAPI {
throw CloudAPIWebhookError.mismatchedXHubSignature();
}
},
accept: () => {
return res.end();
accept: (transform) => {
return (transform?.(res) ?? res).end();
},
reject: () => {
return res.end();
reject: (transform) => {
return (transform?.(res) ?? res).end();
},
};
}
Expand Down

0 comments on commit 12fa51a

Please sign in to comment.