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.
535 lines (528 loc) • 16.1 kB
JavaScript
import {
DailyModule,
RedeemModule
} from "./chunk-GNA5DODF.mjs";
import {
Cookie,
DEFAULT_REFERER,
HTTPRequest,
Hoyolab,
Language,
NodeHoyoError,
ZZZ_RECORD_ABYSS,
ZZZ_RECORD_CHARACTER_API,
ZZZ_RECORD_HOLLOW_ZERO,
ZZZ_RECORD_INDEX_API,
ZZZ_RECORD_NOTE_API,
ZZZ_RECORD_SHIYU_DEFENSE,
__publicField
} from "./chunk-ASXYY4IT.mjs";
// src/client/zzz/zzz.interface.ts
var ZZZRegion = /* @__PURE__ */ ((ZZZRegion2) => {
ZZZRegion2["USA"] = "prod_gf_jp";
ZZZRegion2["EUROPE"] = "prod_gf_eu";
ZZZRegion2["ASIA"] = "prod_gf_jp";
ZZZRegion2["CHINA_TAIWAN"] = "prod_gf_cht";
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 14:
key = "CHINA_TAIWAN";
break;
default:
throw new NodeHoyoError("Given UID ".concat(uid, " is invalid !"));
}
return ZZZRegion[key];
}
// src/client/zzz/record/record.enum.ts
var ShiyuDefense = /* @__PURE__ */ ((ShiyuDefense2) => {
ShiyuDefense2[ShiyuDefense2["CURRENT"] = 1] = "CURRENT";
ShiyuDefense2[ShiyuDefense2["PREVIOUS"] = 2] = "PREVIOUS";
return ShiyuDefense2;
})(ShiyuDefense || {});
var Abyss = /* @__PURE__ */ ((Abyss2) => {
Abyss2[Abyss2["WitheringGarden"] = 1] = "WitheringGarden";
Abyss2[Abyss2["InfernoReap"] = 2] = "InfernoReap";
Abyss2[Abyss2["TopInfernoReap"] = 3] = "TopInfernoReap";
return Abyss2;
})(Abyss || {});
// src/client/zzz/record/record.ts
var ZZZRecordModule = class {
/**
* 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 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(ZZZ_RECORD_INDEX_API);
if (res.retcode !== 0) {
throw new 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 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(ZZZ_RECORD_CHARACTER_API);
if (res.retcode !== 0) {
throw new 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 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(ZZZ_RECORD_NOTE_API);
if (res.retcode !== 0) {
throw new 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 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(ZZZ_RECORD_HOLLOW_ZERO);
if (res.retcode !== 0) {
throw new 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 = 1 /* CURRENT */) {
var _a;
if (!this.region || !this.uid) {
throw new NodeHoyoError("UID parameter is missing or failed to be filled");
}
if (Object.values(ShiyuDefense).includes(scheduleType) === false) {
throw new 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(ZZZ_RECORD_SHIYU_DEFENSE);
if (res.retcode !== 0) {
throw new 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 NodeHoyoError("UID parameter is missing or failed to be filled");
}
if (Object.values(Abyss).includes(abyssType) === false) {
throw new NodeHoyoError("The given abyss type parameter is invalid !");
}
this.request.setQueryParams({
server: this.region,
role_id: this.uid,
type: Abyss[abyssType],
lang: this.lang
}).setDs();
const {
response: res,
body,
params,
headers
} = await this.request.send(ZZZ_RECORD_ABYSS);
if (res.retcode !== 0) {
throw new 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")
};
}
};
// src/client/zzz/zzz.ts
var ZenlessZoneZero = class _ZenlessZoneZero {
/**
* Create a new instance of ZenlessZoneZero.
*
* @constructor
* @param {ZzzOptions} options - The options for the ZenlessZoneZero instance.
*/
constructor(options) {
/**
* The Daily module for the Zenless Zone Zero game.
*
*/
__publicField(this, "daily");
/**
* The Redeem module for the Zenless Zone Zero game.
*
*/
__publicField(this, "redeem");
/**
* The Record module for the Zenless Zone Zero game.
*
* This module provides methods to interact with the Record module endpoints.
*
*/
__publicField(this, "record");
/**
* The cookie used for authentication.
*
*/
__publicField(this, "cookie");
/**
* The request object used to make HTTP requests.
*
*/
__publicField(this, "request");
/**
* HoyYolab account object
*
*/
__publicField(this, "_account", null);
/**
* The UID of the Zenless Zone Zero account.
*
*/
__publicField(this, "uid");
/**
* The region of the Zenless Zone Zero account.
*
*/
__publicField(this, "region");
/**
* The language of the Zenless Zone Zero account.
*
*/
__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,
"zzz_global" /* ZENLESS_ZONE_ZERO */,
this.region
);
this.redeem = new RedeemModule(
this.request,
this.lang,
"zzz_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 ZenlessZoneZero using a Hoyolab account.
* If `uid` is not provided in the `options`, the account with the highest level will be used.
*
* @param {ZzzOptions} options - The options for the ZenlessZoneZero instance.
* @returns {Promise<ZenlessZoneZero>} - A promise that resolves with a new ZenlessZoneZero 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) {
let game = null;
if (typeof options.uid === "undefined") {
const hoyolab = new Hoyolab({
cookie: options.cookie
});
game = await hoyolab.gameAccount("zzz_global" /* ZENLESS_ZONE_ZERO */);
options.uid = parseInt(game.game_uid);
options.region = getZzzRegion(parseInt(game.game_uid));
}
const zzz = new _ZenlessZoneZero(options);
zzz.account = game;
return zzz;
}
/**
* 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 DailyModule.info | DailyModule.info }
* @deprecated Use through { @link ZenlessZoneZero.daily | ZenlessZoneZero.daily.info() } instead
*/
dailyInfo() {
return this.daily.info();
}
/**
*
* @alias {@link DailyModule.rewards | DailyModule.rewards }
* @deprecated Use through { @link ZenlessZoneZero.daily | ZenlessZoneZero.daily.rewards() } instead
*/
dailyRewards() {
return this.daily.rewards();
}
/**
* Fetch reward from daily login based on day
*
* @param day number | null
* @alias {@link DailyModule.reward | DailyModule.reward }
* @deprecated Use through { @link ZenlessZoneZero.daily | ZenlessZoneZero.daily.reward() } instead
*/
dailyReward(day = null) {
return this.daily.reward(day);
}
/**
* Claim current reward
*
* @alias {@link DailyModule.claim | DailyModule.claim }
* @deprecated Use through { @link ZenlessZoneZero.daily | ZenlessZoneZero.daily.claim() } instead
*/
dailyClaim() {
return this.daily.claim();
}
/**
* Redeem Code
*
* @param code string
* @alias {@link RedeemModule.claim | RedeemModule.claim }
* @deprecated Use through { @link ZenlessZoneZero.redeem | ZenlessZoneZero.redeem.claim() } instead
*/
redeemCode(code) {
return this.redeem.claim(code);
}
};
export {
ZZZRegion,
getZzzRegion,
ShiyuDefense,
Abyss,
ZZZRecordModule,
ZenlessZoneZero
};