11import { CACHE_TTL_SECONDS , STATUS } from "../config/constants" ;
2- import { fetchSessionProfile } from "../services/minecraftApi" ;
2+ import { fetchSessionProfile , lookupByUsername } from "../services/minecraftApi" ;
33import { getCachedEntry , setCachedEntry } from "../utils/cache" ;
44import { badRequest , internalError , jsonResponse , notFound } from "../utils/responses" ;
55import {
66 buildCacheMeta ,
77 decodeBase64Json ,
8- isUuid ,
9- normalizeUuid ,
10- nowSeconds
8+ nowSeconds ,
9+ parseUsernameOrUuid
1110} from "../utils/utils" ;
1211
1312function uuidNameKey ( username ) {
@@ -61,12 +60,14 @@ async function cacheIdentityPayload(env, payload) {
6160}
6261
6362export async function handleProfileRoute ( identifier , env ) {
64- if ( ! identifier || ! isUuid ( identifier . trim ( ) ) ) {
65- return badRequest ( "Invalid uuid format" ) ;
63+ const parsed = parseUsernameOrUuid ( identifier ) ;
64+ if ( ! parsed . ok ) {
65+ return badRequest ( parsed . message ) ;
6666 }
6767
68- const normalizedUuid = normalizeUuid ( identifier . trim ( ) ) ;
69- const cacheKey = uuidIdKey ( normalizedUuid ) ;
68+ const cacheKey = parsed . kind === "uuid"
69+ ? uuidIdKey ( parsed . normalizedUuid )
70+ : uuidNameKey ( parsed . username ) ;
7071
7172 const cached = await getCachedEntry ( env , cacheKey ) ;
7273 if ( isValidCachedProfile ( cached ) ) {
@@ -76,7 +77,23 @@ export async function handleProfileRoute(identifier, env) {
7677 }
7778
7879 try {
79- const upstream = await fetchSessionProfile ( normalizedUuid ) ;
80+ let resolvedId = parsed . kind === "uuid" ? parsed . normalizedUuid : null ;
81+
82+ // If a username key is already cached without profile, skip an extra lookup.
83+ if ( ! resolvedId && cached ?. payload ?. id ) {
84+ resolvedId = cached . payload . id ;
85+ }
86+
87+ if ( ! resolvedId ) {
88+ const identityUpstream = await lookupByUsername ( parsed . username ) ;
89+ if ( identityUpstream . notFound ) {
90+ return notFound ( "Player profile not found" ) ;
91+ }
92+
93+ resolvedId = identityUpstream . data . id ;
94+ }
95+
96+ const upstream = await fetchSessionProfile ( resolvedId ) ;
8097 if ( upstream . notFound ) {
8198 return notFound ( "Player profile not found" ) ;
8299 }
0 commit comments