UNPKG

twitch-js

Version:

Javascript library for the Twitch Messaging Interface.

122 lines (121 loc) 3.5 kB
import { ApiRootResponse } from '../twitch'; import { ApiOptions, ApiReadyStates, ApiFetchOptions } from './types'; /** * Make requests to Twitch API. * * ## Initializing * * ```js * // With a token ... * const token = 'cfabdegwdoklmawdzdo98xt2fo512y' * const { api } = new TwitchJs({ token }) * * // ... or with a client ID ... * const clientId = 'uo6dggojyb8d6soh92zknwmi5ej1q2' * const { api } = new TwitchJs({ clientId }) * ``` * * **Note:** The recommended way to initialize the API client is with a token. * * ## Making requests * * By default, the API client makes requests to the * [Helix API](https://dev.twitch.tv/docs/api), and exposes [[Api.get]], * [[Api.post]] and [[Api.put]] methods. Query string parameters and body * parameters are provided via `options.search` and `options.body` properties, * respectively. * * To make requests to the [Kraken/v5 API](https://dev.twitch.tv/docs/v5), use * `options.version = 'kraken'` * * ### Examples * * #### Get bits leaderboard * ```js * api * .get('bits/leaderboard', { search: { user_id: '44322889' } }) * .then(response => { * // Do stuff with response ... * }) * ``` * * #### Get the latest Overwatch live streams * ``` * api * .get('streams', { version: 'kraken', search: { game: 'Overwatch' } }) * .then(response => { * // Do stuff with response ... * }) * ``` * * #### Start a channel commercial * ``` * const channelId = '44322889' * api * .post(`channels/${channelId}/commercial`, { * version: 'kraken', * body: { length: 30 }, * }) * .then(response => { * // Do stuff with response ... * }) * ``` */ declare class Api { private _options; private _log; private _readyState; private _status; constructor(options: Partial<ApiOptions>); get readyState(): ApiReadyStates; get status(): ApiRootResponse; /** * New client options. To update `token` or `clientId`, use [**api.initialize()**]{@link Api#initialize}. */ updateOptions(options: Partial<ApiOptions>): void; /** * Initialize API client and retrieve status. * @see https://dev.twitch.tv/docs/v5/#root-url */ initialize(newOptions?: Partial<ApiOptions>): Promise<void | ApiRootResponse | Response>; /** * Check if current credentials include `scope`. * @see https://dev.twitch.tv/docs/authentication/#twitch-api-v5 */ hasScope( /** Scope to check */ scope: string): Promise<boolean>; /** * GET endpoint. * * @example <caption>Get Live Overwatch Streams (Kraken)</caption> * ``` * api.get('streams', { version: 'kraken', search: { game: 'Overwatch' } }) * .then(response => { * // Do stuff with response ... * }) * ``` * * @example <caption>Get user follows (Helix)</caption> * ``` * api.get('users/follows', { search: { to_id: '23161357' } }) * .then(response => { * // Do stuff with response ... * }) * ``` */ get<T = any>(endpoint?: string, options?: ApiFetchOptions): Promise<Response | T>; /** * POST endpoint. */ post<T = any>(endpoint: string, options?: ApiFetchOptions): Promise<Response | T>; /** * PUT endpoint. */ put<T = any>(endpoint: string, options?: ApiFetchOptions): Promise<Response | T>; private _isVersionHelix; private _getBaseUrl; private _getHeaders; private _handleFetch; } export default Api;