diff --git a/src/api/request.ts b/src/api/request.ts index bc9d1bf..6d7263e 100644 --- a/src/api/request.ts +++ b/src/api/request.ts @@ -107,3 +107,28 @@ export async function post( } return res.json() as Promise } + +/** + * 带 x-token 等自定义头的 PUT 请求 + */ +export async function put( + path: string, + body?: unknown, + config?: RequestConfig, +): Promise { + const url = new URL(path, BASE_URL || window.location.origin) + const headers: Record = { + 'Content-Type': 'application/json', + 'Accept-Language': i18n.global.locale.value as string, + ...config?.headers, + } + const res = await fetch(url.toString(), { + method: 'PUT', + headers, + body: body !== undefined ? JSON.stringify(body) : undefined, + }) + if (!res.ok) { + throw new Error(`HTTP ${res.status}: ${res.statusText}`) + } + return res.json() as Promise +} diff --git a/src/api/user.ts b/src/api/user.ts index 550a81b..4e6c973 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -1,4 +1,5 @@ -import { get } from './request' +import { get, put } from './request' +import type { ApiResponse } from './types' const USDC_DECIMALS = 1_000_000 @@ -123,3 +124,22 @@ export async function getUsdcBalance( }) return res } + +/** + * PUT /user/setSelfUsername + * 修改自身用户名 + * Body: { username: string } + * 需鉴权(ApiKeyAuth) + */ +export interface ChangeSelfUsernameReq { + username: string +} + +export async function setSelfUsername( + authHeaders: Record, + req: ChangeSelfUsernameReq, +): Promise { + return put('/user/setSelfUsername', req, { + headers: authHeaders, + }) +}