@overlaysymphony/twitch 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/package.json +2 -2
  2. package/src/authentication/authentication.ts +1 -1
  3. package/src/authentication/index.ts +1 -1
  4. package/src/chat/chat.ts +4 -4
  5. package/src/chat/helpers.ts +1 -1
  6. package/src/chat/index.ts +4 -3
  7. package/src/chat/interfaces/index.ts +2 -2
  8. package/src/chat/parser.ts +1 -1
  9. package/src/eventsub/events/automod.message.hold-2.ts +81 -0
  10. package/src/eventsub/events/automod.message.update-2.ts +94 -0
  11. package/src/eventsub/events/automod.settings.update-1.ts +65 -0
  12. package/src/eventsub/events/automod.terms.update-1.ts +59 -0
  13. package/src/eventsub/events/channel.ad_break.begin-1.ts +50 -0
  14. package/src/eventsub/events/channel.ban-1.ts +58 -0
  15. package/src/eventsub/events/channel.bits.use-1.ts +112 -0
  16. package/src/eventsub/events/channel.channel_points_automatic_reward_redemption.add-2.ts +88 -0
  17. package/src/eventsub/events/channel.channel_points_custom_reward.add-1.ts +101 -0
  18. package/src/eventsub/events/channel.channel_points_custom_reward.remove-1.ts +103 -0
  19. package/src/eventsub/events/channel.channel_points_custom_reward.update-1.ts +103 -0
  20. package/src/eventsub/events/channel.channel_points_custom_reward_redemption.add-1.ts +65 -0
  21. package/src/eventsub/events/channel.channel_points_custom_reward_redemption.update-1.ts +65 -0
  22. package/src/eventsub/events/channel.charity_campaign.donate-1.ts +69 -0
  23. package/src/eventsub/events/channel.charity_campaign.progress-1.ts +74 -0
  24. package/src/eventsub/events/channel.charity_campaign.start-1.ts +76 -0
  25. package/src/eventsub/events/channel.charity_campaign.stop-1.ts +76 -0
  26. package/src/eventsub/events/channel.chat.clear-1.ts +41 -0
  27. package/src/eventsub/events/channel.chat.clear_user_messages-1.ts +47 -0
  28. package/src/eventsub/events/channel.chat.message-1.ts +160 -0
  29. package/src/eventsub/events/channel.chat.message_delete-1.ts +49 -0
  30. package/src/eventsub/events/channel.chat.notification-1.ts +317 -0
  31. package/src/eventsub/events/channel.chat.user_message_hold-1.ts +75 -0
  32. package/src/eventsub/events/channel.chat.user_message_update-1.ts +82 -0
  33. package/src/eventsub/events/channel.chat_settings.update-1.ts +83 -0
  34. package/src/eventsub/events/channel.cheer-1.ts +50 -0
  35. package/src/eventsub/events/channel.follow-2.ts +49 -0
  36. package/src/eventsub/events/channel.goal.begin-1.ts +80 -0
  37. package/src/eventsub/events/channel.goal.end-1.ts +80 -0
  38. package/src/eventsub/events/channel.goal.progress-1.ts +80 -0
  39. package/src/eventsub/events/channel.guest_star_guest.update-beta.ts +85 -0
  40. package/src/eventsub/events/channel.guest_star_session.begin-beta.ts +50 -0
  41. package/src/eventsub/events/channel.guest_star_session.end-beta.ts +58 -0
  42. package/src/eventsub/events/channel.guest_star_settings.update-beta.ts +62 -0
  43. package/src/eventsub/events/channel.hype_train.begin-2.ts +90 -0
  44. package/src/eventsub/events/channel.hype_train.end-2.ts +70 -0
  45. package/src/eventsub/events/channel.hype_train.progress-2.ts +90 -0
  46. package/src/eventsub/events/channel.moderate-2.ts +258 -0
  47. package/src/eventsub/events/channel.moderator.add-1.ts +44 -0
  48. package/src/eventsub/events/channel.moderator.remove-1.ts +44 -0
  49. package/src/eventsub/events/channel.poll.begin-1.ts +73 -0
  50. package/src/eventsub/events/channel.poll.end-1.ts +75 -0
  51. package/src/eventsub/events/channel.poll.progress-1.ts +73 -0
  52. package/src/eventsub/events/channel.prediction.begin-1.ts +72 -0
  53. package/src/eventsub/events/channel.prediction.end-1.ts +76 -0
  54. package/src/eventsub/events/channel.prediction.lock-1.ts +72 -0
  55. package/src/eventsub/events/channel.prediction.progress-1.ts +72 -0
  56. package/src/eventsub/events/channel.raid-1.ts +48 -0
  57. package/src/eventsub/events/channel.shared_chat.begin-1.ts +55 -0
  58. package/src/eventsub/events/channel.shared_chat.end-1.ts +46 -0
  59. package/src/eventsub/events/channel.shared_chat.update-1.ts +55 -0
  60. package/src/eventsub/events/channel.shield_mode.begin-1.ts +51 -0
  61. package/src/eventsub/events/channel.shield_mode.end-1.ts +51 -0
  62. package/src/eventsub/events/channel.shoutout.create-1.ts +61 -0
  63. package/src/eventsub/events/channel.shoutout.receive-1.ts +51 -0
  64. package/src/eventsub/events/channel.subscribe-1.ts +48 -0
  65. package/src/eventsub/events/channel.subscription.end-1.ts +48 -0
  66. package/src/eventsub/events/channel.subscription.gift-1.ts +52 -0
  67. package/src/eventsub/events/channel.subscription.message-1.ts +66 -0
  68. package/src/eventsub/events/channel.suspicious_user.message-1.ts +85 -0
  69. package/src/eventsub/events/channel.suspicious_user.update-1.ts +55 -0
  70. package/src/eventsub/events/channel.unban-1.ts +50 -0
  71. package/src/eventsub/events/channel.unban_request.create-1.ts +53 -0
  72. package/src/eventsub/events/channel.unban_request.resolve-1.ts +64 -0
  73. package/src/eventsub/events/channel.update-2.ts +48 -0
  74. package/src/eventsub/events/channel.vip.add-1.ts +44 -0
  75. package/src/eventsub/events/channel.vip.remove-1.ts +44 -0
  76. package/src/eventsub/events/channel.warning.acknowledge-1.ts +47 -0
  77. package/src/eventsub/events/channel.warning.send-1.ts +57 -0
  78. package/src/eventsub/events/index.ts +73 -284
  79. package/src/eventsub/events/stream.offline-1.ts +38 -0
  80. package/src/eventsub/events/stream.online-1.ts +44 -0
  81. package/src/eventsub/events/user.update-1.ts +48 -0
  82. package/src/eventsub/events/user.whisper.message-1.ts +51 -0
  83. package/src/eventsub/events-helpers.ts +53 -25
  84. package/src/eventsub/eventsub.ts +37 -74
  85. package/src/eventsub/index.ts +8 -6
  86. package/src/eventsub/messages.ts +16 -4
  87. package/src/helix/channel-points/custom-rewards.ts +3 -3
  88. package/src/helix/channel-points/index.ts +1 -1
  89. package/src/helix/helix.ts +1 -1
  90. package/src/helix/subscriptions/index.ts +1 -1
  91. package/src/helix/subscriptions/subscriptions.ts +40 -47
  92. package/src/helix/users/index.ts +1 -1
  93. package/src/helix/users/users.ts +3 -3
  94. package/src/helpers/alerts/alerts.ts +5 -5
  95. package/src/helpers/alerts/index.ts +2 -2
  96. package/src/helpers/charity/charity.ts +2 -2
  97. package/src/helpers/charity/index.ts +1 -1
  98. package/src/helpers/goal/goal.ts +2 -2
  99. package/src/helpers/goal/index.ts +1 -1
  100. package/src/helpers/hype-train/hype-train.ts +2 -2
  101. package/src/helpers/hype-train/index.ts +1 -1
  102. package/src/helpers/poll/index.ts +1 -1
  103. package/src/helpers/poll/poll.ts +3 -3
  104. package/src/helpers/prediction/index.ts +1 -1
  105. package/src/helpers/prediction/prediction.ts +3 -3
  106. package/src/helpers/redemption/index.ts +1 -1
  107. package/src/helpers/redemption/redemption.ts +3 -3
  108. package/src/helpers/status/index.ts +1 -1
  109. package/src/helpers/status/status.ts +2 -2
  110. package/src/ui/authentication.ts +1 -1
  111. package/src/ui/popup.ts +26 -3
  112. package/src/eventsub/events/channel.ad_break.begin.ts +0 -51
  113. package/src/eventsub/events/channel.ban.ts +0 -59
  114. package/src/eventsub/events/channel.channel_points_custom_reward._.ts +0 -29
  115. package/src/eventsub/events/channel.channel_points_custom_reward.add.ts +0 -75
  116. package/src/eventsub/events/channel.channel_points_custom_reward.remove.ts +0 -77
  117. package/src/eventsub/events/channel.channel_points_custom_reward.update.ts +0 -77
  118. package/src/eventsub/events/channel.channel_points_custom_reward_redemption._.ts +0 -10
  119. package/src/eventsub/events/channel.channel_points_custom_reward_redemption.add.ts +0 -59
  120. package/src/eventsub/events/channel.channel_points_custom_reward_redemption.update.ts +0 -59
  121. package/src/eventsub/events/channel.charity_campaign.donate.ts +0 -66
  122. package/src/eventsub/events/channel.charity_campaign.progress.ts +0 -67
  123. package/src/eventsub/events/channel.charity_campaign.start.ts +0 -69
  124. package/src/eventsub/events/channel.charity_campaign.stop.ts +0 -69
  125. package/src/eventsub/events/channel.chat.clear.ts +0 -42
  126. package/src/eventsub/events/channel.chat.clear_user_messages.ts +0 -48
  127. package/src/eventsub/events/channel.chat.message_delete.ts +0 -50
  128. package/src/eventsub/events/channel.chat.notification.ts +0 -50
  129. package/src/eventsub/events/channel.cheer.ts +0 -51
  130. package/src/eventsub/events/channel.follow.ts +0 -50
  131. package/src/eventsub/events/channel.goal.begin.ts +0 -51
  132. package/src/eventsub/events/channel.goal.end.ts +0 -55
  133. package/src/eventsub/events/channel.goal.progress.ts +0 -51
  134. package/src/eventsub/events/channel.guest_star_guest.update.ts +0 -66
  135. package/src/eventsub/events/channel.guest_star_session.begin.ts +0 -46
  136. package/src/eventsub/events/channel.guest_star_session.end.ts +0 -48
  137. package/src/eventsub/events/channel.guest_star_settings.update.ts +0 -50
  138. package/src/eventsub/events/channel.hype_train._.ts +0 -12
  139. package/src/eventsub/events/channel.hype_train.begin.ts +0 -57
  140. package/src/eventsub/events/channel.hype_train.end.ts +0 -55
  141. package/src/eventsub/events/channel.hype_train.progress.ts +0 -70
  142. package/src/eventsub/events/channel.moderator.add.ts +0 -45
  143. package/src/eventsub/events/channel.moderator.remove.ts +0 -45
  144. package/src/eventsub/events/channel.poll._.ts +0 -26
  145. package/src/eventsub/events/channel.poll.begin.ts +0 -55
  146. package/src/eventsub/events/channel.poll.end.ts +0 -57
  147. package/src/eventsub/events/channel.poll.progress.ts +0 -55
  148. package/src/eventsub/events/channel.prediction._.ts +0 -25
  149. package/src/eventsub/events/channel.prediction.begin.ts +0 -51
  150. package/src/eventsub/events/channel.prediction.end.ts +0 -55
  151. package/src/eventsub/events/channel.prediction.lock.ts +0 -51
  152. package/src/eventsub/events/channel.prediction.progress.ts +0 -51
  153. package/src/eventsub/events/channel.raid.ts +0 -49
  154. package/src/eventsub/events/channel.shield_mode.begin.ts +0 -50
  155. package/src/eventsub/events/channel.shield_mode.end.ts +0 -50
  156. package/src/eventsub/events/channel.shoutout.create.ts +0 -62
  157. package/src/eventsub/events/channel.shoutout.receive.ts +0 -52
  158. package/src/eventsub/events/channel.subscribe.ts +0 -49
  159. package/src/eventsub/events/channel.subscription.end.ts +0 -49
  160. package/src/eventsub/events/channel.subscription.gift.ts +0 -53
  161. package/src/eventsub/events/channel.subscription.message.ts +0 -67
  162. package/src/eventsub/events/channel.unban.ts +0 -51
  163. package/src/eventsub/events/channel.update.ts +0 -49
  164. package/src/eventsub/events/stream.offline.ts +0 -39
  165. package/src/eventsub/events/stream.online.ts +0 -45
  166. package/src/eventsub/events/user.update.ts +0 -45
@@ -0,0 +1,51 @@
1
+ import { type EventConfig, registerEvent } from "../events-helpers.ts"
2
+
3
+ declare module "../events-helpers.ts" {
4
+ interface EventConfigs {
5
+ "user.whisper.message": UserWhisperMessage
6
+ }
7
+ }
8
+
9
+ /** User Whisper Message v1: When a user receives a whisper. */
10
+ type UserWhisperMessage = EventConfig<{
11
+ Type: "user.whisper.message"
12
+ Version: "1"
13
+ /** The conditions to listen for when a user receives a whisper. */
14
+ Condition: {
15
+ /** The user_id of the person receiving whispers. */
16
+ user_id: string
17
+ }
18
+ /** The event fired when a user receives a whisper. */
19
+ Event: {
20
+ /** The ID of the user sending the message. */
21
+ from_user_id: string
22
+ /** The name of the user sending the message. */
23
+ from_user_name: string
24
+ /** The login of the user sending the message. */
25
+ from_user_login: string
26
+ /** The ID of the user receiving the message. */
27
+ to_user_id: string
28
+ /** The name of the user receiving the message. */
29
+ to_user_name: string
30
+ /** The login of the user receiving the message. */
31
+ to_user_login: string
32
+ /** The whisper ID. */
33
+ whisper_id: string
34
+ /** Object containing whisper information. */
35
+ whisper: {
36
+ /** The body of the whisper message. */
37
+ text: string
38
+ }
39
+ }
40
+ }>
41
+
42
+ registerEvent("user.whisper.message", {
43
+ scopes: ["user:manage:whispers", "user:read:whispers"],
44
+ subscriber: (userId) => ({
45
+ type: "user.whisper.message",
46
+ version: "1",
47
+ condition: {
48
+ user_id: userId,
49
+ },
50
+ }),
51
+ })
@@ -1,29 +1,57 @@
1
- export interface BaseSubscription<
2
- Type = unknown,
3
- Version = unknown,
4
- Condition = unknown,
5
- > {
6
- type: Type
7
- version: Version
8
- condition: Condition
9
- }
10
-
11
- export interface NotificationMessage<
12
- Subscription extends BaseSubscription,
13
- Event,
14
- > {
15
- type: "notification"
16
- metadata: {
17
- message_id: string
18
- message_type: "notification"
19
- message_timestamp: Date
20
- subscription_type: Subscription["type"]
21
- subscription_version: Subscription["version"]
1
+ export type EventConfig<
2
+ Config extends {
3
+ Type: string
4
+ Version: string
5
+ Condition: {}
6
+ Event: {}
7
+ },
8
+ > = Config & {
9
+ Subscription: {
10
+ type: Config["Type"]
11
+ version: Config["Version"]
12
+ condition: Config["Condition"]
22
13
  }
23
- payload: {
14
+ Payload: {
24
15
  id: string
25
- type: Subscription["type"]
26
- subscription: Subscription
27
- event: Event
16
+ type: Config["Type"]
17
+ subscription: {
18
+ type: Config["Type"]
19
+ version: Config["Version"]
20
+ condition: Config["Condition"]
21
+ }
22
+ event: Config["Event"]
28
23
  }
29
24
  }
25
+
26
+ export interface EventConfigs {}
27
+
28
+ export type EventType = keyof EventConfigs
29
+ export type EventPayload<Type extends EventType = EventType> =
30
+ EventConfigs[Type]["Payload"]
31
+
32
+ const events: {
33
+ [Type in EventType]?: {
34
+ scopes: string[]
35
+ subscriber: (userId: string) => EventConfigs[Type]["Subscription"]
36
+ }
37
+ } = {}
38
+
39
+ export function registerEvent<Type extends EventType>(
40
+ type: Type,
41
+ config: {
42
+ scopes: string[]
43
+ subscriber: (userId: string) => EventConfigs[Type]["Subscription"]
44
+ },
45
+ ): void {
46
+ // @ts-expect-error too complex for typescript
47
+ events[type] = config
48
+ }
49
+
50
+ export function buildSubscription<Type extends EventType>(
51
+ type: Type,
52
+ userId: string,
53
+ ): EventConfigs[Type]["Subscription"] {
54
+ if (!events[type]) throw new Error(`Event ${type} is not registered.`)
55
+
56
+ return events[type].subscriber(userId)
57
+ }
@@ -1,87 +1,50 @@
1
- import createDefer from "@overlaysymphony/core/libs/defer"
2
- import createPubSub from "@overlaysymphony/core/libs/pubsub"
3
-
4
- import { type Authentication } from "../authentication/index.js"
5
- import { createSubscription } from "../helix/subscriptions/index.js"
1
+ import "./events/index.ts"
6
2
 
7
- import {
8
- type TwitchNotificationMessage,
9
- type TwitchSubscriptionType,
10
- } from "./events/index.js"
11
- import { type TwitchMessage } from "./messages.js"
3
+ import createDefer from "@overlaysymphony/core/libs/defer"
4
+ import createEvents, { type Events } from "@overlaysymphony/core/libs/events"
12
5
 
13
- type EventSubListener = (
14
- callback: (event: TwitchNotificationMessage["payload"]) => void,
15
- ) => () => void
6
+ import { type Authentication } from "../authentication/index.ts"
7
+ import { createSubscription } from "../helix/subscriptions/index.ts"
16
8
 
17
- type EventSubSubscriber = <
18
- EventType extends TwitchSubscriptionType,
19
- Event extends TwitchNotificationMessage<EventType>["payload"],
20
- >(
21
- types: EventType[],
22
- callback: (event: Event) => void,
23
- ) => () => void
9
+ import { type EventPayload, buildSubscription } from "./events-helpers.ts"
10
+ import { type TwitchMessage } from "./messages.ts"
24
11
 
25
- export interface TwitchEventSub {
26
- listen: EventSubListener
27
- subscribe: EventSubSubscriber
28
- }
12
+ export type TwitchEventSub = Events<EventPayload>
29
13
 
30
- export async function createEventSub(
14
+ export default async function createEventSub(
31
15
  authentication: Authentication,
32
16
  ): Promise<TwitchEventSub> {
33
- const { promise, resolve } = createDefer<string>()
34
-
35
- const pubsub = createPubSub<TwitchNotificationMessage["payload"]>()
36
- const socket = new WebSocket("wss://eventsub.wss.twitch.tv/ws")
37
-
38
- socket.addEventListener("message", ({ data: rawData }) => {
39
- const data = JSON.parse(rawData as string) as TwitchMessage
40
- data.type = data.metadata.message_type
41
-
42
- if (data.type === "session_welcome") {
43
- resolve(data.payload.session.id)
44
- return
45
- }
46
-
47
- if (data.type === "notification") {
48
- data.payload.id = data.metadata.message_id
49
- data.payload.type = data.payload.subscription.type
50
-
51
- pubsub.dispatch(data.payload)
52
- return
53
- }
54
- })
55
-
56
- return promise.then((sessionId) => {
57
- const subscriptions: Partial<Record<TwitchSubscriptionType, boolean>> = {}
58
-
59
- const listen: EventSubListener = (callback) => {
60
- return pubsub.subscribe((event) => {
61
- callback(event)
62
- })
63
- }
64
-
65
- const subscribe: EventSubSubscriber = (types, callback) => {
66
- for (const type of types) {
67
- if (!subscriptions[type]) {
68
- subscriptions[type] = true
69
- void createSubscription(sessionId, authentication, type)
17
+ return createEvents<EventPayload, string>(
18
+ async (pubsub) => {
19
+ const socket = new WebSocket("wss://eventsub.wss.twitch.tv/ws")
20
+
21
+ const { promise, resolve } = createDefer<string>()
22
+ socket.addEventListener("message", ({ data: rawData }) => {
23
+ const data = JSON.parse(rawData as string) as TwitchMessage
24
+ data.type = data.metadata.message_type
25
+
26
+ if (data.type === "session_welcome") {
27
+ resolve(data.payload.session.id)
28
+ return
70
29
  }
71
- }
72
30
 
73
- return pubsub.subscribe((event) => {
74
- // @ts-expect-error: generic events are complicated
75
- if (types.includes(event.type)) {
76
- // @ts-expect-error: generic events are complicated
77
- callback(event)
31
+ if (data.type === "notification") {
32
+ data.payload.id = data.metadata.message_id
33
+ data.payload.type = data.payload.subscription.type
34
+
35
+ pubsub.dispatch(data.payload)
36
+ return
78
37
  }
79
38
  })
80
- }
81
39
 
82
- return {
83
- listen,
84
- subscribe,
85
- }
86
- })
40
+ return await promise
41
+ },
42
+ async (pubsub, sessionId, type) => {
43
+ await createSubscription(
44
+ authentication,
45
+ { method: "websocket", session_id: sessionId },
46
+ buildSubscription(type, authentication.user.id),
47
+ )
48
+ },
49
+ )
87
50
  }
@@ -1,7 +1,9 @@
1
- export type {
2
- TwitchSubscriptionType,
3
- TwitchSubscription,
4
- TwitchNotificationMessage,
5
- } from "./events/index.js"
1
+ export {
2
+ type EventConfig,
3
+ type EventConfigs,
4
+ type EventType,
5
+ registerEvent,
6
+ } from "./events-helpers.ts"
6
7
 
7
- export * from "./eventsub.js"
8
+ export { default } from "./eventsub.ts"
9
+ export * from "./eventsub.ts"
@@ -1,6 +1,6 @@
1
- import { type TwitchNotificationMessage } from "./events/index.js"
1
+ import { type EventConfigs, type EventType } from "./events-helpers.ts"
2
2
 
3
- export interface SessionWelcomeMessage {
3
+ export type SessionWelcomeMessage = {
4
4
  type: "session_welcome"
5
5
  metadata: {
6
6
  message_id: string
@@ -18,7 +18,7 @@ export interface SessionWelcomeMessage {
18
18
  }
19
19
  }
20
20
 
21
- export interface SessionKeepaliveMessage {
21
+ export type SessionKeepaliveMessage = {
22
22
  type: "session_keepalive"
23
23
  metadata: {
24
24
  message_id: string
@@ -28,7 +28,19 @@ export interface SessionKeepaliveMessage {
28
28
  payload: Record<string, never>
29
29
  }
30
30
 
31
+ export type NotificationMessage<Type extends EventType = EventType> = {
32
+ type: "notification"
33
+ metadata: {
34
+ message_id: string
35
+ message_type: "notification"
36
+ message_timestamp: Date
37
+ subscription_type: EventConfigs[Type]["Type"]
38
+ subscription_version: EventConfigs[Type]["Version"]
39
+ }
40
+ payload: EventConfigs[Type]["Payload"]
41
+ }
42
+
31
43
  export type TwitchMessage =
32
44
  | SessionWelcomeMessage
33
45
  | SessionKeepaliveMessage
34
- | TwitchNotificationMessage
46
+ | NotificationMessage
@@ -1,5 +1,5 @@
1
- import { type Authentication } from "../../authentication/index.js"
2
- import { helix } from "../helix.js"
1
+ import { type Authentication } from "../../authentication/index.ts"
2
+ import { helix } from "../helix.ts"
3
3
 
4
4
  interface CustomReward {
5
5
  broadcaster_id: string
@@ -51,7 +51,7 @@ export async function getCustomRewards(
51
51
  broadcaster_id: string
52
52
  }
53
53
  >(authentication, {
54
- method: "get",
54
+ method: "GET",
55
55
  path: "/channel_points/custom_rewards",
56
56
  params: {
57
57
  broadcaster_id: authentication.user.id,
@@ -1 +1 @@
1
- export * from "./custom-rewards.js"
1
+ export * from "./custom-rewards.ts"
@@ -1,6 +1,6 @@
1
1
  import querystring from "@overlaysymphony/core/libs/querystring"
2
2
 
3
- import { type Authentication } from "../authentication/index.js"
3
+ import { type Authentication } from "../authentication/index.ts"
4
4
 
5
5
  export async function helix<
6
6
  Data = never,
@@ -1 +1 @@
1
- export * from "./subscriptions.js"
1
+ export * from "./subscriptions.ts"
@@ -1,11 +1,7 @@
1
- import { type Authentication } from "../../authentication/index.js"
2
- import {
3
- type TwitchSubscription,
4
- type TwitchSubscriptionType,
5
- buildSubscription,
6
- } from "../../eventsub/events/index.js"
7
- import { type BaseSubscription } from "../../eventsub/events-helpers.js"
8
- import { helix } from "../helix.js"
1
+ import { type Authentication } from "../../authentication/index.ts"
2
+ import { type EventConfigs, type EventType } from "../../eventsub/index.ts"
3
+
4
+ import { helix } from "../helix.ts"
9
5
 
10
6
  interface SubscriptionWebhookTransport {
11
7
  method: "webhook"
@@ -22,15 +18,15 @@ type SubscriptionTransport =
22
18
  | SubscriptionWebhookTransport
23
19
  | SubscriptionWebsocketTransport
24
20
 
25
- export interface SubscriptionRequest<Subscription extends BaseSubscription> {
26
- type: Subscription["type"]
27
- version: Subscription["version"]
28
- condition: Subscription["condition"]
21
+ interface SubscriptionRequest<Type extends EventType> {
22
+ type: EventConfigs[Type]["Type"]
23
+ version: EventConfigs[Type]["Version"]
24
+ condition: EventConfigs[Type]["Condition"]
29
25
  transport: SubscriptionTransport
30
26
  }
31
27
 
32
- export type ActiveSubscription<Subscription extends BaseSubscription> =
33
- Subscription & {
28
+ type ActiveSubscription<Type extends EventType> =
29
+ EventConfigs[Type]["Subscription"] & {
34
30
  id: string
35
31
  status:
36
32
  | "enabled"
@@ -53,60 +49,57 @@ export type ActiveSubscription<Subscription extends BaseSubscription> =
53
49
  transport: SubscriptionTransport
54
50
  }
55
51
 
56
- export async function createSubscription<
57
- Type extends TwitchSubscriptionType,
58
- Subscription extends TwitchSubscription<Type>,
59
- >(
60
- sessionId: string,
52
+ export async function createSubscription<Type extends EventType>(
61
53
  authentication: Authentication,
62
- type: Type,
63
- ): Promise<ActiveSubscription<Subscription>> {
64
- const subscription = buildSubscription(type, authentication.user.id)
65
-
66
- const [activeSubscription] = await helix(authentication, {
67
- method: "post",
54
+ transport: SubscriptionTransport,
55
+ subscription: EventConfigs[Type]["Subscription"],
56
+ ): Promise<ActiveSubscription<Type>> {
57
+ const [activeSubscription] = await helix<
58
+ ActiveSubscription<EventType>,
59
+ never,
60
+ never,
61
+ SubscriptionRequest<EventType>
62
+ >(authentication, {
63
+ method: "POST",
68
64
  path: "/eventsub/subscriptions",
69
65
  body: {
70
66
  ...subscription,
71
- transport: {
72
- method: "websocket" as const,
73
- session_id: sessionId,
74
- },
67
+ transport,
75
68
  },
76
69
  })
77
70
 
78
71
  return activeSubscription
79
72
  }
80
73
 
81
- export async function deleteSubscription(
82
- authentication: Authentication,
83
- id: string,
84
- ): Promise<void> {
85
- await helix<never, never, { id: string }>(authentication, {
86
- method: "delete",
87
- path: "/eventsub/subscriptions",
88
- params: {
89
- id,
90
- },
91
- })
92
- }
93
-
94
74
  export async function getSubscriptions(
95
75
  authentication: Authentication,
96
- ): Promise<Array<ActiveSubscription<TwitchSubscription>>> {
76
+ ): Promise<Array<ActiveSubscription<EventType>>> {
97
77
  const subscriptions = await helix<
98
- ActiveSubscription<TwitchSubscription>,
78
+ ActiveSubscription<EventType>,
99
79
  never,
100
80
  {
101
- status?: ActiveSubscription<TwitchSubscription>["status"]
102
- type?: TwitchSubscriptionType
81
+ status?: ActiveSubscription<EventType>["status"]
82
+ type?: EventType
103
83
  user_id?: string
104
84
  after?: string
105
85
  }
106
86
  >(authentication, {
107
- method: "get",
87
+ method: "GET",
108
88
  path: "/eventsub/subscriptions",
109
89
  })
110
90
 
111
91
  return subscriptions
112
92
  }
93
+
94
+ export async function deleteSubscription(
95
+ authentication: Authentication,
96
+ id: string,
97
+ ): Promise<void> {
98
+ await helix<never, never, { id: string }>(authentication, {
99
+ method: "DELETE",
100
+ path: "/eventsub/subscriptions",
101
+ params: {
102
+ id,
103
+ },
104
+ })
105
+ }
@@ -1 +1 @@
1
- export * from "./users.js"
1
+ export * from "./users.ts"
@@ -1,5 +1,5 @@
1
- import { type Authentication } from "../../authentication/index.js"
2
- import { helix } from "../helix.js"
1
+ import { type Authentication } from "../../authentication/index.ts"
2
+ import { helix } from "../helix.ts"
3
3
 
4
4
  export interface TwitchUser {
5
5
  id: string
@@ -25,7 +25,7 @@ export async function getUsers(
25
25
  const data = await helix<TwitchUser, RawUser>(
26
26
  authentication,
27
27
  {
28
- method: "get",
28
+ method: "GET",
29
29
  path: "/users",
30
30
  params: {
31
31
  id,
@@ -1,16 +1,16 @@
1
1
  import createQueue, { type Queue } from "@overlaysymphony/core/libs/queue"
2
2
 
3
- import { type TwitchNotificationMessage } from "../../eventsub/events/index.js"
4
- import { type TwitchEventSub } from "../../eventsub/index.js"
3
+ import { type EventPayload } from "../../eventsub/events-helpers.ts"
4
+ import { type TwitchEventSub } from "../../eventsub/index.ts"
5
5
 
6
- export type Alert = TwitchNotificationMessage<
6
+ export type Alert = EventPayload<
7
7
  | "channel.cheer"
8
8
  | "channel.follow"
9
9
  | "channel.raid"
10
10
  | "channel.subscribe"
11
11
  | "channel.subscription.gift"
12
12
  | "channel.subscription.message"
13
- >["payload"]
13
+ >
14
14
 
15
15
  export const mapTypeToPriority = {
16
16
  "channel.follow": 0,
@@ -25,7 +25,7 @@ export function onAlert(
25
25
  eventsub: TwitchEventSub,
26
26
  handleAlert: (alert: Alert) => void,
27
27
  ): void {
28
- eventsub.subscribe(
28
+ eventsub.on(
29
29
  [
30
30
  "channel.cheer",
31
31
  "channel.follow",
@@ -1,2 +1,2 @@
1
- export { default } from "./alerts.js"
2
- export * from "./alerts.js"
1
+ export { default } from "./alerts.ts"
2
+ export * from "./alerts.ts"
@@ -1,4 +1,4 @@
1
- import { type TwitchEventSub } from "../../eventsub/index.js"
1
+ import { type TwitchEventSub } from "../../eventsub/index.ts"
2
2
 
3
3
  export interface Charity {
4
4
  name: string
@@ -42,7 +42,7 @@ export function onCharity(
42
42
  targetAmount: 0,
43
43
  }
44
44
 
45
- eventsub.subscribe(
45
+ eventsub.on(
46
46
  [
47
47
  "channel.charity_campaign.start",
48
48
  "channel.charity_campaign.progress",
@@ -1 +1 @@
1
- export * from "./charity.js"
1
+ export * from "./charity.ts"
@@ -1,4 +1,4 @@
1
- import { type TwitchEventSub } from "../../eventsub/index.js"
1
+ import { type TwitchEventSub } from "../../eventsub/index.ts"
2
2
 
3
3
  export interface Goal {
4
4
  type: string
@@ -24,7 +24,7 @@ export function onGoal(
24
24
  targetAmount: 0,
25
25
  }
26
26
 
27
- eventsub.subscribe(
27
+ eventsub.on(
28
28
  ["channel.goal.begin", "channel.goal.progress", "channel.goal.end"],
29
29
  (payload) => {
30
30
  goal.type = payload.event.type
@@ -1 +1 @@
1
- export * from "./goal.js"
1
+ export * from "./goal.ts"
@@ -1,4 +1,4 @@
1
- import { type TwitchEventSub } from "../../eventsub/index.js"
1
+ import { type TwitchEventSub } from "../../eventsub/index.ts"
2
2
 
3
3
  export interface HypeTrain {
4
4
  level: number
@@ -27,7 +27,7 @@ export function onHypeTrain(
27
27
  goal: 0,
28
28
  }
29
29
 
30
- eventsub.subscribe(
30
+ eventsub.on(
31
31
  [
32
32
  "channel.hype_train.begin",
33
33
  "channel.hype_train.progress",
@@ -1 +1 @@
1
- export * from "./hype-train.js"
1
+ export * from "./hype-train.ts"
@@ -1 +1 @@
1
- export * from "./poll.js"
1
+ export * from "./poll.ts"
@@ -1,4 +1,4 @@
1
- import { type TwitchEventSub } from "../../eventsub/index.js"
1
+ import { type TwitchEventSub } from "../../eventsub/index.ts"
2
2
 
3
3
  export interface Poll {
4
4
  id: string
@@ -31,7 +31,7 @@ export function onPoll(
31
31
  endsAt: undefined,
32
32
  }
33
33
 
34
- eventsub.subscribe(
34
+ eventsub.on(
35
35
  ["channel.poll.begin", "channel.poll.progress", "channel.poll.end"],
36
36
  (payload) => {
37
37
  // Twitch sometimes sends duplicate end events.
@@ -66,7 +66,7 @@ export function onPoll(
66
66
  payload.type === "channel.poll.begin" ||
67
67
  payload.type === "channel.poll.progress"
68
68
  ) {
69
- poll.endsAt = payload.event.ends_at
69
+ poll.endsAt = new Date(payload.event.ends_at)
70
70
  } else {
71
71
  poll.endsAt = undefined
72
72
  }
@@ -1 +1 @@
1
- export * from "./prediction.js"
1
+ export * from "./prediction.ts"
@@ -1,4 +1,4 @@
1
- import { type TwitchEventSub } from "../../eventsub/index.js"
1
+ import { type TwitchEventSub } from "../../eventsub/index.ts"
2
2
 
3
3
  export interface Prediction {
4
4
  title: string
@@ -32,7 +32,7 @@ export function onPrediction(
32
32
  locksAt: new Date(""),
33
33
  }
34
34
 
35
- eventsub.subscribe(
35
+ eventsub.on(
36
36
  [
37
37
  "channel.prediction.begin",
38
38
  "channel.prediction.progress",
@@ -55,7 +55,7 @@ export function onPrediction(
55
55
  payload.type === "channel.prediction.begin" ||
56
56
  payload.type === "channel.prediction.progress"
57
57
  ) {
58
- prediction.locksAt = payload.event.locks_at
58
+ prediction.locksAt = new Date(payload.event.locks_at)
59
59
  } else {
60
60
  prediction.locksAt = undefined
61
61
  }
@@ -1 +1 @@
1
- export * from "./redemption.js"
1
+ export * from "./redemption.ts"