diff options
author | mat <27899617+mat-1@users.noreply.github.com> | 2021-02-13 14:13:42 -0600 |
---|---|---|
committer | mat <27899617+mat-1@users.noreply.github.com> | 2021-02-13 14:13:42 -0600 |
commit | 52e38809212133ef673d11bfa96ba3bb43c3644c (patch) | |
tree | f54408afd41cc64b64d5e82a3ad814b1bb55d4a7 /src/mojang.ts | |
parent | a23103ec24128f2e24b93ad101ade6dfdd4758c3 (diff) | |
download | skyblock-api-52e38809212133ef673d11bfa96ba3bb43c3644c.tar.gz skyblock-api-52e38809212133ef673d11bfa96ba3bb43c3644c.tar.bz2 skyblock-api-52e38809212133ef673d11bfa96ba3bb43c3644c.zip |
move stuff into src folder
Diffstat (limited to 'src/mojang.ts')
-rw-r--r-- | src/mojang.ts | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/mojang.ts b/src/mojang.ts new file mode 100644 index 0000000..746f674 --- /dev/null +++ b/src/mojang.ts @@ -0,0 +1,59 @@ +import fetch from 'node-fetch' +import { Agent } from 'https' + +// We need to create an agent to prevent memory leaks +const httpsAgent = new Agent({ + keepAlive: true +}) + +interface AshconHistoryItem { + username: string + changed_at?: string +} + +interface AshconTextures { + custom: boolean + slim: boolean + skin: { url: string, data: string } + raw: { value: string, signature: string } +} + +interface AshconResponse { + uuid: string + username: string + username_history: AshconHistoryItem[] + textures: AshconTextures + created_at?: string +} + +/** + * Get mojang api data from ashcon.app + */ +export async function mojangDataFromUser(user: string): Promise<AshconResponse> { + console.log('cache miss :( mojangDataFromUser', user) + const fetchResponse = await fetch( + 'https://api.ashcon.app/mojang/v2/user/' + user, + { agent: () => httpsAgent } + ) + return await fetchResponse.json() +} + +/** + * Fetch the uuid from a user + * @param user A user can be either a uuid or a username + */ +export async function uuidFromUser(user: string): Promise<string> { + const fetchJSON = await mojangDataFromUser(user) + return fetchJSON.uuid.replace(/-/g, '') +} + +/** + * Fetch the username from a user + * @param user A user can be either a uuid or a username + */ +export async function usernameFromUser(user: string): Promise<string> { + // get a minecraft uuid from a username, using ashcon.app's mojang api + const fetchJSON = await mojangDataFromUser(user) + return fetchJSON.username +} + |