mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-13 14:14:10 +00:00
Add function to fetch player information (#2010)
## Description: This pull request introduces the fetchPlayerById() function together with its associated schema components. It represents one part of a series of split pull requests related to the PlayerInfoModal (Player Profile). Subsequent pull requests will address UI implementation and additional features. (origin pr:https://github.com/openfrontio/OpenFrontIO/pull/1758) ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: aotumuri --------- Co-authored-by: evanpelle <evanpelle@gmail.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { decodeJwt } from "jose";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
PlayerProfile,
|
||||
PlayerProfileSchema,
|
||||
RefreshResponseSchema,
|
||||
TokenPayload,
|
||||
TokenPayloadSchema,
|
||||
@@ -268,3 +270,42 @@ export async function getUserMe(): Promise<UserMeResponse | false> {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchPlayerById(
|
||||
playerId: string,
|
||||
): Promise<PlayerProfile | false> {
|
||||
try {
|
||||
const base = getApiBase();
|
||||
const token = getToken();
|
||||
if (!token) return false;
|
||||
const url = `${base}/player/${encodeURIComponent(playerId)}`;
|
||||
|
||||
const res = await fetch(url, {
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (res.status !== 200) {
|
||||
console.warn(
|
||||
"fetchPlayerById: unexpected status",
|
||||
res.status,
|
||||
res.statusText,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
const json = await res.json();
|
||||
const parsed = PlayerProfileSchema.safeParse(json);
|
||||
if (!parsed.success) {
|
||||
console.warn("fetchPlayerById: Zod validation failed", parsed.error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return parsed.data;
|
||||
} catch (err) {
|
||||
console.warn("fetchPlayerById: request failed", err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user