diff options
author | mat <github@matdoes.dev> | 2021-06-02 19:26:33 -0500 |
---|---|---|
committer | mat <github@matdoes.dev> | 2021-06-02 19:26:33 -0500 |
commit | a43543190cccac676a0c38dd0a015bccf7b3e6c9 (patch) | |
tree | 1596f868d2af47772116eff1e2a7907a8df00687 /src/mojang.ts | |
parent | 1894126287af5fc584a2b65b27604c776e0b24f2 (diff) | |
download | skyblock-api-a43543190cccac676a0c38dd0a015bccf7b3e6c9.tar.gz skyblock-api-a43543190cccac676a0c38dd0a015bccf7b3e6c9.tar.bz2 skyblock-api-a43543190cccac676a0c38dd0a015bccf7b3e6c9.zip |
add api.ashcon.app as alternative to mojang api
Diffstat (limited to 'src/mojang.ts')
-rw-r--r-- | src/mojang.ts | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/src/mojang.ts b/src/mojang.ts index 829de7f..331b3f3 100644 --- a/src/mojang.ts +++ b/src/mojang.ts @@ -70,8 +70,11 @@ export async function profileFromUsername(username: string): Promise<MojangApiRe let data try { data = await fetchResponse.json() - } catch { - return { uuid: null, username: null } + } catch {} + + if (!data.id) { + console.log('mojang api failed, trying ashcon as backup') + return await profileFromUsernameAlternative(username) } return { uuid: data.id, @@ -79,6 +82,31 @@ export async function profileFromUsername(username: string): Promise<MojangApiRe } } +export async function profileFromUsernameAlternative(username: string): Promise<MojangApiResponse> { + let fetchResponse: nodeFetch.Response + + try { + fetchResponse = await fetch( + `https://api.ashcon.app/mojang/v2/user/${username}`, + { agent: () => httpsAgent } + ) + } catch { + // if there's an error, wait a second and try again + await new Promise((resolve) => setTimeout(resolve, 1000)) + return await profileFromUsernameAlternative(username) + } + + let data + try { + data = await fetchResponse.json() + } catch { + return { uuid: null, username: null } + } + return { + uuid: data.uuid, + username: data.username + } +} export async function profileFromUser(user: string): Promise<MojangApiResponse> { if (isUuid(user)) { |