blob: a340ae01928bd55f4157531e49456b876ba44ec9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/**
* Fetch the Mojang username API through api.ashcon.app
*/
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 AshconV2Response {
uuid: string
username: string
username_history: AshconHistoryItem[]
textures: AshconTextures
created_at?: string
}
interface AshconV1Response {
uuid: string
username: string
username_history: AshconHistoryItem[]
textures: AshconTextures
cached_at?: string
}
/**
* Get mojang api data from ashcon.app
*/
export async function mojangDataFromUser(user: string): Promise<AshconV1Response> {
const fetchResponse = await fetch(
// we use v1 rather than v2 since its more stable
`https://api.ashcon.app/mojang/v1/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
}
|