From 52ffb7b364f7354f60f26d4ad80881091331f863 Mon Sep 17 00:00:00 2001 From: Tim Smart Date: Tue, 6 Dec 2022 09:25:42 +1300 Subject: [PATCH] feat: improve handleWebhook api --- src/Interactions/index.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Interactions/index.ts b/src/Interactions/index.ts index d9415b3..c7f519c 100644 --- a/src/Interactions/index.ts +++ b/src/Interactions/index.ts @@ -21,6 +21,14 @@ export { BadWebhookSignature, } from "./webhook.js" +export interface HandleWebhookOpts { + headers: Webhook.Headers + body: string + success: (a: Discord.InteractionResponse) => Effect + error: (e: Cause) => Effect + empty: Effect +} + class InteractionBuilder { constructor(readonly definitions: D.InteractionDefinition[]) {} @@ -40,8 +48,23 @@ class InteractionBuilder { return Gateway.run(this.definitions, catchAll, opts) } - handleWebhook(headers: Webhook.Headers, rawBody: string) { - return Webhook.run(this.definitions, headers, rawBody) + handleWebhook({ + headers, + body, + success, + empty, + error, + }: HandleWebhookOpts< + E | Webhook.WebhookParseError | Webhook.BadWebhookSignature + >) { + return Webhook.run(this.definitions, headers, body) + .flatMap((o) => + o.match( + () => empty, + (a) => success(a), + ), + ) + .catchAllCause(error) } get syncGlobal() {