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 | 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 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Cookie, ICookie } from '../../cookie' import { HoyoAPIError } from '../../error' import { Language, LanguageEnum } from '../../language' import { HTTPRequest } from '../../request' import { GAME_RECORD_CARD_API, USER_GAMES_LIST } from '../../routes' import { GamesEnum, IGame, IGameRecordCard, IGamesList, IHoyolabOptions, } from './hoyolab.interface' /** * Represents the Hoyolab API client. * * @class * @category Main */ export class Hoyolab { /** * The parsed ICookie object used to authenticate requests. */ readonly cookie: ICookie /** * The underlying `Request` object used to make HTTP requests. */ readonly request: HTTPRequest /** * The language used for API responses. */ public lang: LanguageEnum /** * Creates a new instance of `Hoyolab`. * * @constructor * @param {IHoyolabOptions} options - The options to initialize the `Hoyolab` instance. * @throws {HoyoAPIError} If `ltuid` or `ltoken` keys are missing in the `ICookie` object. * * @remarks * Because CookieTokenV2 has a short expiration time and cannot be refreshed so far. * It is evident that every few days, when logging in, it always requests authentication first. * Therefore, this method that uses CookieTokenV2 is not suitable if filled statically. */ constructor(options: IHoyolabOptions) { /** * The parsed ICookie object used to authenticate requests. * @type {ICookie} */ const cookie: ICookie = typeof options.cookie === 'string' ? Cookie.parseCookieString(options.cookie) : options.cookie this.cookie = cookie if (!options.lang) { options.lang = Language.parseLang(cookie.mi18nLang) } // Parse language to prevent language error options.lang = Language.parseLang(options.lang) /** * The underlying `Request` object used to make HTTP requests. * @type {Request} */ this.request = new HTTPRequest(Cookie.parseCookie(this.cookie)) this.request.setLang(options.lang) /** * The language used for API responses. * @type {LanguageEnum} */ this.lang = options.lang } /** * Get the list of games on this Hoyolab account. * * @async * @param {GamesEnum} [game] The optional game for which to retrieve accounts. * @throws {HoyoAPIError} Thrown if there are no game accounts on this Hoyolab account. * @returns {Promise<IGame[]>} The list of games on this Hoyolab account. * * @remarks * Because CookieTokenV2 has a short expiration time and cannot be refreshed so far. * It is evident that every few days, when logging in, it always requests authentication first. * Therefore, this method that uses CookieTokenV2 is not suitable if filled statically. */ public async gamesList(game?: GamesEnum): Promise<IGame[]> { if (!this.cookie.cookieTokenV2) { throw new HoyoAPIError( 'You must set options.cookie.cookieTokenV2 to access this API', ) } if (game) { this.request.setQueryParams({ game_biz: game, }) } this.request.setQueryParams({ uid: this.cookie.ltuid, sLangKey: this.cookie.mi18nLang, }) const { response: res, params, body, headers, } = await this.request.send(USER_GAMES_LIST) const data = res.data as IGamesList /* c8 ignore next 5 */ if (!res.data || !data.list) { throw new HoyoAPIError( res.message ?? 'There is no game account on this hoyolab account !', res.retcode, { response: res, request: { body, headers, params, }, }, ) } return data.list as IGame[] } /** * Get the account of a specific game from the games list. * * @async * @param {GamesEnum} game - The game that the account belongs to. * @throws {HoyoAPIError} If there is no game account on this hoyolab account. * @returns {Promise<IGame>} The game account. * * @remarks * Because CookieTokenV2 has a short expiration time and cannot be refreshed so far. * It is evident that every few days, when logging in, it always requests authentication first. * Therefore, this method that uses CookieTokenV2 is not suitable if filled statically. */ public async gameAccount(game: GamesEnum): Promise<IGame> { const games = await this.gamesList(game) /* c8 ignore next 5 */ if (games.length < 1) { throw new HoyoAPIError( 'There is no game account on this hoyolab account !', ) } return games.reduce((first, second) => { return second.level > first.level ? second : first }) } /** * Retrieves the game record card * * @async * @returns {Promise<IGameRecordCard>} The game account. */ async gameRecordCard(): Promise<IGameRecordCard> { /* c8 ignore start */ this.request.setQueryParams({ uid: this.cookie.ltuid || this.cookie.accountId || this.cookie.accountIdV2, }) const { response: res } = await this.request.send(GAME_RECORD_CARD_API) return (res as any).data.list as IGameRecordCard } /* c8 ignore stop */ } |