Skip to content

Commit

Permalink
add helix channel API methods
Browse files Browse the repository at this point in the history
  • Loading branch information
d-fischer committed Nov 16, 2020
1 parent 0c04d4a commit 474e9fe
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
75 changes: 75 additions & 0 deletions packages/twitch/src/API/Helix/Channel/HelixChannel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Enumerable } from '@d-fischer/shared-utils';
import type { HelixGame } from '../Game/HelixGame';
import type { ApiClient } from '../../../ApiClient';

/** @private */
export interface HelixChannelData {
broadcaster_id: string;
broadcaster_name: string;
broadcaster_language: string;
game_id: string;
game_name: string;
title: string;
}

/**
* A Twitch channel.
*/
export class HelixChannel {
/** @private */
@Enumerable(false) protected readonly _client: ApiClient;

/** @private */
constructor(/** @private */ protected _data: HelixChannelData, client: ApiClient) {
this._client = client;
}

/**
* The ID of the channel.
*/
get id(): string {
return this._data.broadcaster_id;
}

/**
* The display name of the channel.
*/
get displayName(): string {
return this._data.broadcaster_name;
}

/**
* The language of the channel.
*/
get language(): string {
return this._data.broadcaster_language;
}

/**
* The ID of the game currently played on the channel.
*/
get gameId(): string {
return this._data.game_id;
}

/**
* The name of the game currently played on the channel.
*/
get gameName(): string {
return this._data.game_name;
}

/**
* Retrieves information about the game that is being played on this stream.
*/
async getGame(): Promise<HelixGame | null> {
return this._client.helix.games.getGameById(this._data.game_id);
}

/**
* The title of the channel.
*/
get title(): string {
return this._data.title;
}
}
82 changes: 82 additions & 0 deletions packages/twitch/src/API/Helix/Channel/HelixChannelApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { TwitchApiCallType } from 'twitch-api-call';
import type { UserIdResolvable } from '../../../Toolkit/UserTools';
import { extractUserId } from '../../../Toolkit/UserTools';
import { BaseApi } from '../../BaseApi';
import type { HelixPaginatedResponse } from '../HelixResponse';
import type { HelixChannelData } from './HelixChannel';
import { HelixChannel } from './HelixChannel';

/**
* Channel data to update using {@HelixChannelApi#updateChannel}.
*/
export interface HelixChannelUpdate {
/**
* The language of the stream.
*/
language?: string;

/**
* The ID of the game you're playing.
*/
gameId?: string;

/**
* The title of the stream.
*/
title?: string;
}

/**
* The Helix API methods that deal with channels.
*
* Can be accessed using `client.helix.channels` on an {@ApiClient} instance.
*
* ## Example
* ```ts
* const api = new ApiClient(new StaticAuthProvider(clientId, accessToken));
* const channel = await api.helix.channels.getChannelInfoByUserId('125328655');
* ```
*/
export class HelixChannelApi extends BaseApi {
/**
* Retrieves the channel data for the given user.
*
* @param user The user you want to get channel info for.
*/
async getChannelInfo(user: UserIdResolvable): Promise<HelixChannel | null> {
const userId = extractUserId(user);
const result = await this._client.callApi<HelixPaginatedResponse<HelixChannelData>>({
type: TwitchApiCallType.Helix,
url: 'channels',
query: {
broadcaster_id: userId
}
});

return result.data.length ? new HelixChannel(result.data[0], this._client) : null;
}

/**
* Updates the given user's channel data.
*
* @param user The user you want to update channel info for.
* @param data The channel info to set.
*/
async updateChannelInfo(user: UserIdResolvable, data: HelixChannelUpdate): Promise<void> {
const userId = extractUserId(user);
await this._client.callApi({
type: TwitchApiCallType.Helix,
url: 'channels',
method: 'PATCH',
scope: 'user:edit:broadcast',
query: {
broadcaster_id: userId
},
jsonBody: {
game_id: data.gameId,
broadcaster_language: data.language,
title: data.title
}
});
}
}
9 changes: 9 additions & 0 deletions packages/twitch/src/API/Helix/HelixApiGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Cacheable, CachedGetter } from '@d-fischer/cache-decorators';
import { BaseApi } from '../BaseApi';

import { HelixBitsApi } from './Bits/HelixBitsApi';
import { HelixChannelApi } from './Channel/HelixChannelApi';
import { HelixClipApi } from './Clip/HelixClipApi';
import { HelixExtensionsApi } from './Extensions/HelixExtensionsApi';
import { HelixGameApi } from './Game/HelixGameApi';
Expand All @@ -28,6 +29,14 @@ export class HelixApiGroup extends BaseApi {
return new HelixBitsApi(this._client);
}

/**
* The Helix channels API methods.
*/
@CachedGetter()
get channels(): HelixChannelApi {
return new HelixChannelApi(this._client);
}

/**
* The Helix clips API methods.
*/
Expand Down

0 comments on commit 474e9fe

Please sign in to comment.