enkanetwork
Version:
API wrapper for enka.network written on TypeScript which provides localization, caching and convenience
153 lines (152 loc) • 6.85 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnkaNetwork = void 0;
//@ts-ignore
const config_json_1 = __importDefault(require("../assets/config.json"));
//@ts-ignore
const package_json_1 = require("../package.json");
const errors_1 = require("./errors");
const helpers_1 = require("./helpers");
const models_1 = require("./models");
__exportStar(require("./models"), exports);
/** The class in which all interaction with enka.network takes place
*
* [Documentation](https://kravets.gitbook.io/enkanetwork/class-description/enkanetwork)
**/
class EnkaNetwork {
cache;
language;
assets;
assetsUpdater;
userAgent;
constructor({ language = "EN", caching = true, userAgent, assets = { instant: false, languages: [] }, uiAssetsPath, } = {}) {
this.language = language;
this.userAgent = userAgent;
this.cache = caching ? new helpers_1.CacheManager({ checkPeriod: 20 }) : undefined;
const dataManager = new helpers_1.DataManager();
const localizationManager = new helpers_1.LocalizationManager();
this.assets = new helpers_1.AssetsFinder({
uiAssetsPath,
dataManager,
localizationManager,
});
// Thus it is not necessary to fill in the languages in assets if it is selected
if (typeof assets === "object") {
if (!assets.languages)
assets.languages = [];
assets.languages.push(language);
}
this.assetsUpdater = assets
? new helpers_1.AssetsUpdater(assets, dataManager, localizationManager)
: undefined;
}
async request(url, cacheKey) {
if (cacheKey && this.cache) {
const cache = await this.cache.get(cacheKey);
if (cache)
return cache;
}
const res = await fetch(`https://enka.network/api${url}`, {
method: "GET",
headers: {
...(this.userAgent !== false && {
"User-Agent": typeof this.userAgent === "string"
? this.userAgent
: `enkaNetwork@${package_json_1.version}`,
}),
},
});
if (!res.ok)
throw new errors_1.APIError(res.status);
const data = await res.json();
if (cacheKey && this.cache && data.ttl) {
this.cache.set(cacheKey, data, data.ttl);
}
return data;
}
/**
* Fetch only profile info by uid from the game.
* @param {number} uid `UID` from the game.
* @param {string} language The language to be used in the localization of names (character, artifacts, etc.). Default is EnkaNetwork.language.
*/
async fetchProfileInfo(uid, language) {
const response = await this.request(`/uid/${uid}/?info`, `playerInfoByUID-${uid}-${language || this.language}`);
const data = new models_1.FetchProfileInfo(this.assets, language || this.language, response);
return data;
}
/**
* Fetch user by uid from the game.
*
* [Documentation](https://kravets.gitbook.io/enkanetwork/methods/fetchuser)
* @param {number} uid `UID` from the game.
* @param {string} language The language to be used in the localization of names (character, artifacts, etc.). Default is EnkaNetwork.language.
*/
async fetchUser(uid, language) {
const response = await this.request(`/uid/${uid}`, `uid-${uid}-${language || this.language}`);
const data = new models_1.FetchUserUID(this.assets, language || this.language, response);
return data;
}
/**
* Fetch enkaProfile by username from the site.
* @param {string} username `username` from the site.
*/
async fetchEnkaProfile(username) {
const response = await this.request(`/profile/${username}`);
return new models_1.FetchEnkaProfile(response);
}
/**
* Fetch enkaProfile accounts by username from the site.
* @param {string} username `username` from the site.
* @param {string} language The language to be used in the localization of names (character, artifacts, etc.). Default is EnkaNetwork.language.
*/
async fetchEnkaHoyos(username, language) {
const response = await this.request(`/profile/${username}/hoyos`);
return Object.values(response).map((account) => new models_1.FetchEnkaHoyo(this.assets, language || this.language, account));
}
/**
* Fetch enkaProfile account by username and account hash from the site.
* @param {string} username `username` from the site.
* @param {string} hash account `hash` from the account.
* @param {string} language The language to be used in the localization of names (character, artifacts, etc.). Default is EnkaNetwork.language.
*/
async fetchEnkaHoyo(username, hash, language) {
const response = await this.request(`/profile/${username}/hoyos/${hash}`);
return new models_1.FetchEnkaHoyo(this.assets, language || this.language, response);
}
/**
* Fetch enkaProfile builds for account from the site.
* @param {string} username `username` from the site.
* @param {string} hash account `hash` from the account.
* @param {string} language The language to be used in the localization of names (character, artifacts, etc.). Default is EnkaNetwork.language.
*/
async fetchEnkaHoyoBuilds(username, hash, language) {
const response = await this.request(`/profile/${username}/hoyos/${hash}/builds`);
return Object.values(response)
.flat()
.map((build) => new models_1.FetchEnkaHoyoBuilds(this.assets, language || this.language, build));
}
setLanguage(language) {
if (!config_json_1.default.languages.includes(language))
throw new errors_1.NoLanguageFound("This language is not downloaded");
this.language = language;
return this;
}
}
exports.EnkaNetwork = EnkaNetwork;