Skip to content
This repository was archived by the owner on Oct 27, 2024. It is now read-only.

Commit de342b6

Browse files
committed
use alternative mojang api
1 parent fc08668 commit de342b6

File tree

1 file changed

+25
-51
lines changed

1 file changed

+25
-51
lines changed

src/mojang.ts

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { Response as UndiciResponse } from 'undici/types/fetch'
99

1010
// We need to create an agent to prevent memory leaks
1111
const httpsAgent = new Agent({
12-
keepAlive: true
12+
keepAlive: true,
1313
})
1414

1515
interface MojangApiResponse {
@@ -21,13 +21,15 @@ interface MojangApiResponse {
2121
/**
2222
* Get mojang api data from the session server
2323
*/
24-
export let profileFromUuid = async function profileFromUuid(uuid: string): Promise<MojangApiResponse> {
24+
export let profileFromUuid = async function profileFromUuid(
25+
uuid: string
26+
): Promise<MojangApiResponse> {
2527
let fetchResponse: UndiciResponse
2628

2729
try {
2830
fetchResponse = await fetch(
2931
// using mojang directly is faster than ashcon, also there seem to be no ratelimits here?
30-
`https://sessionserver.mojang.com/session/minecraft/profile/${undashUuid(uuid)}`,
32+
`https://mowojang.matdoes.dev/${undashUuid(uuid)}`
3133
)
3234
} catch {
3335
// if there's an error, wait a second and try again
@@ -50,20 +52,19 @@ export let profileFromUuid = async function profileFromUuid(uuid: string): Promi
5052
}
5153
return {
5254
uuid: data.id,
53-
username: data.name
55+
username: data.name,
5456
}
5557
}
5658

57-
58-
export let profileFromUsername = async function profileFromUsername(username: string): Promise<MojangApiResponse> {
59+
export let profileFromUsername = async function profileFromUsername(
60+
username: string
61+
): Promise<MojangApiResponse> {
5962
// since we don't care about anything other than the uuid, we can use /uuid/ instead of /user/
6063

6164
let fetchResponse: UndiciResponse
6265

6366
try {
64-
fetchResponse = await fetch(
65-
`https://api.mojang.com/users/profiles/minecraft/${username}`,
66-
)
67+
fetchResponse = await fetch(`https://mowojang.matdoes.dev/${username}`)
6768
} catch {
6869
// if there's an error, wait a second and try again
6970
await sleep(1000)
@@ -74,56 +75,29 @@ export let profileFromUsername = async function profileFromUsername(username: st
7475
const rawData = await fetchResponse.text()
7576
try {
7677
data = JSON.parse(rawData)
77-
} catch { }
78-
79-
80-
if (!data?.id) {
81-
// return { uuid: null, username: null }
82-
return await profileFromUsernameAlternative(username)
83-
}
78+
} catch {}
8479

8580
return {
8681
uuid: data.id,
87-
username: data.name
88-
}
89-
}
90-
91-
export async function profileFromUsernameAlternative(username: string): Promise<MojangApiResponse> {
92-
let fetchResponse: UndiciResponse
93-
94-
try {
95-
fetchResponse = await fetch(
96-
`https://api.ashcon.app/mojang/v2/user/${username}`,
97-
)
98-
} catch {
99-
// if there's an error, wait a second and try again
100-
await sleep(1000)
101-
return await profileFromUsernameAlternative(username)
102-
}
103-
104-
let data
105-
try {
106-
data = await fetchResponse.json()
107-
} catch {
108-
return { uuid: null, username: null }
109-
}
110-
if (!data.uuid)
111-
return { uuid: null, username: null }
112-
return {
113-
uuid: undashUuid(data.uuid),
114-
username: data.username
82+
username: data.name,
11583
}
11684
}
11785

118-
export let profileFromUser = async function profileFromUser(user: string): Promise<MojangApiResponse> {
86+
export let profileFromUser = async function profileFromUser(
87+
user: string
88+
): Promise<MojangApiResponse> {
11989
if (isUuid(user)) {
12090
return await profileFromUuid(user)
121-
} else
122-
return await profileFromUsername(user)
91+
} else return await profileFromUsername(user)
12392
}
12493

125-
12694
// this is necessary for mocking in the tests because es6
127-
export function mockProfileFromUuid($value) { profileFromUuid = $value }
128-
export function mockProfileFromUsername($value) { profileFromUsername = $value }
129-
export function mockProfileFromUser($value) { profileFromUser = $value }
95+
export function mockProfileFromUuid($value) {
96+
profileFromUuid = $value
97+
}
98+
export function mockProfileFromUsername($value) {
99+
profileFromUsername = $value
100+
}
101+
export function mockProfileFromUser($value) {
102+
profileFromUser = $value
103+
}

0 commit comments

Comments
 (0)