UNPKG

@yeci226/hoyoapi

Version:

HoYoAPI is an unofficial API Wrapper library developed to facilitate communication with the official HoYoLab API.

682 lines (675 loc) 19.7 kB
import { DiaryEnum, DiaryMonthEnum, GenshinDiaryModule, GenshinImpact, GenshinRecordModule, GenshinRegion, GenshinTCGModule, SpiralAbyssScheduleEnum, getGenshinRegion } from "./chunk-3RU7DQPZ.mjs"; import { HIRecordModule, HonkaiImpact, HonkaiRegion, getHi3Region } from "./chunk-RNRBWFTV.mjs"; import { ForgottenHallModeEnum, ForgottenHallScheduleEnum, HSRDiaryEnum, HSRDiaryModule, HSRDiaryMonthEnum, HSRRecordModule, HonkaiStarRail, HsrRegion, getHsrRegion } from "./chunk-KLRYDGT3.mjs"; import { DailyModule, RedeemModule } from "./chunk-TQVUBJ73.mjs"; import { ACCOUNT_API, BBS_API, Cookie, DAILY_CLAIM_API, DAILY_INFO_API, DAILY_REWARD_API, DEFAULT_REFERER, GAME_RECORD_CARD_API, GENSHIN_DIARY_DETAIL_API, GENSHIN_DIARY_LIST_API, GENSHIN_RECORD_AVATAR_BASIC_INFO_API, GENSHIN_RECORD_CHARACTER_API, GENSHIN_RECORD_DAILY_NOTE_API, GENSHIN_RECORD_INDEX_API, GENSHIN_RECORD_SPIRAL_ABYSS_API, GENSHIN_REDEEM_CLAIM_API, GENSHIN_TCG_BASICINFO, GENSHIN_TCG_CARDLIST, GENSHIN_TCG_CHALLANGE_DECK, GENSHIN_TCG_CHALLANGE_RECORD, GENSHIN_TCG_CHALLANGE_SCHEDULE, GENSHIN_TCG_MATCHLIST, GamesEnum, HI_RECORD_ABYSS_API, HI_RECORD_ARENA_API, HI_RECORD_CHARACTER_API, HI_RECORD_ELYSIAN_API, HI_RECORD_INDEX_API, HI_REDEEM_CLAIM_API, HK4E_API, HKRPG_API, HSR_DIARY_DETAIL_API, HSR_DIARY_LIST_API, HSR_RECORD_APOCALYPSE_PHANTOM_API, HSR_RECORD_CHARACTER_API, HSR_RECORD_FORGOTTEN_HALL_API, HSR_RECORD_INDEX_API, HSR_RECORD_NOTE_API, HSR_RECORD_PURE_FICTION_API, HSR_RECORD_WIDGET_API, HSR_REDEEM_CLAIM_API, HTTPRequest, HoyoAPIError, Hoyolab, Language, LanguageEnum, NAP_API, PUBLIC_API, TAKUMI_API, USER_GAMES_LIST, ZZZ_BANBOO_API, ZZZ_RECORD_CHARACTER_API, ZZZ_RECORD_CHARACTER_LIST_API, ZZZ_RECORD_DEADLY_ASSAULT_API, ZZZ_RECORD_INDEX_API, ZZZ_RECORD_NOTE_API, ZZZ_RECORD_SHIYU_DEFENSE_API, ZZZ_REDEEM_CLAIM_API, __publicField } from "./chunk-WTPSN6GP.mjs"; // src/client/zzz/zzz.interface.ts var ZZZRegion = /* @__PURE__ */ ((ZZZRegion2) => { ZZZRegion2["USA"] = "prod_gf_us"; ZZZRegion2["EUROPE"] = "prod_gf_eu"; ZZZRegion2["ASIA"] = "prod_gf_jp"; ZZZRegion2["CHINA_TAIWAN"] = "prod_gf_sg"; return ZZZRegion2; })(ZZZRegion || {}); // src/client/zzz/zzz.helper.ts function getZZZRegion(uid) { const server_region = Number(uid.toString().trim().slice(0, 2)); let key; switch (server_region) { case 10: key = "USA"; break; case 15: key = "EUROPE"; break; case 13: key = "ASIA"; break; case 17: key = "CHINA_TAIWAN"; break; default: throw new HoyoAPIError("Given UID ".concat(uid, " is invalid !")); } return ZZZRegion[key]; } // src/client/zzz/record/record.enum.ts var ShiyuDefenseScheduleEnum = /* @__PURE__ */ ((ShiyuDefenseScheduleEnum2) => { ShiyuDefenseScheduleEnum2[ShiyuDefenseScheduleEnum2["CURRENT"] = 1] = "CURRENT"; ShiyuDefenseScheduleEnum2[ShiyuDefenseScheduleEnum2["PREVIOUS"] = 2] = "PREVIOUS"; return ShiyuDefenseScheduleEnum2; })(ShiyuDefenseScheduleEnum || {}); var DeadlyAssaultScheduleEnum = /* @__PURE__ */ ((DeadlyAssaultScheduleEnum2) => { DeadlyAssaultScheduleEnum2[DeadlyAssaultScheduleEnum2["CURRENT"] = 1] = "CURRENT"; DeadlyAssaultScheduleEnum2[DeadlyAssaultScheduleEnum2["PREVIOUS"] = 2] = "PREVIOUS"; return DeadlyAssaultScheduleEnum2; })(DeadlyAssaultScheduleEnum || {}); // src/client/zzz/record/record.ts var ZZZRecordModule = class { /** * Creates an instance of ZZZRecordModule. * * @param request The HTTPRequest object used for making API requests. * @param lang The language enum value. * @param region The region string or null if not provided. * @param uid The UID number or null if not provided. */ constructor(request, lang, region, uid) { this.request = request; this.lang = lang; this.region = region; this.uid = uid; } /** * Retrieves the characters associated with the provided region and UID. * * @returns {Promise<IZZZCharacterFull[]>} A Promise that resolves to an array of full ZZZ characters. * @throws {HoyoAPIError} if the region or UID parameters are missing or failed to be filled. * @throws {HoyoAPIError} if failed to retrieve data, please double-check the provided UID. */ async characters() { var _a; if (!this.region || !this.uid) { throw new HoyoAPIError("UID parameter is missing or failed to be filled"); } this.request.setQueryParams({ server: this.region, role_id: this.uid, lang: this.lang }).setDs(true); const { response: res, body, params, headers } = await this.request.send(ZZZ_RECORD_CHARACTER_LIST_API); if (res.retcode !== 0) { throw new HoyoAPIError( (_a = res.message) != null ? _a : "Failed to retrieve data, please double-check the provided UID.", res.retcode, { response: res, request: { body, headers, params } } ); } const data = res.data; return data.avatar_list; } /** * Retrieves the character associated with the provided region and UID. * * @param characterId Character ID to retrieve. * @returns {Promise<IZZZCharacterFull>} A Promise that resolves to a ZZZ character. * @throws {HoyoAPIError} if the region or UID parameters are missing or failed to be filled. * @throws {HoyoAPIError} if the character ID parameter is missing or failed to be filled. * @throws {HoyoAPIError} if failed to retrieve data, please double-check the provided UID. */ async character(characterId) { var _a; if (!this.region || !this.uid) { throw new HoyoAPIError("UID parameter is missing or failed to be filled"); } if (!characterId) { throw new HoyoAPIError( "Character ID parameter is missing or failed to be filled" ); } this.request.setQueryParams({ server: this.region, role_id: this.uid, lang: this.lang }).setDs(true); const { response: res, body, params, headers } = await this.request.send( ZZZ_RECORD_CHARACTER_API + "?id_list[]=" + characterId ); if (res.retcode !== 0) { throw new HoyoAPIError( (_a = res.message) != null ? _a : "Failed to retrieve data, please double-check the provided UID.", res.retcode, { response: res, request: { body, headers, params } } ); } const data = res.data; return data.avatar_list; } /** * Retrieves the records associated with the provided region and UID. * * @returns {Promise<IZZZRecord>} A Promise that resolves to the ZZZ record object. * @throws {HoyoAPIError} if the region or UID parameters are missing or failed to be filled. * @throws {HoyoAPIError} if failed to retrieve data, please double-check the provided UID. */ async records() { var _a; if (!this.region || !this.uid) { throw new HoyoAPIError("UID parameter is missing or failed to be filled"); } this.request.setQueryParams({ server: this.region, role_id: this.uid, lang: this.lang }).setDs(true); const { response: res, body, params, headers } = await this.request.send(ZZZ_RECORD_INDEX_API); if (res.retcode !== 0) { throw new HoyoAPIError( (_a = res.message) != null ? _a : "Failed to retrieve data, please double-check the provided UID.", res.retcode, { response: res, request: { body, headers, params } } ); } return res.data; } /** * Retrieves the note associated with the provided region and UID. * * @returns {Promise<IZZZNote>} A Promise that resolves to the ZZZ note object. * @throws {HoyoAPIError} if the region or UID parameters are missing or failed to be filled. * @throws {HoyoAPIError} if failed to retrieve data, please double-check the provided UID. */ async note() { var _a; if (!this.region || !this.uid) { throw new HoyoAPIError("UID parameter is missing or failed to be filled"); } this.request.setQueryParams({ server: this.region, role_id: this.uid, lang: this.lang }).setDs(true); const { response: res, body, params, headers } = await this.request.send(ZZZ_RECORD_NOTE_API); if (res.retcode !== 0) { throw new HoyoAPIError( (_a = res.message) != null ? _a : "Failed to retrieve data, please double-check the provided UID.", res.retcode, { response: res, request: { body, headers, params } } ); } return res.data; } /** * Retrieves the shiyu defense information associated with the provided region and UID. * * @param scheduleType The schedule type for the shiyu defense (optional, defaults to CURRENT). * @returns {Promise<IZZZShiyuDefense>} A Promise that resolves to the shiyu defense information object. * @throws {HoyoAPIError} if the region or UID parameters are missing or failed to be filled. * @throws {HoyoAPIError} if the given scheduleType parameter is invalid. * @throws {HoyoAPIError} if failed to retrieve data, please double-check the provided UID. */ async shiyuDefense(scheduleType = 1 /* CURRENT */) { var _a; if (!this.region || !this.uid) { throw new HoyoAPIError("UID parameter is missing or failed to be filled"); } if (Object.values(ShiyuDefenseScheduleEnum).includes(scheduleType) === false) { throw new HoyoAPIError("The given scheduleType parameter is invalid !"); } this.request.setQueryParams({ server: this.region, role_id: this.uid, schedule_type: scheduleType, lang: this.lang, need_all: "true" }).setDs(); const { response: res, body, params, headers } = await this.request.send(ZZZ_RECORD_SHIYU_DEFENSE_API); if (res.retcode !== 0) { throw new HoyoAPIError( (_a = res.message) != null ? _a : "Failed to retrieve data, please double-check the provided UID.", res.retcode, { response: res, request: { body, headers, params } } ); } return res.data; } /** * Retrieves the deadly assault information associated with the provided region and UID. * * @param scheduleType The schedule type for the deadly assault (optional, defaults to CURRENT). * @returns {Promise<IZZZDeadlyAssault>} A Promise that resolves to the deadly assault information object. * @throws {HoyoAPIError} if the region or UID parameters are missing or failed to be filled. * @throws {HoyoAPIError} if the given scheduleType parameter is invalid. * @throws {HoyoAPIError} if failed to retrieve data, please double-check the provided UID. */ async deadlyAssault(scheduleType = 1 /* CURRENT */) { var _a; if (!this.region || !this.uid) { throw new HoyoAPIError("UID parameter is missing or failed to be filled"); } if (Object.values(DeadlyAssaultScheduleEnum).includes(scheduleType) === false) { throw new HoyoAPIError("The given scheduleType parameter is invalid !"); } this.request.setQueryParams({ region: this.region, uid: this.uid, schedule_type: scheduleType, lang: this.lang, need_all: "true" }).setDs(); const { response: res, body, params, headers } = await this.request.send(ZZZ_RECORD_DEADLY_ASSAULT_API); if (res.retcode !== 0) { throw new HoyoAPIError( (_a = res.message) != null ? _a : "Failed to retrieve data, please double-check the provided UID.", res.retcode, { response: res, request: { body, headers, params } } ); } return res.data; } }; // src/client/zzz/zzz.ts var ZenlessZoneZero = class _ZenlessZoneZero { /** * Constructs a new `ZZZ` object. * * @param options The options object used to configure the object. */ constructor(options) { /** * The `DailyModule` object provides an interface to interact with the daily check-in feature in Zenless Zone Zero. * */ __publicField(this, "daily"); /** * The `RedeemModule` object provides an interface to interact with the code redemption feature in Zenless Zone Zero. * */ __publicField(this, "redeem"); /** * The `ZZZRecordModule` object provides an interface to interact with the user record feature in Honkai Star Rails. * */ __publicField(this, "record"); /** * HoyYolab account object * */ __publicField(this, "_account", null); /** * The cookie object to be used in requests. */ __publicField(this, "cookie"); /** * The `Request` object used to make requests. */ __publicField(this, "request"); /** * The UID of the user, if available. */ __publicField(this, "uid"); /** * The region of the user, if available. */ __publicField(this, "region"); /** * The language to be used in requests. */ __publicField(this, "lang"); var _a; const cookie = typeof options.cookie === "string" ? Cookie.parseCookieString(options.cookie) : options.cookie; this.cookie = cookie; if (!options.lang) { options.lang = Language.parseLang(cookie.mi18nLang); } options.lang = Language.parseLang(options.lang); this.request = new HTTPRequest(Cookie.parseCookie(this.cookie)); this.request.setReferer(DEFAULT_REFERER); this.request.setLang(options.lang); this.uid = (_a = options.uid) != null ? _a : null; this.region = this.uid !== null ? getZZZRegion(this.uid) : null; this.lang = options.lang; this.daily = new DailyModule( this.request, this.lang, "nap_global" /* ZENLESS_ZONE_ZERO */, this.region ); this.redeem = new RedeemModule( this.request, this.lang, "nap_global" /* ZENLESS_ZONE_ZERO */, this.region, this.uid ); this.record = new ZZZRecordModule( this.request, this.lang, this.region, this.uid ); } /** * Create a new instance of the ZenlessZoneZero class asynchronously. * * @param options The options object used to configure the object. * @throws {HoyoAPIError} Error Wnen the CookieTokenV2 is not set. * @returns {Promise<ZenLessZoneZero>} A promise that resolves with a new ZZZ instance. * * @remarks * If an object is instantiated from this method but options.cookie.cookieTokenV2 is not set, * it will throw an error. This method will access an Endpoint that contains a list of game accounts, * which requires the cookieTokenV2 option. * @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. */ static async create(options) { try { let game = null; if (typeof options.uid === "undefined") { const hoyolab = new Hoyolab({ cookie: options.cookie }); game = await hoyolab.gameAccount("nap_global" /* ZENLESS_ZONE_ZERO */); options.uid = parseInt(game.game_uid); } const zzz = new _ZenlessZoneZero(options); zzz.account = game; return zzz; } catch (error) { throw new HoyoAPIError(error.message, error.code); } } /** * Setter for the account property. Prevents from changing the value once set * @param game The game object to set as the account. */ set account(game) { if (this.account === null && game !== null) { this._account = game; } } /** * Getter for the account property. * @returns {IGame | null} The current value of the account property. */ get account() { return this._account; } /** * Retrieves daily information. * * @alias {@link ZenLessZoneZero.daily | ZZZ.daily.info()} * @deprecated Use through {@link ZenLessZoneZero.daily | ZZZ.daily.info()} instead */ dailyInfo() { return this.daily.info(); } /** * Retrieve daily rewards information. * * @alias {@link ZenLessZoneZero.daily | ZZZ.daily.rewards()} * @deprecated Use through {@link ZenLessZoneZero.daily | ZZZ.daily.rewards()} instead */ dailyRewards() { return this.daily.rewards(); } /** * Get the daily reward for a specific day or the current day * * @param day number | null * @alias {@link ZenLessZoneZero.daily | ZZZ.daily.reward()} * @deprecated Use through {@link ZenLessZoneZero.daily | ZZZ.daily.reward()} instead */ dailyReward(day = null) { return this.daily.reward(day); } /** * Claim current reward * * @alias {@link ZenLessZoneZero.daily | ZZZ.daily.claim()} * @deprecated Use through {@link ZenLessZoneZero.daily | ZZZ.daily.claim()} instead */ dailyClaim() { return this.daily.claim(); } /** * Redeems a code for a specific account. * * @param code string * @alias {@link ZenLessZoneZero.daily | ZZZ.redeem.claim()} * @deprecated Use through {@link ZenLessZoneZero.daily | ZZZ.redeem.claim()} instead */ redeemCode(code) { return this.redeem.claim(code); } }; export { ACCOUNT_API, BBS_API, Cookie, DAILY_CLAIM_API, DAILY_INFO_API, DAILY_REWARD_API, DEFAULT_REFERER, DailyModule, DiaryEnum, DiaryMonthEnum, ForgottenHallModeEnum, ForgottenHallScheduleEnum, GAME_RECORD_CARD_API, GENSHIN_DIARY_DETAIL_API, GENSHIN_DIARY_LIST_API, GENSHIN_RECORD_AVATAR_BASIC_INFO_API, GENSHIN_RECORD_CHARACTER_API, GENSHIN_RECORD_DAILY_NOTE_API, GENSHIN_RECORD_INDEX_API, GENSHIN_RECORD_SPIRAL_ABYSS_API, GENSHIN_REDEEM_CLAIM_API, GENSHIN_TCG_BASICINFO, GENSHIN_TCG_CARDLIST, GENSHIN_TCG_CHALLANGE_DECK, GENSHIN_TCG_CHALLANGE_RECORD, GENSHIN_TCG_CHALLANGE_SCHEDULE, GENSHIN_TCG_MATCHLIST, GamesEnum, GenshinDiaryModule, GenshinImpact, GenshinRecordModule, GenshinRegion, GenshinTCGModule, HIRecordModule, HI_RECORD_ABYSS_API, HI_RECORD_ARENA_API, HI_RECORD_CHARACTER_API, HI_RECORD_ELYSIAN_API, HI_RECORD_INDEX_API, HI_REDEEM_CLAIM_API, HK4E_API, HKRPG_API, HSRDiaryEnum, HSRDiaryModule, HSRDiaryMonthEnum, HSRRecordModule, HSR_DIARY_DETAIL_API, HSR_DIARY_LIST_API, HSR_RECORD_APOCALYPSE_PHANTOM_API, HSR_RECORD_CHARACTER_API, HSR_RECORD_FORGOTTEN_HALL_API, HSR_RECORD_INDEX_API, HSR_RECORD_NOTE_API, HSR_RECORD_PURE_FICTION_API, HSR_RECORD_WIDGET_API, HSR_REDEEM_CLAIM_API, HTTPRequest, HonkaiImpact, HonkaiRegion, HonkaiStarRail, HoyoAPIError, Hoyolab, HsrRegion, Language, LanguageEnum, NAP_API, PUBLIC_API, RedeemModule, SpiralAbyssScheduleEnum, TAKUMI_API, USER_GAMES_LIST, ZZZRegion, ZZZ_BANBOO_API, ZZZ_RECORD_CHARACTER_API, ZZZ_RECORD_CHARACTER_LIST_API, ZZZ_RECORD_DEADLY_ASSAULT_API, ZZZ_RECORD_INDEX_API, ZZZ_RECORD_NOTE_API, ZZZ_RECORD_SHIYU_DEFENSE_API, ZZZ_REDEEM_CLAIM_API, ZenlessZoneZero, getGenshinRegion, getHi3Region, getHsrRegion, getZZZRegion };