Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add creator goals to the EventSub and the API package (#315)
* Add creator goals to the Helix API * Add creator goals to EventSub * Add creator goals property names to eslint * Fix the creator goal type for EventSub * Add consistency in the docs Co-authored-by: Daniel Fischer <daniel@d-fischer.dev>
- Loading branch information
1 parent
2b610c6
commit fe443b4
Showing
16 changed files
with
707 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { Enumerable } from '@d-fischer/shared-utils'; | ||
import { DataObject, rawDataSymbol, rtfm } from '@twurple/common'; | ||
import type { ApiClient } from '../../../ApiClient'; | ||
import type { HelixUser } from '../user/HelixUser'; | ||
|
||
export type HelixGoalType = 'follower' | 'subscription'; | ||
|
||
/** @private */ | ||
export interface HelixGoalData { | ||
id: string; | ||
broadcaster_id: string; | ||
broadcaster_name: string; | ||
broadcaster_login: string; | ||
type: HelixGoalType; | ||
description: string; | ||
current_amount: number; | ||
target_amount: number; | ||
created_at: Date; | ||
} | ||
|
||
/** | ||
* A creator goal. | ||
*/ | ||
@rtfm<HelixGoal>('api', 'HelixGoal', 'id') | ||
export class HelixGoal extends DataObject<HelixGoalData> { | ||
@Enumerable(false) private readonly _client: ApiClient; | ||
|
||
/** @private */ | ||
constructor(data: HelixGoalData, client: ApiClient) { | ||
super(data); | ||
this._client = client; | ||
} | ||
|
||
/** | ||
* The ID of the goal. | ||
*/ | ||
get id(): string { | ||
return this[rawDataSymbol].id; | ||
} | ||
|
||
/** | ||
* The ID of the broadcaster the goal belongs to. | ||
*/ | ||
get broadcasterId(): string { | ||
return this[rawDataSymbol].broadcaster_id; | ||
} | ||
|
||
/** | ||
* The display name of the broadcaster the goal belongs to. | ||
*/ | ||
get broadcasterDisplayName(): string { | ||
return this[rawDataSymbol].broadcaster_name; | ||
} | ||
|
||
/** | ||
* The name of the broadcaster the goal belongs to. | ||
*/ | ||
get broadcasterName(): string { | ||
return this[rawDataSymbol].broadcaster_login; | ||
} | ||
|
||
/** | ||
* Retrieves more information about the broadcaster. | ||
*/ | ||
async getBroadcaster(): Promise<HelixUser> { | ||
return (await this._client.users.getUserById(this[rawDataSymbol].broadcaster_id))!; | ||
} | ||
|
||
/** | ||
* The type of the goal. Can be either "follower" or "subscription". | ||
*/ | ||
get type(): HelixGoalType { | ||
return this[rawDataSymbol].type; | ||
} | ||
|
||
/** | ||
* The description of the goal. | ||
*/ | ||
get description(): string { | ||
return this[rawDataSymbol].description; | ||
} | ||
|
||
/** | ||
* The current value of the goal. | ||
*/ | ||
get currentAmount(): number { | ||
return this[rawDataSymbol].current_amount; | ||
} | ||
|
||
/** | ||
* The target value of the goal. | ||
*/ | ||
get targetAmount(): number { | ||
return this[rawDataSymbol].target_amount; | ||
} | ||
|
||
/** | ||
* The date and time when the goal was created. | ||
*/ | ||
get creationDate(): Date { | ||
return this[rawDataSymbol].created_at; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { UserIdResolvable } from '@twurple/common'; | ||
import { extractUserId, rtfm } from '@twurple/common'; | ||
import { BaseApi } from '../../BaseApi'; | ||
import type { HelixResponse } from '../HelixResponse'; | ||
import type { HelixGoalData } from './HelixGoal'; | ||
import { HelixGoal } from './HelixGoal'; | ||
|
||
/** | ||
* The Helix API methods that deal with creator goals. | ||
* | ||
* Can be accessed using `client.helix.goals` on an {@ApiClient} instance. | ||
* | ||
* ## Example | ||
* ```ts | ||
* const api = new ApiClient(new StaticAuthProvider(clientId, accessToken)); | ||
* const { data: goals } = await api.helix.goals.getGoals('61369223'); | ||
*/ | ||
@rtfm('api', 'HelixGoalApi') | ||
export class HelixGoalApi extends BaseApi { | ||
async getGoals(broadcaster: UserIdResolvable): Promise<HelixGoal[]> { | ||
const result = await this._client.callApi<HelixResponse<HelixGoalData>>({ | ||
type: 'helix', | ||
url: 'goals', | ||
query: { | ||
broadcaster_id: extractUserId(broadcaster) | ||
} | ||
}); | ||
|
||
return result.data.map(data => new HelixGoal(data, this._client)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.