Created
October 2, 2022 12:44
-
-
Save aimproxy/4926b95d071ca9318be71652ca5863f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { NextApiRequest, NextApiResponse } from 'next' | |
| import axios, { AxiosInstance } from 'axios' | |
| import { atob } from 'buffer' | |
| const UUIDService = axios.create({ | |
| baseURL: 'https://api.mojang.com/users/profiles/minecraft/', | |
| }) | |
| const ProfileService = axios.create({ | |
| baseURL: 'https://sessionserver.mojang.com/session/minecraft/profile/', | |
| }) | |
| async function __customFetcher<T>(fetcher: AxiosInstance, urlParams: string) { | |
| return (await fetcher.get<T>(urlParams)).data | |
| } | |
| interface UsernameToUUID { | |
| id: string | |
| name: string | |
| } | |
| async function getUUIDByUsername(username: string) { | |
| return await __customFetcher<UsernameToUUID>(UUIDService, username) | |
| } | |
| interface Property { | |
| name: string, | |
| value: string, | |
| signature?: string, | |
| } | |
| interface Profile { | |
| id: string | |
| name: string | |
| properties: Property[] | |
| legacy?: boolean | |
| } | |
| async function getProfileByUUID(uuid: string) { | |
| return await __customFetcher<Profile>(ProfileService, `${uuid}?unsigned=false`) | |
| } | |
| interface Texture { | |
| url: string | |
| metadata?: { | |
| model: 'slim' | 'classic' | |
| } | |
| } | |
| interface DecodedTexture { | |
| timestamp: string | |
| profileId: string | |
| profileName: string | |
| textures: { [key: string]: Texture } | |
| } | |
| function decodeTexture(texture: string) { | |
| const base64decoded = atob(texture) | |
| return JSON.parse(base64decoded) as DecodedTexture | |
| } | |
| export default async function handler( | |
| request: NextApiRequest, | |
| response: NextApiResponse, | |
| ) { | |
| const { name } = request.query | |
| const uuidResponse = await getUUIDByUsername(name as string) | |
| const uuid = uuidResponse.id | |
| const profileResponse = await getProfileByUUID(uuid) | |
| const decodedTexture = decodeTexture(profileResponse.properties[0].value) | |
| const skin = decodedTexture.textures['SKIN'].url | |
| const cape = decodedTexture.textures['CAPE'].url | |
| response.status(200).json({ | |
| timestamp: decodedTexture.timestamp, | |
| name: decodedTexture.profileName, | |
| uuid: decodedTexture.profileId, | |
| skin, | |
| cape, | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment