|
1 | 1 | import { OpenStringUnion } from '@app/utils'; |
2 | | -import { ivrApi } from './api'; |
| 2 | +import { twitchApi } from './api'; |
3 | 3 |
|
4 | | -interface TwitchBadge { |
| 4 | +interface TwitchBadgeVersion { |
5 | 5 | id: string; |
6 | 6 | image_url_1x: string; |
7 | 7 | image_url_2x: string; |
8 | 8 | image_url_4x: string; |
9 | | - /** |
10 | | - * @example cheer 1 |
11 | | - */ |
12 | 9 | title: string; |
13 | | - |
14 | | - /** |
15 | | - * @example cheer 1 |
16 | | - */ |
17 | 10 | description: string; |
18 | | - click_action: OpenStringUnion<'visit_url' | 'subscribe_to_channel'> | null; |
| 11 | + click_action: string; |
19 | 12 | click_url: string | null; |
20 | 13 | } |
21 | 14 |
|
22 | | -interface IvrChannelBadges { |
23 | | - set_id: OpenStringUnion<'bits' | 'subscriber'>; |
24 | | - versions: TwitchBadge[]; |
| 15 | +interface TwitchBadge { |
| 16 | + set_id: OpenStringUnion<'subscriber' | 'bits'>; |
| 17 | + versions: TwitchBadgeVersion[]; |
| 18 | +} |
| 19 | + |
| 20 | +export interface SanitisedBadgeSet { |
| 21 | + id: string; |
| 22 | + url: string; |
| 23 | + type: OpenStringUnion< |
| 24 | + | 'Twitch Channel Badge' |
| 25 | + | 'Twitch Subscriber Badge' |
| 26 | + | 'Twitch Bit Badge' |
| 27 | + | 'Twitch Global Badge' |
| 28 | + >; |
| 29 | + title: string; |
| 30 | + |
| 31 | + color?: string; |
| 32 | + owner_username?: string; |
| 33 | + /** |
| 34 | + * The set ID |
| 35 | + */ |
| 36 | + set: string; |
25 | 37 | } |
26 | 38 |
|
27 | 39 | export const twitchBadgeService = { |
28 | | - getBadges: async (broadcasterLogin: string) => { |
29 | | - const result = await ivrApi.get<IvrChannelBadges[]>( |
30 | | - '/twitch/badges/channel', |
| 40 | + listSanitisedChannelBadges: async ( |
| 41 | + channelId: string, |
| 42 | + ): Promise<SanitisedBadgeSet[]> => { |
| 43 | + const result = await twitchApi.get<{ data: TwitchBadge[] }>( |
| 44 | + '/chat/badges', |
31 | 45 | { |
32 | 46 | params: { |
33 | | - login: broadcasterLogin, |
| 47 | + broadcaster_id: channelId, |
34 | 48 | }, |
35 | 49 | }, |
36 | 50 | ); |
37 | 51 |
|
38 | | - return result; |
| 52 | + const sanitisedBadges: SanitisedBadgeSet[] = []; |
| 53 | + |
| 54 | + result.data.forEach(badgeSet => { |
| 55 | + if (badgeSet.set_id === 'bits') { |
| 56 | + badgeSet.versions.forEach((badge: TwitchBadgeVersion) => { |
| 57 | + sanitisedBadges.push({ |
| 58 | + id: badge.id, |
| 59 | + url: badge.image_url_4x, |
| 60 | + type: 'Twitch Bit Badge', |
| 61 | + title: `Cheer ${badge.id}`, |
| 62 | + set: badgeSet.set_id, |
| 63 | + }); |
| 64 | + }); |
| 65 | + } |
| 66 | + if (badgeSet.set_id === 'subscriber') { |
| 67 | + badgeSet.versions.forEach((badge: TwitchBadgeVersion) => { |
| 68 | + sanitisedBadges.push({ |
| 69 | + id: badge.id, |
| 70 | + url: badge.image_url_4x, |
| 71 | + type: 'Twitch Subscriber Badge', |
| 72 | + title: badge.title, |
| 73 | + set: badgeSet.set_id, |
| 74 | + }); |
| 75 | + }); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + return sanitisedBadges; |
39 | 80 | }, |
40 | | - getGlobalBadges: async () => { |
41 | | - const result = await ivrApi.get<IvrChannelBadges[]>( |
42 | | - '/twitch/badges/global', |
| 81 | + listSanitisedGlobalBadges: async (): Promise<SanitisedBadgeSet[]> => { |
| 82 | + const result = await twitchApi.get<{ data: TwitchBadge[] }>( |
| 83 | + '/chat/badges/global', |
43 | 84 | ); |
44 | 85 |
|
45 | | - return result; |
| 86 | + const sanitisedBadges: SanitisedBadgeSet[] = []; |
| 87 | + |
| 88 | + result.data.forEach(badgeSet => { |
| 89 | + if (Object.keys(badgeSet).length > 0) { |
| 90 | + badgeSet.versions.forEach(version => { |
| 91 | + sanitisedBadges.push({ |
| 92 | + id: `${badgeSet.set_id}_${version.id}`, // set set_id as id |
| 93 | + url: version.image_url_4x, |
| 94 | + title: version.title, |
| 95 | + type: 'Twitch Global Badge', |
| 96 | + set: badgeSet.set_id, |
| 97 | + }); |
| 98 | + }); |
| 99 | + } |
| 100 | + }); |
| 101 | + return sanitisedBadges; |
46 | 102 | }, |
47 | 103 | } as const; |
0 commit comments