@genshin-kit/core
Version:
An API wrapper for fetching player data of Genshin Impact from any servers.
242 lines • 8.21 kB
JavaScript
"use strict";
/**
* @name GenshinKit
* @desc An API wrapper for fetching player data of Genshin Impact
*
* @author 机智的小鱼君 <dragon-fish@qq.com>
* @license Apache-2.0
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenshinKit = exports.util = exports.name = void 0;
const tslib_1 = require("tslib");
// Name
exports.name = 'genshin-kit';
// Modules
const _getApiEndpoint_1 = require("./module/_getApiEndpoint");
const _getDS_1 = require("./module/_getDS");
const _getHttpHeaders_1 = require("./module/_getHttpHeaders");
const _getServer_1 = require("./module/_getServer");
const _hoyolabVersion_1 = require("./module/_hoyolabVersion");
const request_1 = require("./module/request");
exports.util = (0, tslib_1.__importStar)(require("./util"));
const url_1 = require("url");
const util_1 = require("util");
class GenshinKit {
constructor() {
// Cache
this._cache = {};
// Variables
this.cookie = '';
this._getApiEndpoint = _getApiEndpoint_1._getApiEndpoint;
this._getDS = _getDS_1._getDS;
this._getHttpHeaders = _getHttpHeaders_1._getHttpHeaders;
this._getServer = _getServer_1._getServer;
this._hoyolabVersion = _hoyolabVersion_1._hoyolabVersion;
this.request = request_1.request;
this.serverType = 'cn';
this.serverLocale = 'zh-cn';
// Alias
this.getCharacters = this.getAllCharacters;
this.getUserRoles = this.getAllCharacters;
this.getAbyss = this.getSpiralAbyss;
this.getCurAbyss = this.getCurrentAbyss;
this.getPrevAbyss = this.getPreviousAbyss;
this.setCookie = this.loginWithCookie;
}
/**
* @method loginWithCookie
* @param {String} cookie
*/
loginWithCookie(cookie) {
this.clearCache();
this.cookie = cookie;
return this;
}
/**
* @method clearCache
*/
clearCache() {
this._cache = {};
return this;
}
/**
* @method setServerType
* @param type Server type: cn => China server, os => Oversea server
*/
setServerType(type) {
this.serverType = type;
return this;
}
/**
* @method setServerLanguage
* @param locale Server locale: Language in which character names, weapons, etc. will be displayed.
*/
setServerLocale(locale) {
this.serverLocale = locale;
return this;
}
/**
* @function getUserInfo
* @param {Number} uid
* @returns {Promise<UserInfo>}
*/
async getUserInfo(uid, noCache = false) {
var _a, _b;
const temp = (_b = (_a = this._cache) === null || _a === void 0 ? void 0 : _a[uid]) === null || _b === void 0 ? void 0 : _b.info;
if (temp && !noCache) {
return temp;
}
const server = this._getServer(uid);
const data = await this.request('get', 'index', {
role_id: uid,
server,
});
if (data.retcode !== 0 || !data.data) {
throw {
code: data.retcode,
message: data.message,
};
}
this._cache[uid] = {
...this._cache[uid],
info: data.data,
};
return data.data;
}
/**
* @function getAllCharacters
* @param {Number} uid
* @returns {Promise<Character[]>}
*/
async getAllCharacters(uid, noCache = false) {
var _a, _b, _c, _d;
const temp = (_b = (_a = this._cache) === null || _a === void 0 ? void 0 : _a[uid]) === null || _b === void 0 ? void 0 : _b.roles;
if (temp && !noCache) {
return temp;
}
const server = this._getServer(uid);
const userInfo = await this.getUserInfo(uid);
const character_ids = userInfo.avatars.map((item) => {
return item.id;
});
const data = await this.request('post', 'character', {
character_ids,
role_id: uid,
server,
});
if (data.retcode !== 0 || !data.data) {
throw {
code: data.retcode,
message: data.message,
};
}
else {
this._cache[uid] = {
...this._cache[uid],
roles: (_c = data === null || data === void 0 ? void 0 : data.data) === null || _c === void 0 ? void 0 : _c.avatars,
};
return ((_d = data === null || data === void 0 ? void 0 : data.data) === null || _d === void 0 ? void 0 : _d.avatars) || [];
}
}
getCharacterDetailsUrl(uid, id) {
return (0, util_1.deprecate)(() => {
const server = this._getServer(uid);
return `https://webstatic.mihoyo.com/app/community-game-records/index.html?${new url_1.URLSearchParams({ bbs_presentation_style: 'fullscreen' })}#/ys/role?${new url_1.URLSearchParams({
role_id: uid.toString(),
server: server,
id: id.toString(),
})}`;
}, '`getCharacterDetailsUrl()` has been deprecated.')();
}
/**
* @function getSpiralAbyss
* @param {Number} uid
* @param {1|2} type 1 cur, 2 prev
* @returns {Promise<Abyss>}
*/
async getSpiralAbyss(uid, type = 1, noCache = false) {
var _a, _b, _c;
if (type !== 1 && type !== 2) {
throw { code: -1, message: 'Invalid abyss type' };
}
const temp = (_c = (_b = (_a = this._cache) === null || _a === void 0 ? void 0 : _a[uid]) === null || _b === void 0 ? void 0 : _b.abyss) === null || _c === void 0 ? void 0 : _c[type];
if (temp && !noCache) {
return temp;
}
const server = this._getServer(uid);
const data = await this.request('get', 'spiralAbyss', {
role_id: uid,
schedule_type: type,
server,
});
if (data.retcode !== 0 || !data.data) {
throw { code: data.retcode, message: data.message };
}
else {
this._cache[uid] = this._cache[uid] || {};
this._cache[uid].abyss = {
...this._cache[uid].abyss,
[type]: data.data,
};
return data.data;
}
}
/**
* @method getActivities 获取限时活动信息
*/
async getActivities(uid) {
const server = this._getServer(uid);
const data = await this.request('get', 'activities', {
role_id: uid,
server,
});
if (data.retcode !== 0 || !data.data) {
throw { code: data.retcode, message: data.message };
}
else {
return data.data;
}
}
/**
* @function getCurrentAbyss
*/
async getCurrentAbyss(uid, noCache) {
return this.getSpiralAbyss(uid, 1, noCache);
}
/**
* @function getPreviousAbyss
*/
async getPreviousAbyss(uid, noCache) {
return this.getSpiralAbyss(uid, 2, noCache);
}
/**
* @function getDailyNote
* @param {Number} uid
* @returns {Promise<DailyNote>}
*/
async getDailyNote(uid, noCache = false) {
var _a, _b;
const temp = (_b = (_a = this._cache) === null || _a === void 0 ? void 0 : _a[uid]) === null || _b === void 0 ? void 0 : _b.dailyNote;
if (temp && !noCache) {
return temp;
}
const server = this._getServer(uid);
const data = await this.request('get', 'dailyNote', {
role_id: uid,
server,
});
if (data.retcode !== 0 || !data.data) {
throw {
code: data.retcode,
message: data.message,
};
}
this._cache[uid] = {
...this._cache[uid],
dailyNote: data.data,
};
return data.data;
}
}
exports.GenshinKit = GenshinKit;
//# sourceMappingURL=index.js.map