UNPKG

node-hoyolab

Version:

node-hoyolab is an unofficial API Wrapper library developed to integrate with the Hoyolab, which provides a more convenient way to access the Hoyolab API.

306 lines (305 loc) 10.3 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var record_exports = {}; __export(record_exports, { ZZZRecordModule: () => ZZZRecordModule }); module.exports = __toCommonJS(record_exports); var import_record = require("./record.enum"); var import_error = require("../../../error"); var import_routes = require("../../../routes"); class ZZZRecordModule { /** * Creates an instance of HSRRecordModule. * * @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 records associated with the provided region and UID. * * @returns {Promise<ZZZRecord>} A Promise that resolves to the ZZZ record object. * @throws {NodeHoyoError} if the region or UID parameters are missing or failed to be filled. * @throws {NodeHoyoError} if failed to retrieve data, please double-check the provided UID. */ async records() { var _a; if (!this.region || !this.uid) { throw new import_error.NodeHoyoError("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(import_routes.ZZZ_RECORD_INDEX_API); if (res.retcode !== 0) { throw new import_error.NodeHoyoError( (_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 characters associated with the provided region and UID. * * @returns {Promise<ZZZCharacterFull[]>} A Promise that resolves to an array of ZZZCharacterFull objects. * @throws {NodeHoyoError} if the region or UID parameters are missing or failed to be filled. * @throws {NodeHoyoError} if failed to retrieve data, please double-check the provided UID. */ async characters() { var _a; if (!this.region || !this.uid) { throw new import_error.NodeHoyoError("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(import_routes.ZZZ_RECORD_CHARACTER_API); if (res.retcode !== 0) { throw new import_error.NodeHoyoError( (_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 note associated with the provided region and UID. * * @returns {Promise<ZZZNote>} A Promise that resolves to the ZZZ note object. * @throws {NodeHoyoError} if the region or UID parameters are missing or failed to be filled. * @throws {NodeHoyoError} if failed to retrieve data, please double-check the provided UID. */ async note() { var _a; if (!this.region || !this.uid) { throw new import_error.NodeHoyoError("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(import_routes.ZZZ_RECORD_NOTE_API); if (res.retcode !== 0) { throw new import_error.NodeHoyoError( (_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 hollow zero data associated with the provided region and UID. * * @returns {Promise<ZZZHollowZero>} A Promise that resolves to the ZZZ hollow zero object. * @throws {NodeHoyoError} if the region or UID parameters are missing or failed to be filled. * @throws {NodeHoyoError} if failed to retrieve data, please double-check the provided UID. */ async hollowZero() { var _a; if (!this.region || !this.uid) { throw new import_error.NodeHoyoError("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(import_routes.ZZZ_RECORD_HOLLOW_ZERO); if (res.retcode !== 0) { throw new import_error.NodeHoyoError( (_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 data for the given schedule type. * * @param {ShiyuDefense} scheduleType - The schedule type of the Shiyu Defense, either CURRENT or PREVIOUS. Defaults to CURRENT. * @returns {Promise<ZZZShiyuDefense>} A Promise that resolves with the Shiyu Defense data. * @throws {NodeHoyoError} if the region or UID parameters are missing or failed to be filled. * @throws {NodeHoyoError} if the given scheduleType parameter is invalid. * @throws {NodeHoyoError} if failed to retrieve data, please double-check the provided UID. */ async shiyuDefense(scheduleType = import_record.ShiyuDefense.CURRENT) { var _a; if (!this.region || !this.uid) { throw new import_error.NodeHoyoError("UID parameter is missing or failed to be filled"); } if (Object.values(import_record.ShiyuDefense).includes(scheduleType) === false) { throw new import_error.NodeHoyoError("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(import_routes.ZZZ_RECORD_SHIYU_DEFENSE); if (res.retcode !== 0) { throw new import_error.NodeHoyoError( (_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 information about the player's performance in the specified abyss type. * * @param {ZZZAbyssType} abyssType - The type of abyss to retrieve information for. Defaults to 'WitheringGarden'. * @return {Promise<ZZZAbyss>} A Promise that resolves with an object containing the player's abyss data. * @throws {NodeHoyoError} if UID parameter is missing or failed to be filled, or if the given abyss type parameter is invalid. * @throws {NodeHoyoError} if failed to retrieve data, please double-check the provided UID. */ async abyss(abyssType = "WitheringGarden") { var _a; if (!this.region || !this.uid) { throw new import_error.NodeHoyoError("UID parameter is missing or failed to be filled"); } if (Object.values(import_record.Abyss).includes(abyssType) === false) { throw new import_error.NodeHoyoError("The given abyss type parameter is invalid !"); } this.request.setQueryParams({ server: this.region, role_id: this.uid, type: import_record.Abyss[abyssType], lang: this.lang }).setDs(); const { response: res, body, params, headers } = await this.request.send(import_routes.ZZZ_RECORD_ABYSS); if (res.retcode !== 0) { throw new import_error.NodeHoyoError( (_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 URL of a character's avatar based on the provided avatar ID. * * @param {number} avatarId - The ID of the character's avatar. * @return {string} The URL of the character's avatar. */ getCharacterAvatar(avatarId) { return { avatar: "https://act-webstatic.hoyoverse.com/game_record/zzz/role_square_avatar/role_square_avatar_".concat(avatarId, ".png"), avatarFull: "https://act-webstatic.hoyoverse.com/game_record/zzz/role_vertical_painting/role_vertical_painting_".concat(avatarId, ".png") }; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ZZZRecordModule });