All files / src/lib/app/handlers dispatchWebhookEvent.ts

100% Statements 9/9
100% Branches 8/8
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                                                                                                              8x   1x           1x           1x           1x           1x           1x   1x   1x      
import type {
  ChannelFollowEvent,
  ChatMessageEvent,
  KicksGiftedEvent,
  LivestreamStatusUpdatedEvent,
  NewSubscriptionEvent,
  SubscriptionGiftEvent,
  SubscriptionRenewalEvent,
} from "kick-api-types/payloads";
import type { KickWebhookValidationResult } from "../../functions/validateWebhook";
import type { DbClient } from "../../prisma";
import type { WebhookContext } from "../types";
 
/**
 * Typed collection of handlers invoked for each supported Kick webhook event.
 */
export interface WebhookEventHandlers {
  followEvent: (
    event: ChannelFollowEvent,
    db: DbClient,
    ctx: WebhookContext
  ) => Promise<Response>;
  livestreamStatusUpdate: (
    event: LivestreamStatusUpdatedEvent,
    db: DbClient,
    ctx: WebhookContext
  ) => Promise<Response>;
  newSubscriber: (
    event: NewSubscriptionEvent,
    db: DbClient,
    ctx: WebhookContext
  ) => Promise<Response>;
  giftedSubs: (
    event: SubscriptionGiftEvent,
    db: DbClient,
    ctx: WebhookContext
  ) => Promise<Response>;
  renewedSub: (
    event: SubscriptionRenewalEvent,
    db: DbClient,
    ctx: WebhookContext
  ) => Promise<Response>;
  kicksGifted: (
    event: KicksGiftedEvent,
    db: DbClient,
    ctx: WebhookContext
  ) => Promise<Response>;
  chatHandler: (
    event: ChatMessageEvent,
    db: DbClient,
    ctx: WebhookContext
  ) => Promise<Response>;
}
 
/**
 * Dispatches a validated webhook payload to the appropriate event handler.
 */
export async function dispatchWebhookEvent(
  result: KickWebhookValidationResult,
  db: DbClient,
  ctx: WebhookContext,
  handlers: WebhookEventHandlers
): Promise<Response> {
  switch (result.eventType) {
    case "channel.followed":
      return handlers.followEvent(
        result.payload as ChannelFollowEvent,
        db,
        ctx
      );
    case "livestream.status.updated":
      return handlers.livestreamStatusUpdate(
        result.payload as LivestreamStatusUpdatedEvent,
        db,
        ctx
      );
    case "channel.subscription.new":
      return handlers.newSubscriber(
        result.payload as NewSubscriptionEvent,
        db,
        ctx
      );
    case "channel.subscription.gifts":
      return handlers.giftedSubs(
        result.payload as SubscriptionGiftEvent,
        db,
        ctx
      );
    case "channel.subscription.renewal":
      return handlers.renewedSub(
        result.payload as SubscriptionRenewalEvent,
        db,
        ctx
      );
    case "kicks.gifted":
      return handlers.kicksGifted(result.payload as KicksGiftedEvent, db, ctx);
    case "chat.message.sent":
      return handlers.chatHandler(result.payload as ChatMessageEvent, db, ctx);
    default:
      return ctx.json({ message: "Unknown event type" }, 422);
  }
}