25 lines
1.3 KiB
TypeScript
25 lines
1.3 KiB
TypeScript
export type OrganizationBalance = {
|
|
organization: { id: string; name: string };
|
|
wallet: { organizationId: string; balanceFen: number; updatedAt: string };
|
|
};
|
|
|
|
export async function fetchOrganizationBalance(organizationId: string, signal?: AbortSignal): Promise<OrganizationBalance> {
|
|
const response = await fetch("/api/billing/balance", { cache: "no-store", signal });
|
|
const payload = await response.json().catch(() => null);
|
|
if (response.status === 401 || response.status === 403 || response.status === 422) {
|
|
throw new Error("登录状态或组织信息已变更,请刷新页面后重试。");
|
|
}
|
|
if (!response.ok) throw new Error("余额暂时无法读取,请重试。");
|
|
if (typeof payload?.organization?.id === "string" && payload.organization.id !== organizationId) {
|
|
throw new Error("账号所属组织已变更,请刷新页面后重试。");
|
|
}
|
|
if (!payload || payload.organization?.id !== organizationId
|
|
|| typeof payload.organization?.name !== "string"
|
|
|| payload.wallet?.organizationId !== organizationId
|
|
|| !Number.isSafeInteger(payload.wallet?.balanceFen)
|
|
|| typeof payload.wallet?.updatedAt !== "string") {
|
|
throw new Error("余额信息不完整,请刷新后重试。");
|
|
}
|
|
return payload as OrganizationBalance;
|
|
}
|