-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
443 additions
and
4 deletions.
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
108 changes: 108 additions & 0 deletions
108
packages/twitch/src/API/Helix/Search/HelixChannelSearchResult.ts
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,108 @@ | ||
import { Enumerable } from '@d-fischer/shared-utils'; | ||
import type { ApiClient } from '../../../ApiClient'; | ||
import type { HelixGame } from '../Game/HelixGame'; | ||
import type { HelixTag } from '../Tag/HelixTag'; | ||
import type { HelixUser } from '../User/HelixUser'; | ||
|
||
/** @private */ | ||
export interface HelixChannelSearchResultData { | ||
broadcaster_language: string; | ||
display_name: string; | ||
game_id: string; | ||
id: string; | ||
is_live: boolean; | ||
tag_ids: string[]; | ||
thumbnail_url: string; | ||
title: string; | ||
started_at: string; | ||
} | ||
|
||
/** | ||
* The result of a channel search. | ||
*/ | ||
export class HelixChannelSearchResult { | ||
/** @private */ | ||
@Enumerable(false) protected readonly _client: ApiClient; | ||
|
||
/** @private */ | ||
constructor(/** @private */ protected _data: HelixChannelSearchResultData, client: ApiClient) { | ||
this._client = client; | ||
} | ||
|
||
/** | ||
* The language of the channel. | ||
*/ | ||
get language(): string { | ||
return this._data.broadcaster_language; | ||
} | ||
|
||
/** | ||
* The ID of the channel. | ||
*/ | ||
get id(): string { | ||
return this._data.id; | ||
} | ||
|
||
/** | ||
* Retrieves additional information about the owner of the channel. | ||
*/ | ||
async getUser(): Promise<HelixUser | null> { | ||
return this._client.helix.users.getUserById(this._data.id); | ||
} | ||
|
||
/** | ||
* The display name of the channel | ||
*/ | ||
get displayName(): string { | ||
return this._data.display_name; | ||
} | ||
|
||
/** | ||
* The ID of the game currently played on the channel. | ||
*/ | ||
get gameId(): string { | ||
return this._data.game_id; | ||
} | ||
|
||
/** | ||
* Retrieves information about the game that is being played on the stream. | ||
*/ | ||
async getGame(): Promise<HelixGame | null> { | ||
return this._client.helix.games.getGameById(this._data.game_id); | ||
} | ||
|
||
/** | ||
* Whether the channel is currently live. | ||
*/ | ||
get isLive(): boolean { | ||
return this._data.is_live; | ||
} | ||
|
||
/** | ||
* The IDs of the tags set on the channel. | ||
*/ | ||
get tagIds(): string[] { | ||
return this._data.tag_ids; | ||
} | ||
|
||
/** | ||
* Retrieves the tags of the channel. | ||
*/ | ||
async getTags(): Promise<HelixTag[]> { | ||
return this._client.helix.tags.getStreamTagsByIds(this._data.tag_ids); | ||
} | ||
|
||
/** | ||
* The thumbnail URL of the stream. | ||
*/ | ||
get thumbnailUrl(): string { | ||
return this._data.thumbnail_url; | ||
} | ||
|
||
/** | ||
* The start date of the stream. Returns `null` if the stream is not live. | ||
*/ | ||
get startDate(): Date | null { | ||
return this._data.is_live ? new Date(this._data.started_at) : null; | ||
} | ||
} |
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,130 @@ | ||
import { TwitchApiCallType } from 'twitch-api-call'; | ||
import { BaseApi } from '../../BaseApi'; | ||
import type { HelixGameData } from '../Game/HelixGame'; | ||
import { HelixGame } from '../Game/HelixGame'; | ||
import { HelixPaginatedRequest } from '../HelixPaginatedRequest'; | ||
import type { HelixPaginatedResult } from '../HelixPaginatedResult'; | ||
import { createPaginatedResult } from '../HelixPaginatedResult'; | ||
import type { HelixForwardPagination } from '../HelixPagination'; | ||
import { makePaginationQuery } from '../HelixPagination'; | ||
import type { HelixPaginatedResponse } from '../HelixResponse'; | ||
import { HelixUser } from '../User/HelixUser'; | ||
import type { HelixUserData } from '../User/HelixUser'; | ||
|
||
/** | ||
* Filters for a channel search. | ||
*/ | ||
export interface HelixChannelSearchFilter { | ||
/** | ||
* Include only channels that are currently live. | ||
*/ | ||
liveOnly?: boolean; | ||
} | ||
|
||
/** @inheritDoc */ | ||
export interface HelixPaginatedChannelSearchFilter extends HelixChannelSearchFilter, HelixForwardPagination {} | ||
|
||
/** | ||
* The Helix API methods that run searches. | ||
* | ||
* Can be accessed using `client.helix.search` on an {@ApiClient} instance. | ||
* | ||
* ## Example | ||
* ```ts | ||
* const api = new ApiClient(new StaticAuthProvider(clientId, accessToken)); | ||
* const channels = await api.helix.search.searchChannels('pear'); | ||
* ``` | ||
*/ | ||
export class HelixSearchApi extends BaseApi { | ||
/** | ||
* Search categories/games for an exact or partial match. | ||
* | ||
* @param query The search term. | ||
* @param pagination | ||
* | ||
* @expandParams | ||
*/ | ||
async searchCategories( | ||
query: string, | ||
pagination: HelixForwardPagination = {} | ||
): Promise<HelixPaginatedResult<HelixGame>> { | ||
const result = await this._client.callApi<HelixPaginatedResponse<HelixGameData>>({ | ||
type: TwitchApiCallType.Helix, | ||
url: 'search/categories', | ||
query: { | ||
query, | ||
...makePaginationQuery(pagination) | ||
} | ||
}); | ||
|
||
return createPaginatedResult(result, HelixGame, this._client); | ||
} | ||
|
||
/** | ||
* Creates a paginator for a category/game search. | ||
* | ||
* @param query The search term. | ||
*/ | ||
searchCategoriesPaginated(query: string): HelixPaginatedRequest<HelixGameData, HelixGame> { | ||
return new HelixPaginatedRequest<HelixGameData, HelixGame>( | ||
{ | ||
url: 'search/categories', | ||
query: { | ||
query | ||
} | ||
}, | ||
this._client, | ||
data => new HelixGame(data, this._client) | ||
); | ||
} | ||
|
||
/** | ||
* Search channels for an exact or partial match. | ||
* | ||
* @param query The search term. | ||
* @param filter | ||
* | ||
* @expandParams | ||
*/ | ||
async searchChannels( | ||
query: string, | ||
filter: HelixPaginatedChannelSearchFilter = {} | ||
): Promise<HelixPaginatedResult<HelixUser>> { | ||
const result = await this._client.callApi<HelixPaginatedResponse<HelixUserData>>({ | ||
type: TwitchApiCallType.Helix, | ||
url: 'search/channels', | ||
query: { | ||
query, | ||
live_only: filter.liveOnly?.toString(), | ||
...makePaginationQuery(filter) | ||
} | ||
}); | ||
|
||
return createPaginatedResult(result, HelixUser, this._client); | ||
} | ||
|
||
/** | ||
* Creates a paginator for a channel search. | ||
* | ||
* @param query The search term. | ||
* @param filter | ||
* | ||
* @expandParams | ||
*/ | ||
searchChannelsPaginated( | ||
query: string, | ||
filter: HelixChannelSearchFilter = {} | ||
): HelixPaginatedRequest<HelixUserData, HelixUser> { | ||
return new HelixPaginatedRequest<HelixUserData, HelixUser>( | ||
{ | ||
url: 'search/channels', | ||
query: { | ||
query, | ||
live_only: filter.liveOnly?.toString() | ||
} | ||
}, | ||
this._client, | ||
data => new HelixUser(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.