All files / apis/lol/spectator spectator.ts

93.33% Statements 14/15
100% Branches 4/4
66.67% Functions 2/3
93.33% Lines 14/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 441x     1x     1x   1x   2x 2x 1x                               2x     2x 2x   2x 1x 1x     1x        
import { NOT_FOUND } from 'http-status-codes'
import { Regions } from '../../../constants'
import { FeaturedGamesDTO } from '../../../models-dto/spectator/featured-games.dto'
import { endpointsV4 } from '../../../endpoints/endpoints'
import { CurrentGameInfoDTO } from '../../../models-dto/spectator/current-game-info.dto'
import { SpectatorNotAvailableDTO } from '../../../models-dto/spectator/spectator-not-available.dto'
import { BaseApiLol } from '../base/base.api.lol'
 
export class SpectatorApi extends BaseApiLol {
  private errorHandler (e: any) {
    const { statusCode } = e.error || e
    if (statusCode !== NOT_FOUND) {
      throw e
    }
  }
  /**
   * Get featured games
   * @param region
   */
  public async featuredGames (region: Regions) {
    return this.request<FeaturedGamesDTO>(region, endpointsV4.SpectatorFeaturedGames)
  }
  /**
   * Get summoner active game
   * @param encryptedSummonerId
   * @param region
   */
  public async activeGame (encryptedSummonerId: string, region: Regions) {
    const params = {
      encryptedSummonerId
    }
    try {
      return await this.request<CurrentGameInfoDTO>(region, endpointsV4.SpectatorSummoner, params)
    } catch (e) {
      this.errorHandler(e)
      const message = 'No active game found'
      const response: SpectatorNotAvailableDTO = {
        message
      }
      return response
    }
  }
}