All files / src/module/daily daily.ts

88.88% Statements 224/252
93.54% Branches 29/31
100% Functions 6/6
88.88% Lines 224/252

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 2531x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x                             6x 6x 6x 6x 2x 2x 6x 6x 4x 4x 4x 4x 4x 4x 4x 4x 4x 6x 6x 4x 4x 6x 6x 4x 4x 6x 6x 4x 4x 6x 6x 6x 60x 60x 60x 60x 60x 60x 60x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x                             12x 12x 12x 12x 8x 8x 12x 12x 4x 12x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 4x 12x 12x 12x 60x 60x 60x 60x 60x 60x 60x 60x 60x 9x 9x 9x 1x 1x 1x 1x 1x 1x 6x 9x 9x 9x 9x 9x 9x 9x 9x 6x 9x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 60x 60x 60x 60x 60x 60x 60x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { GamesEnum } from '../../client/hoyolab'
import { LanguageEnum } from '../../language'
import { HTTPRequest } from '../../request'
import { DAILY_CLAIM_API, DAILY_INFO_API, DAILY_REWARD_API } from '../../routes'
import {
  IDailyClaim,
  IDailyInfo,
  IDailyReward,
  IDailyRewards,
} from './daily.interface'
import { HoyoAPIError } from '../../error'
 
/**
 * DailyModule class provides methods to interact with Genshin Impact's daily module endpoints.
 *
 * @class
 * @internal
 * @category Module
 */
export class DailyModule {
  private dailyInfoUrl: string
  private dailyRewardUrl: string
  private dailySignUrl: string
 
  constructor(
    private request: HTTPRequest,
    private lang: LanguageEnum,
    private game: GamesEnum,
    private region: string | null,
  ) {
    this.dailyInfoUrl = DAILY_INFO_API(game)
    this.dailyRewardUrl = DAILY_REWARD_API(game)
    this.dailySignUrl = DAILY_CLAIM_API(game)
  }
 
  /**
   * Retrieves daily information.
   *
   * @returns {Promise<IDailyInfo>} A promise that resolves to an IDailyInfo object.
   */
  async info(): Promise<IDailyInfo> {
    this.request
      .setQueryParams({
        lang: this.lang,
      })
      .setLang(this.lang)
 
    const {
      response: res,
      body,
      headers,
      params,
    } = await this.request.send(this.dailyInfoUrl)
 
    if (res.retcode !== 0 || !res.data) {
      throw new HoyoAPIError(
        res.message ??
          'Failed to retrieve data, please double-check the provided UID.',
        res.retcode,
        {
          response: res,
          request: {
            body,
            headers,
            params,
          },
        },
      )
    }
 
    const data = res.data as IDailyInfo
 
    if (typeof data?.first_bind === 'undefined' || data?.first_bind === null) {
      data.first_bind = false
    }
 
    if (typeof data.month_last_day === 'undefined') {
      const today = new Date()
      const lastDayOfMonth = new Date(
        today.getFullYear(),
        today.getMonth() + 1,
        0,
      ).getDate()
 
      data.month_last_day = today.getDate() === lastDayOfMonth
    }
 
    if (typeof data.sign_cnt_missed === 'undefined') {
      data.sign_cnt_missed = 0
    }
 
    if (typeof data.short_sign_day === 'undefined') {
      data.short_sign_day = 0
    }
 
    if (data.region === '' && this.region) {
      data.region = this.region
    }
 
    return data
  }
 
  /**
   * Retrieve daily rewards information.
   *
   * @returns {Promise<IDailyRewards>} A promise that resolves to an IDailyRewards object.
   */
  async rewards(): Promise<IDailyRewards> {
    this.request
      .setQueryParams({
        lang: this.lang,
      })
      .setLang(this.lang)
 
    const {
      response: res,
      body,
      headers,
      params,
    } = await this.request.send(this.dailyRewardUrl)
 
    if (res.retcode !== 0 || !res.data) {
      throw new HoyoAPIError(
        res.message ??
          'Failed to retrieve data, please double-check the provided UID.',
        res.retcode,
        {
          response: res,
          request: {
            body,
            headers,
            params,
          },
        },
      )
    }
 
    const data = res.data as IDailyRewards
 
    if (typeof data.now === 'undefined') {
      data.now = Math.round(new Date().getTime() / 1000).toString()
    }
 
    if (this.game === GamesEnum.GENSHIN_IMPACT) {
      data.biz = 'hk4e'
    } else if (this.game === GamesEnum.HONKAI_IMPACT) {
      data.biz = 'hk4e'
    } else if (this.game === GamesEnum.HONKAI_STAR_RAIL) {
      data.biz = 'hkrpg'
      /* c8 ignore next 3 */
    } else {
      data.biz = ''
    }
    /* c8 ignore next 3 */
 
    if (typeof data.resign === 'undefined') {
      data.resign = false
    }
 
    return data
  }
 
  /**
   * Get the daily reward for a specific day or the current day
   *
   * @param {number | null} day - The day to retrieve the reward for. If null, retrieve the reward for the current day.
   * @returns {Promise<IDailyReward>} - A promise that resolves with the daily reward for the specified day or the current day
   * @throws {HoyoAPIError} - If the specified day is not a valid date in the current month or if the reward for the specified day is undefined.
   */
  async reward(day: number | null = null): Promise<IDailyReward> {
    const response = await this.rewards()
 
    if (day === null) {
      /* c8 ignore start */
      const now = response?.now
        ? new Date(parseInt(response.now) * 1000)
        : new Date()
      day = now.getDate()
      /* c8 ignore stop */
    }
 
    const date = new Date()
    const year = date.getFullYear()
    const month = date.getMonth()
    const daysInMonth = new Date(year, month + 1, 0).getDate()
 
    if (
      !(day > 0 && day <= daysInMonth) ||
      typeof response.awards[day - 1] === undefined
    ) {
      throw new HoyoAPIError(`${day} is not a valid date in this month.`)
    }
 
    return {
      month: response.month,
      now: response.now,
      biz: response.biz,
      resign: response.resign,
      award: response.awards[day - 1],
    }
  }
 
  /**
   * Claim the daily rewards.
   *
   * @returns {Promise<IDailyClaim>} The claim information.
   */
  async claim(): Promise<IDailyClaim> {
    this.request
      .setQueryParams({
        lang: this.lang,
      })
      .setLang(this.lang)
 
    const { response } = await this.request.send(this.dailySignUrl, 'POST', 0)
 
    const info = await this.info()
    const reward = await this.reward()
 
    /* c8 ignore start */
    if (response.retcode === -5003) {
      return {
        status: response.message,
        code: -5003,
        reward,
        info,
      }
    }
 
    if (
      (response.data as IDailyClaim | undefined)?.code
        .toString()
        .toLowerCase() === 'ok' &&
      response.retcode === 0
    ) {
      return {
        status: response.message,
        code: 0,
        reward,
        info,
      }
    }
 
    return {
      status: response.message,
      code: response.retcode,
      reward: null,
      info,
    }
    /* c8 ignore start */
  }
}