-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
HelixChannelPointsApi.ts
421 lines (384 loc) · 12.1 KB
/
HelixChannelPointsApi.ts
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import type { HelixPaginatedResponse, HelixResponse } from '@twurple/api-call';
import type { UserIdResolvable } from '@twurple/common';
import { extractUserId, rtfm } from '@twurple/common';
import { BaseApi } from '../../BaseApi';
import { HelixPaginatedRequest } from '../HelixPaginatedRequest';
import type { HelixPaginatedResult } from '../HelixPaginatedResult';
import { createPaginatedResult } from '../HelixPaginatedResult';
import type { HelixForwardPagination } from '../HelixPagination';
import { makePaginationQuery } from '../HelixPagination';
import type { HelixCustomRewardData } from './HelixCustomReward';
import { HelixCustomReward } from './HelixCustomReward';
import type {
HelixCustomRewardRedemptionData,
HelixCustomRewardRedemptionStatus,
HelixCustomRewardRedemptionTargetStatus
} from './HelixCustomRewardRedemption';
import { HelixCustomRewardRedemption } from './HelixCustomRewardRedemption';
/**
* Data to create a new custom reward.
*/
export interface HelixCreateCustomRewardData {
/**
* The title of the reward.
*/
title: string;
/**
* The channel points cost of the reward.
*/
cost: number;
/**
* The prompt shown to users when redeeming the reward.
*/
prompt?: string;
/**
* Whether the reward is enabled (shown to users).
*/
isEnabled?: boolean;
/**
* The hex code of the background color of the reward.
*/
backgroundColor?: string;
/**
* Whether the reward requires user input to be redeemed.
*/
userInputRequired?: boolean;
/**
* The maximum number of redemptions of the reward per stream. 0 or `null` means no limit.
*/
maxRedemptionsPerStream?: number | null;
/**
* The maximum number of redemptions of the reward per stream for each user. 0 or `null` means no limit.
*/
maxRedemptionsPerUserPerStream?: number | null;
/**
* The cooldown between two redemptions of the reward, in seconds. 0 or `null` means no cooldown.
*/
globalCooldown?: number | null;
/**
* Whether the redemption should automatically set its status to fulfilled.
*/
autoFulfill?: boolean;
}
/**
* Data to update an existing custom reward.
*
* @inheritDoc
*/
export interface HelixUpdateCustomRewardData extends Partial<HelixCreateCustomRewardData> {
/**
* Whether the reward is paused. If true, users can't redeem it.
*/
isPaused?: boolean;
}
/**
* Filters for the custom reward redemptions request.
*/
export interface HelixCustomRewardRedemptionFilter {
/**
* Whether to put the newest redemptions first.
*
* Oldest redemptions are shown first by default.
*/
newestFirst?: boolean;
}
/**
* @inheritDoc
*/
export interface HelixPaginatedCustomRewardRedemptionFilter
extends HelixCustomRewardRedemptionFilter,
HelixForwardPagination {}
/**
* The Helix API methods that deal with channel points.
*
* Can be accessed using `client.channelPoints` on an {@ApiClient} instance.
*
* ## Example
* ```ts
* const api = new ApiClient(new StaticAuthProvider(clientId, accessToken));
* const rewards = await api.channelPoints.getCustomRewards('125328655');
* ```
*/
@rtfm('api', 'HelixChannelPointsApi')
export class HelixChannelPointsApi extends BaseApi {
/**
* Retrieves all custom rewards for the given broadcaster.
*
* @param broadcaster The broadcaster to retrieve the rewards for.
* @param onlyManageable Whether to only retrieve rewards that can be managed by the API.
*/
async getCustomRewards(broadcaster: UserIdResolvable, onlyManageable?: boolean): Promise<HelixCustomReward[]> {
const result = await this._client.callApi<HelixResponse<HelixCustomRewardData>>({
type: 'helix',
url: 'channel_points/custom_rewards',
scope: 'channel:read:redemptions',
query: {
broadcaster_id: extractUserId(broadcaster),
only_manageable_rewards: onlyManageable?.toString()
}
});
return result.data.map(data => new HelixCustomReward(data, this._client));
}
/**
* Retrieves custom rewards by IDs.
*
* @param broadcaster The broadcaster to retrieve the rewards for.
* @param rewardIds The IDs of the rewards.
*/
async getCustomRewardsByIds(broadcaster: UserIdResolvable, rewardIds: string[]): Promise<HelixCustomReward[]> {
if (!rewardIds.length) {
return [];
}
const result = await this._client.callApi<HelixResponse<HelixCustomRewardData>>({
type: 'helix',
url: 'channel_points/custom_rewards',
scope: 'channel:read:redemptions',
query: {
broadcaster_id: extractUserId(broadcaster),
id: rewardIds
}
});
return result.data.map(data => new HelixCustomReward(data, this._client));
}
/**
* Retrieves a custom reward by ID.
*
* @param broadcaster The broadcaster to retrieve the reward for.
* @param rewardId The ID of the reward.
*/
async getCustomRewardById(broadcaster: UserIdResolvable, rewardId: string): Promise<HelixCustomReward | null> {
const rewards = await this.getCustomRewardsByIds(broadcaster, [rewardId]);
return rewards.length ? rewards[0] : null;
}
/**
* Creates a new custom reward.
*
* @param broadcaster The broadcaster to create the reward for.
* @param data The reward data.
*
* @expandParams
*/
async createCustomReward(
broadcaster: UserIdResolvable,
data: HelixCreateCustomRewardData
): Promise<HelixCustomReward> {
const result = await this._client.callApi<HelixResponse<HelixCustomRewardData>>({
type: 'helix',
url: 'channel_points/custom_rewards',
method: 'POST',
scope: 'channel:manage:redemptions',
query: {
broadcaster_id: extractUserId(broadcaster)
},
jsonBody: HelixChannelPointsApi._transformRewardData(data)
});
return new HelixCustomReward(result.data[0], this._client);
}
/**
* Updates a custom reward.
*
* @param broadcaster The broadcaster to update the reward for.
* @param rewardId The ID of the reward.
* @param data The reward data.
*/
async updateCustomReward(
broadcaster: UserIdResolvable,
rewardId: string,
data: HelixUpdateCustomRewardData
): Promise<HelixCustomReward> {
const result = await this._client.callApi<HelixResponse<HelixCustomRewardData>>({
type: 'helix',
url: 'channel_points/custom_rewards',
method: 'PATCH',
scope: 'channel:manage:redemptions',
query: {
broadcaster_id: extractUserId(broadcaster),
id: rewardId
},
jsonBody: HelixChannelPointsApi._transformRewardData(data)
});
return new HelixCustomReward(result.data[0], this._client);
}
/**
* Deletes a custom reward.
*
* @param broadcaster The broadcaster to delete the reward for.
* @param rewardId The ID of the reward.
*/
async deleteCustomReward(broadcaster: UserIdResolvable, rewardId: string): Promise<void> {
await this._client.callApi({
type: 'helix',
url: 'channel_points/custom_rewards',
method: 'DELETE',
scope: 'channel:manage:redemptions',
query: {
broadcaster_id: extractUserId(broadcaster),
id: rewardId
}
});
}
/**
* Retrieves custom reward redemptions by IDs.
*
* @param broadcaster The broadcaster to retrieve the redemptions for.
* @param rewardId The ID of the reward.
* @param redemptionIds The IDs of the redemptions.
*/
async getRedemptionsByIds(
broadcaster: UserIdResolvable,
rewardId: string,
redemptionIds: string[]
): Promise<HelixCustomRewardRedemption[]> {
if (!redemptionIds.length) {
return [];
}
const result = await this._client.callApi<HelixResponse<HelixCustomRewardRedemptionData>>({
type: 'helix',
url: 'channel_points/custom_rewards/redemptions',
scope: 'channel:read:redemptions',
query: {
broadcaster_id: extractUserId(broadcaster),
reward_id: rewardId,
id: redemptionIds
}
});
return result.data.map(data => new HelixCustomRewardRedemption(data, this._client));
}
/**
* Retrieves a custom reward redemption by ID.
*
* @param broadcaster The broadcaster to retrieve the redemption for.
* @param rewardId The ID of the reward.
* @param redemptionId The ID of the redemption.
*/
async getRedemptionById(
broadcaster: UserIdResolvable,
rewardId: string,
redemptionId: string
): Promise<HelixCustomRewardRedemption | null> {
const redemptions = await this.getRedemptionsByIds(broadcaster, rewardId, [redemptionId]);
return redemptions.length ? redemptions[0] : null;
}
/**
* Retrieves custom reward redemptions for the given broadcaster.
*
* @param broadcaster The broadcaster to retrieve the redemptions for.
* @param rewardId The ID of the reward.
* @param status The status of the redemptions to retrieve.
* @param filter
*
* @expandParams
*/
async getRedemptionsForBroadcaster(
broadcaster: UserIdResolvable,
rewardId: string,
status: HelixCustomRewardRedemptionStatus,
filter: HelixPaginatedCustomRewardRedemptionFilter
): Promise<HelixPaginatedResult<HelixCustomRewardRedemption>> {
const result = await this._client.callApi<HelixPaginatedResponse<HelixCustomRewardRedemptionData>>({
type: 'helix',
url: 'channel_points/custom_rewards/redemptions',
scope: 'channel:read:redemptions',
query: {
broadcaster_id: extractUserId(broadcaster),
reward_id: rewardId,
status,
sort: filter.newestFirst ? 'NEWEST' : 'OLDEST',
...makePaginationQuery(filter)
}
});
return createPaginatedResult(result, HelixCustomRewardRedemption, this._client);
}
/**
* Creates a paginator for custom reward redemptions for the given broadcaster.
*
* @param broadcaster The broadcaster to retrieve the redemptions for.
* @param rewardId The ID of the reward.
* @param status The status of the redemptions to retrieve.
* @param filter
*
* @expandParams
*/
getRedemptionsForBroadcasterPaginated(
broadcaster: UserIdResolvable,
rewardId: string,
status: HelixCustomRewardRedemptionStatus,
filter: HelixCustomRewardRedemptionFilter
): HelixPaginatedRequest<HelixCustomRewardRedemptionData, HelixCustomRewardRedemption> {
return new HelixPaginatedRequest(
{
url: 'channel_points/custom_rewards/redemptions',
scope: 'channel:read:redemptions',
query: {
broadcaster_id: extractUserId(broadcaster),
reward_id: rewardId,
status,
sort: filter.newestFirst ? 'NEWEST' : 'OLDEST'
}
},
this._client,
data => new HelixCustomRewardRedemption(data, this._client),
50
);
}
/**
* Updates the status of the given redemptions by IDs.
*
* @param broadcaster The broadcaster to retrieve the redemptions for.
* @param rewardId The ID of the reward.
* @param redemptionIds The IDs of the redemptions to update.
* @param status The status to set for the redemptions.
*/
async updateRedemptionStatusByIds(
broadcaster: UserIdResolvable,
rewardId: string,
redemptionIds: string[],
status: HelixCustomRewardRedemptionTargetStatus
): Promise<HelixCustomRewardRedemption[]> {
if (!redemptionIds.length) {
return [];
}
const result = await this._client.callApi<HelixResponse<HelixCustomRewardRedemptionData>>({
type: 'helix',
url: 'channel_points/custom_rewards/redemptions',
method: 'PATCH',
scope: 'channel:manage:redemptions',
query: {
broadcaster_id: extractUserId(broadcaster),
reward_id: rewardId,
id: redemptionIds
},
jsonBody: {
status
}
});
return result.data.map(data => new HelixCustomRewardRedemption(data, this._client));
}
private static _transformRewardData(data: HelixCreateCustomRewardData | HelixUpdateCustomRewardData) {
const result: Record<string, unknown> = {
title: data.title,
cost: data.cost,
prompt: data.prompt,
background_color: data.backgroundColor,
is_enabled: data.isEnabled,
is_user_input_required: data.userInputRequired,
should_redemptions_skip_request_queue: data.autoFulfill
};
if (data.maxRedemptionsPerStream !== undefined) {
result.is_max_per_stream_enabled = !!data.maxRedemptionsPerStream;
result.max_per_stream = data.maxRedemptionsPerStream ?? 0;
}
if (data.maxRedemptionsPerUserPerStream !== undefined) {
result.is_max_per_user_per_stream_enabled = !!data.maxRedemptionsPerUserPerStream;
result.max_per_user_per_stream = data.maxRedemptionsPerUserPerStream ?? 0;
}
if (data.globalCooldown !== undefined) {
result.is_global_cooldown_enabled = !!data.globalCooldown;
result.global_cooldown_seconds = data.globalCooldown ?? 0;
}
if ('isPaused' in data) {
result.is_paused = data.isPaused;
}
return result;
}
}