Files
juwan-frontend/lib/api/users.ts
T
2026-04-26 01:53:05 +08:00

76 lines
1.8 KiB
TypeScript

import { httpJson } from "@/lib/api/http"
import { isApiError } from "@/lib/errors"
import type { User, UserRole } from "@/lib/types"
export type UpdateCurrentUserInput = {
nickname?: string
avatar?: string
bio?: string
}
export type VerificationMaterials = {
idCardFront: string
idCardBack: string
gameScreenshots?: string[]
voiceDemo?: string
}
export type VerificationRecord = {
id: number
userId: number
userNickname: string
role: UserRole
status: string
materials: Record<string, string>
rejectReason?: string
createdAt: string
reviewedAt?: string
}
export async function getUserById(userId: string): Promise<User | undefined> {
try {
return await httpJson<User>(`/api/v1/users/${encodeURIComponent(userId)}`)
} catch (err) {
if (isApiError(err) && err.code === 404) return undefined
throw err
}
}
export async function getCurrentUserForLogin(): Promise<User> {
return httpJson<User>("/api/v1/users/me")
}
export async function updateCurrentUser(input: UpdateCurrentUserInput): Promise<User> {
return httpJson<User>("/api/v1/users/me", {
method: "PUT",
json: input,
})
}
export async function switchCurrentRole(role: UserRole): Promise<void> {
await httpJson<unknown>("/api/v1/users/me/switch-role", {
method: "POST",
json: { role },
})
}
export async function applyCurrentUserVerification(input: {
role: UserRole
materials: VerificationMaterials
}): Promise<void> {
await httpJson<unknown>("/api/v1/users/me/verification", {
method: "POST",
json: input,
})
}
export async function listCurrentUserVerifications(): Promise<VerificationRecord[]> {
const res = await httpJson<{ list: VerificationRecord[] | null }>(
"/api/v1/users/me/verification",
{
cache: "no-store",
},
)
return res.list ?? []
}