UNPKG

@yeci226/hoyoapi

Version:

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

109 lines (108 loc) 4.07 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; 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 __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; var wiki_exports = {}; __export(wiki_exports, { WikiModule: () => WikiModule }); module.exports = __toCommonJS(wiki_exports); class WikiModule { /** * @param request - HTTPRequest instance (no auth needed for wiki). * @param lang - Language for wiki responses. * @param game - 'hsr' or 'zzz'. */ constructor(_request, lang, game) { this.lang = lang; __publicField(this, "searchApi"); __publicField(this, "entryApi"); __publicField(this, "entryListApi"); __publicField(this, "wikiHeaders"); const base = "https://sg-wiki-api-static.hoyolab.com/hoyowiki/".concat(game, "/wapi"); this.searchApi = "".concat(base, "/search"); this.entryApi = "".concat(base, "/entry_page"); this.entryListApi = "".concat(base, "/get_entry_page_list"); this.wikiHeaders = { "x-rpc-wiki_app": game, "x-rpc-language": lang, Referer: "https://wiki.hoyolab.com/", Origin: "https://wiki.hoyolab.com" }; } /** * Searches the wiki by keyword. * Returns the first matching entry page ID, or null if not found. * * @param keyword - Character/weapon/item name to search for. */ async search(keyword) { var _a, _b; const url = "".concat(this.searchApi, "?keyword=").concat(encodeURIComponent(keyword)); const resp = await fetch(url, { headers: this.wikiHeaders }); const data = await resp.json(); const results = (_a = data == null ? void 0 : data.data) == null ? void 0 : _a.results; return (_b = results == null ? void 0 : results[0]) != null ? _b : null; } /** * Retrieves the full entry page for a given entry_page_id. * * @param entryPageId - The entry_page_id from {@link search}. */ async entryPage(entryPageId) { var _a, _b; const url = "".concat(this.entryApi, "?entry_page_id=").concat(entryPageId, "&lang=").concat(this.lang); const resp = await fetch(url, { headers: this.wikiHeaders }); const data = await resp.json(); return (_b = (_a = data == null ? void 0 : data.data) == null ? void 0 : _a.page) != null ? _b : null; } /** * Lists entries in a wiki menu category. * * @param menuId - The menu_id for the category (e.g. characters, weapons). * @param pageNum - Page number (1-indexed). * @param pageSize - Results per page (max 100). */ async entryList(menuId, pageNum = 1, pageSize = 100) { var _a, _b; const resp = await fetch(this.entryListApi, { method: "POST", headers: { ...this.wikiHeaders, "Content-Type": "application/json" }, body: JSON.stringify({ filters: [], menu_id: menuId, page_num: pageNum, page_size: pageSize, use_es: true }) }); const data = await resp.json(); return (_b = (_a = data == null ? void 0 : data.data) == null ? void 0 : _a.list) != null ? _b : []; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { WikiModule });