module import { resolveCname } from 'dns' ; import XMLHttpRequestPromise , { XHRResponse } from '../libs/xhr-promise' ; import Util from './util' ; /**
 * A single user object as from  getTwitchUsers .
 *
 *  typedef TwitchUser property { string } _id property { string } bio property { string } created_at see https property property property property property export interface TwitchUser { _id : string ; bio : string ; created_at : string ; display_name : string ; logo : string ; name : string ; type : string ; updated_at : string ; } /**
 * A single user object as from  getTwitchUsersByID .
 *
 *  typedef HelixTwitchUser property { string } id property { string } login property { string } description property { string } display_name property { string } profile_image_url property { string } offline_image_url property { string } type property { string } broadcaster_type property { int } view_count export interface HelixTwitchUser { id : string ; login : string ; description : string ; display_name : string ; profile_image_url : string ; offline_image_url : string ; type : string ; broadcaster_type : string ; view_count : number ; } /**
 * A single good object as from  getUserGoods .
 *
 *  typedef ExtensionGood property { string } next_instruction property { string } receipt_id property { string } sku /**
 * A receipt detailing which good's fulfillment status needs to be set. Used as a parameter for
 *  updateFulfilledGoods .
 *
 *  typedef Receipt property { string } fulfillment_address property { string } receipt_id - Receipt ID for the digital good, returned by  getUserGoods property { string } last_instruction - The last thing you did. Corresponds to the "next
 * instruction" for the purchase returned by  getUserGoods . Value value: `FULFILL`.
  /**
 * Provides a convenient interface for Twitch API requests with an automatically set and updated
 * extension client id.
 *
 * Should not normally be created directly, instead an instance is made available
 * and namespaced appropriately when using  Muxy . TwitchClient .
 *
 *  private example export default class TwitchClient { public extensionId : string ; public promise : Promise < any > ; /**
     * Create an instance of TwitchClient bound to the provided client ID.
     *
     * Prefer  Muxy . TwitchClient instead.
     *
     *  since ignore param { string } clientID constructor ( clientID ) { ignore this . extensionId = clientID ; ignore this . promise = ( Promise as any ) . resolve ( ) ; } since public return { Promise } public loaded ( ) { return this . promise ; } async since ignore param { string } method param { string } endpoint param { string ? } data param { Object } JWT return { Promise } public signedTwitchRequest ( method , endpoint , data ? , JWT ? ) { const headers = { Accept : 'application/vnd.twitchtv.v5+json' , 'Client-ID' : this . extensionId , Authorization : undefined } ; if ( JWT ) { headers . Authorization = `Bearer ${ JWT }` ; } return new Promise ( ( resolve , reject ) => { const xhrPromise = new XMLHttpRequestPromise ( { data , headers , method , url : `https://api.twitch.tv/kraken/${ endpoint }` } ) ; return xhrPromise . send ( ) . catch ( reject ) . then ( ( resp ) => { if ( resp . status < 400 ) { resolve ( resp . responseText ) ; } reject ( resp . responseText ) ; } ) ; } ) ; } async ignore param { string } method param { string } endpoint param { string ? } data param { Object } helixToken return { Promise } public signedTwitchHelixRequest ( method , endpoint , data ? , helixToken ? ) { const headers = { Authorization : undefined , 'Client-ID' : this . extensionId } ; if ( helixToken ) { headers . Authorization = `Extension ${ helixToken }` ; } return new Promise ( ( resolve , reject ) => { const xhrPromise = new XMLHttpRequestPromise ( { data , headers , method , url : `https://api.twitch.tv/helix/${ endpoint }` } ) ; return xhrPromise . send ( ) . then ( ( resp ) => { if ( resp . status < 400 ) { try { if ( resp . responseText . hasOwnProperty ( 'data' ) ) { const anyResp = resp . responseText as any ; const r = anyResp . data ; resolve ( r ) ; } else { resolve ( resp . responseText ) ; } } catch ( err ) { reject ( 'Unexpected response from Twitch' ) ; } } reject ( resp . responseText ) ; } ) . catch ( reject ) ; } ) ; } async ignore param { string } method param { string } endpoint param { string ? } data param { Object } JWT return { Promise } public signedTwitchExtensionRequest ( method , endpoint , data ? , JWT ? ) { const headers = { 'Client-ID' : this . extensionId , Authorization : undefined } ; if ( JWT ) { headers . Authorization = `Bearer ${ JWT }` ; } return new Promise ( ( resolve , reject ) => { const xhrPromise = new XMLHttpRequestPromise ( { data , headers , method , url : `https://api.twitch.tv/extensions/${ endpoint }` } ) ; return xhrPromise . send ( ) . catch ( reject ) . then ( ( resp ) => { if ( resp . status < 400 ) { resolve ( resp . responseText ) ; } reject ( resp . responseText ) ; } ) ; } ) ; } async since throws param { [ ] string return { Promise < [ ] TwitchUser>} Resolves with a list of  TwitchUser * objects for each of the usernames provided.
     *
     *  example public getTwitchUsers ( usernames ) { if ( usernames . length === 0 ) { return Promise . resolve ( [ ] ) ; } return this . signedTwitchRequest ( 'GET' , `users?login=${ usernames . join ( ',' ) }` ) ; } async throws param { [ ] string return { Promise < [ ] HelixTwitchUser>} Resolves with a list of  HelixTwitchUser * objects for each of the user IDs provided.
     *
     *  example public getTwitchUsersByID ( userIDs ) { if ( userIDs . length === 0 ) { return Promise . resolve ( [ ] ) ; } return this . signedTwitchHelixRequest ( 'GET' , `users?id=${ userIDs . join ( ',' ) }` ) ; } /**
     * Monetization
     */ param { Object } jwt return { Promise < [ ] ExtensionGood>} Resolves with a list of  ExtensionGood objects for
     * each of the goods the user is entitled to.
      public getUserGoods ( jwt ) { return this . signedTwitchRequest ( 'POST' , 'commerce/user/goods' , '{}' , jwt ) ; } param { Object } jwt param { [ ] Receipt } receipts - List of  Receipt objects detailing which goods need to be
     * updated.
     *
     *  return { Promise < [ ] public updateFulfilledGoods ( jwt , receipts ) { return this . signedTwitchRequest ( 'POST' , 'commerce/user/goods/fulfill' , receipts , jwt ) ; } param jwt param configurationString public setExtensionRequiredConfiguration ( jwt , configurationString ) { const environment = Util . getTwitchEnvironment ( ) ; const data = { required_configuration : configurationString } ; const token = Util . extractJWTInfo ( jwt ) ; return this . signedTwitchExtensionRequest ( 'PUT' , `${ this . extensionId }/${ environment . version }/required_configuration?channel_id=${ token . channel_id }` , JSON . stringify ( data ) , jwt ) ; } }