Skip to content

Commit

Permalink
feat: improve handleWebhook api
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Smart committed Dec 5, 2022
1 parent b4ba156 commit 52ffb7b
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/Interactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ export {
BadWebhookSignature,
} from "./webhook.js"

export interface HandleWebhookOpts<E> {
headers: Webhook.Headers
body: string
success: (a: Discord.InteractionResponse) => Effect<never, never, void>
error: (e: Cause<E>) => Effect<never, never, void>
empty: Effect<never, never, void>
}

class InteractionBuilder<R, E> {
constructor(readonly definitions: D.InteractionDefinition<R, E>[]) {}

Expand All @@ -40,8 +48,23 @@ class InteractionBuilder<R, E> {
return Gateway.run<R, R2, E, E2>(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() {
Expand Down

0 comments on commit 52ffb7b

Please sign in to comment.