diff options
author | mat <27899617+mat-1@users.noreply.github.com> | 2021-02-13 14:14:50 -0600 |
---|---|---|
committer | mat <27899617+mat-1@users.noreply.github.com> | 2021-02-13 14:14:50 -0600 |
commit | f3e74e786df8e11f85ec895e0750df8ab124b475 (patch) | |
tree | 25c53f6452c0e75ad8f8c1290f70d9da8e46261f /build/mojang.js | |
parent | 52e38809212133ef673d11bfa96ba3bb43c3644c (diff) | |
download | skyblock-api-f3e74e786df8e11f85ec895e0750df8ab124b475.tar.gz skyblock-api-f3e74e786df8e11f85ec895e0750df8ab124b475.tar.bz2 skyblock-api-f3e74e786df8e11f85ec895e0750df8ab124b475.zip |
add compiled code in build folder
Diffstat (limited to 'build/mojang.js')
-rw-r--r-- | build/mojang.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/build/mojang.js b/build/mojang.js new file mode 100644 index 0000000..981bf31 --- /dev/null +++ b/build/mojang.js @@ -0,0 +1,40 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.usernameFromUser = exports.uuidFromUser = exports.mojangDataFromUser = void 0; +const node_fetch_1 = __importDefault(require("node-fetch")); +const https_1 = require("https"); +// We need to create an agent to prevent memory leaks +const httpsAgent = new https_1.Agent({ + keepAlive: true +}); +/** + * Get mojang api data from ashcon.app + */ +async function mojangDataFromUser(user) { + console.log('cache miss :( mojangDataFromUser', user); + const fetchResponse = await node_fetch_1.default('https://api.ashcon.app/mojang/v2/user/' + user, { agent: () => httpsAgent }); + return await fetchResponse.json(); +} +exports.mojangDataFromUser = mojangDataFromUser; +/** + * Fetch the uuid from a user + * @param user A user can be either a uuid or a username + */ +async function uuidFromUser(user) { + const fetchJSON = await mojangDataFromUser(user); + return fetchJSON.uuid.replace(/-/g, ''); +} +exports.uuidFromUser = uuidFromUser; +/** + * Fetch the username from a user + * @param user A user can be either a uuid or a username + */ +async function usernameFromUser(user) { + // get a minecraft uuid from a username, using ashcon.app's mojang api + const fetchJSON = await mojangDataFromUser(user); + return fetchJSON.username; +} +exports.usernameFromUser = usernameFromUser; |