fair-twitch
Version:
Fair's Twitch API and Chat bot library
136 lines (135 loc) • 6.43 kB
TypeScript
import request from 'request';
import ExpandedEventEmitter from './ExpandedEventEmitter';
interface APIOptionsParam {
/** Your API application client id */
clientID?: string;
/** Your API application client secret */
clientSecret?: string;
/** Your API application redirect Oauth URL */
redirectURL?: string;
/**
* Refresh token if you need to do authorized requests.
* It will use this to generate new accessTokens if it has none or they become expired.
*/
refreshToken?: string;
/** Your access token. If you already have an access token refresh */
accessToken?: string;
/**
* The timestamp of when the access token expires.
* Requests after this timestamp will cause a token refresh first, given that autoRefreshToken is true.
* Defaults to null (will not refresh token based on expired).
*/
accessTokenExpireTS?: number;
/** If the api should automatically refresh access token if getting an unauthorized error. Defaults to true */
autoRefreshToken?: boolean;
/** If should validate token on construction. Defaults to true */
validateToken?: boolean;
/** The Twitch API url. Defaults to "https://api.twitch.tv/" */
apiURL?: string;
/** The Twitch authentication url. Defaults to "https://id.twitch.tv/" */
authURL?: string;
}
interface APIOptions {
clientID: string | null;
clientSecret: string | null;
redirectURL: string | null;
refreshToken: string | null;
accessToken: string | null;
accessTokenExpireTS: number | null;
autoRefreshToken: boolean;
validateToken: boolean;
apiURL: string;
authURL: string;
}
declare type RequestCallback = (err: any, data?: {} | any, res?: request.Response, body?: any) => void;
interface RequestOptionsMethod extends RequestOptions {
method: 'GET' | 'POST' | 'DELETE' | 'PUT' | 'HEAD';
}
interface RequestOptions {
/** The API endpoint. Example: "streams" */
url: string;
/** The base URL to use. Defaults to constructor option apiURL */
baseURL?: string;
/** Replacement clientID, use null to not set. Defaults to constructor option clientID */
clientID?: string;
/** Replacement accessToken, use null to not set. Defaults to constructor option accessToken */
accessToken?: string;
/** The access token prefix. Defaults to "OAuth" for kraken calls and "Bearer" for other */
accessTokenPrefix?: string;
}
interface TwitchAPI {
addListener(event: string, listener: (...args: any[]) => void): this;
/** When an error has happened */
addListener(event: 'error', listener: (error: any) => void): this;
/** When a debug message happens */
addListener(event: 'debug', listener: (...message: any) => void): this;
/** When the access token has been validated */
addListener(event: 'tokenvalidate', listener: (data: any) => void): this;
/** When the access token has been refreshed */
addListener(event: 'tokenrefresh', listener: (data: any) => void): this;
on(event: string, listener: (...args: any[]) => void): this;
/** When an error has happened */
on(event: 'error', listener: (error: any) => void): this;
/** When a debug message happens */
on(event: 'debug', listener: (...message: any) => void): this;
/** When the access token has been validated */
on(event: 'tokenvalidate', listener: (data: any) => void): this;
/** When the access token has been refreshed */
on(event: 'tokenrefresh', listener: (data: any) => void): this;
once(event: string, listener: (...args: any[]) => void): this;
/** When an error has happened */
once(event: 'error', listener: (error: any) => void): this;
/** When a debug message happens */
once(event: 'debug', listener: (...message: any) => void): this;
/** When the access token has been validated */
once(event: 'tokenvalidate', listener: (data: any) => void): this;
/** When the access token has been refreshed */
once(event: 'tokenrefresh', listener: (data: any) => void): this;
emit(event: string, ...args: any[]): boolean;
emit(event: 'error', error: any): boolean;
emit(event: 'debug', ...message: any): boolean;
emit(event: 'tokenvalidate', data: any): boolean;
emit(event: 'tokenrefresh', data: any): boolean;
}
declare class TwitchAPI extends ExpandedEventEmitter {
options: APIOptions;
tokenData: any;
constructor(options?: APIOptionsParam | string);
private _getAuthHeaders;
private _jsonTwitchBody;
/**
* Will refresh the internal access token.
* Requires clientID, clientSecret and refreshToken in constructor options.
* @param callback Callback for when the access token has been refreshed
*/
refreshAccessToken(callback?: (err: any) => void): void;
request(options: RequestOptionsMethod, callback?: RequestCallback, recursive?: boolean): void;
/**
* Runs an oauth validate access token from Twitch
* @param callback The callback for the Twitch data sent back
*/
validateAccessToken(callback?: (err: any, data?: any) => void): void;
get(urlOrOptions: RequestOptions | string, callback?: RequestCallback): void;
post(urlOrOptions: RequestOptions | string, callback?: RequestCallback): void;
delete(urlOrOptions: RequestOptions | string, callback?: RequestCallback): void;
put(urlOrOptions: RequestOptions | string, callback?: RequestCallback): void;
/**
* Get OAuth Authorization Code Flow url that clients need to login with.
* Remember to add state query to the url.
* Requires clientID and redirectURL in construction parameters
* When the user has logged in, they will be redirected to:
* https://<your registered redirect URI>/?code=<authorization code>
* You can then use that code to get a refresh token with:
* getRefreshToken(<authorization code>, callback..)
* @param scopes List of scopes to be in authorization url
*/
getAuthenticationURL(...scopes: string[]): string;
/**
* Get a refresh token from an authorization process.
* Use getAuthenticationURL() to get the url the user needs to login at.
* @param authCode The authorization code gotten from redirect URL query
* @param callback Callback for the information Twitch sends back
*/
getRefreshToken(authCode: string, callback: RequestCallback): void;
}
export = TwitchAPI;