UNPKG

@yeci226/hoyoapi

Version:

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

196 lines (195 loc) 5.88 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 mimo_exports = {}; __export(mimo_exports, { MimoModule: () => MimoModule }); module.exports = __toCommonJS(mimo_exports); var import_routes = require("../../routes"); class MimoModule { /** * @param request - HTTPRequest instance with auth cookies set. * @param lang - Language for API responses. * @param gameId - Mimo game ID (6 = HSR, 8 = ZZZ). */ constructor(request, lang, gameId) { this.request = request; this.lang = lang; this.gameId = gameId; } /** * Fetches the current Mimo version_id for this game. * Returns null if the event is not active. */ async getVersionId() { var _a, _b; this.request.setQueryParams({ lang: this.lang }); const { response } = await this.request.send(import_routes.MIMO_INDEX_API, "GET", 0); const list = response == null ? void 0 : response.list; return (_b = (_a = list == null ? void 0 : list.find((g) => g.game_id === this.gameId)) == null ? void 0 : _a.version_id) != null ? _b : null; } /** * Retrieves the list of Mimo tasks for the current version. * * @param versionId - The version_id from {@link getVersionId}. */ async tasks(versionId) { var _a; this.request.setQueryParams({ game_id: this.gameId, lang: this.lang, version_id: versionId }); const { response } = await this.request.send(import_routes.MIMO_TASK_LIST_API, "GET", 0); return (_a = response == null ? void 0 : response.list) != null ? _a : []; } /** * Marks a task as finished (triggers completion for click-type tasks). * * @param versionId - The current version_id. * @param taskId - ID of the task to finish. */ async finishTask(versionId, taskId) { this.request.setBody({ game_id: this.gameId, lang: this.lang, version_id: versionId, task_id: taskId }); const { response } = await this.request.send( import_routes.MIMO_FINISH_TASK_API, "POST", 0 ); return (response == null ? void 0 : response.retcode) === 0 || true; } /** * Claims the reward points for a completed task. * * @param versionId - The current version_id. * @param taskId - ID of the completed task. */ async claimTask(versionId, taskId) { this.request.setQueryParams({ game_id: this.gameId, lang: this.lang, version_id: versionId, task_id: taskId }); const { response } = await this.request.send( import_routes.MIMO_RECEIVE_POINT_API, "GET", 0 ); return !!response; } /** * Completes all pending tasks and claims their rewards in one call. * Returns the number of tasks successfully claimed. */ async claimAllTasks() { const versionId = await this.getVersionId(); if (!versionId) return 0; const taskList = await this.tasks(versionId); let claimed = 0; for (const task of taskList) { if (task.status === 0) { await this.finishTask(versionId, task.id); if (await this.claimTask(versionId, task.id)) claimed++; } else if (task.status === 1) { if (await this.claimTask(versionId, task.id)) claimed++; } } return claimed; } /** * Retrieves the Mimo shop exchange items. * * @param versionId - The current version_id. */ async exchangeList(versionId) { var _a; this.request.setQueryParams({ game_id: this.gameId, lang: this.lang, version_id: versionId }); const { response } = await this.request.send( import_routes.MIMO_EXCHANGE_LIST_API, "GET", 0 ); return (_a = response == null ? void 0 : response.list) != null ? _a : []; } /** * Exchanges points for a shop item (typically returns a gift code). * * @param versionId - The current version_id. * @param awardId - The award_id of the shop item. */ async exchange(versionId, awardId) { this.request.setBody({ game_id: this.gameId, lang: this.lang, version_id: versionId, award_id: awardId }); const { response } = await this.request.send(import_routes.MIMO_EXCHANGE_API, "POST", 0); return response; } /** * Gets current lottery state (total points, cost per draw, draws done). * * @param versionId - The current version_id. */ async lotteryInfo(versionId) { this.request.setQueryParams({ game_id: this.gameId, lang: this.lang, version_id: versionId }); const { response } = await this.request.send( import_routes.MIMO_LOTTERY_INFO_API, "GET", 0 ); return response; } /** * Performs one lottery draw. * * @param versionId - The current version_id. */ async lottery(versionId) { this.request.setBody({ game_id: this.gameId, lang: this.lang, version_id: versionId }); const { response } = await this.request.send(import_routes.MIMO_LOTTERY_API, "POST", 0); return !!response; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { MimoModule });