Files
Ydisks-Xianyu-Helper/frontend/app/features/settings/api.ts
T

110 lines
6.5 KiB
TypeScript

import type { OperationResponse,SystemSettings } from '../../../shared/api-contract/settings';
import { contractClient, runContractRequest } from '../../../shared/api-contract/client';
import { type RequestControlOptions } from '../../../shared/http/client';
export type * from '../../../shared/api-contract/settings';
/** 设置页面使用的会话状态传输契约,避免跨 feature 依赖。 */
export interface SettingsSessionStatusResponse {
/** 当前浏览器会话是否已认证。 */
authenticated: boolean;
/** 服务是否已经完成首次管理员初始化。 */
initialized?: boolean;
/** 当前用户的数据库标识。 */
user_id?: number;
/** 当前用户的登录名称。 */
username?: string;
/** 当前用户是否具有管理员权限。 */
is_admin?: boolean;
}
/** 敏感系统设置的显式三态变更命令。 */
export type SensitiveSettingChange = {
/** 敏感值的保存策略。 */
action: 'retain' | 'replace' | 'clear';
/** replace 策略需要保存的新值,只存在于提交请求中。 */
value?: string;
};
/** 系统设置更新请求,将普通配置与敏感命令分开传输。 */
export type SystemSettingsUpdate = {
/** 非敏感配置字段。 */
values?: Record<string, string | number | boolean>;
/** 由服务端解释的敏感值变更命令。 */
secrets?: Record<string, SensitiveSettingChange>;
};
/** SENSITIVE_SYSTEM_SETTING_KEYS 标识不得写入普通 values 的秘密键。 */
const SENSITIVE_SYSTEM_SETTING_KEYS = new Set(['ai_api_key', 'smtp_password', 'qq_reply_secret_key', 'captcha.remote_secret_key']);
/** 将历史设置草稿转换为服务端要求的普通值与敏感命令结构。 */
const normalizeSystemSettingsUpdate = (settings: Partial<SystemSettings> | SystemSettingsUpdate): SystemSettingsUpdate => {
if ('values' in settings || 'secrets' in settings) return settings as SystemSettingsUpdate;
// values 保存普通设置,永不容纳秘密文本。
const values: Record<string, string | number | boolean> = {};
// secrets 保存服务端需要独立处理的敏感设置命令。
const secrets: Record<string, SensitiveSettingChange> = {};
for (const /* key、value 是当前待分流的设置键和值。 */ [key, value] of Object.entries(settings)) {
if (value === undefined || value === null) continue;
if (SENSITIVE_SYSTEM_SETTING_KEYS.has(key)) {
secrets[key] = value === '' ? { action: 'clear' } : { action: 'replace', value: String(value) };
continue;
}
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') values[key] = value;
}
return Object.keys(secrets).length > 0 ? { values, secrets } : values;
};
/** 将服务端可兼容的字符串和数值设置归一为 UI 可消费的只读契约。 */
const normalizeSettings = (settings: Record<string, unknown>): SystemSettings => {
// result 是不修改原始响应的设置副本。
const result: Record<string, unknown> = { ...settings };
// sensitiveKey 是当前需要转为布尔状态的敏感键。
for (const /* sensitiveKey 是当前需要读取配置状态的敏感设置键。 */ sensitiveKey of SENSITIVE_SYSTEM_SETTING_KEYS) {
// configuredKey 是敏感键对应的配置状态字段。
const configuredKey = `${sensitiveKey}_configured`;
if (configuredKey in result) result[configuredKey] = result[configuredKey] === true || result[configuredKey] === 'true';
}
if ('renewal_log_retention_days' in result) {
// days 是完成数值转换后的日志保留天数。
const days = Number(result.renewal_log_retention_days);
result.renewal_log_retention_days = Number.isFinite(days) ? days : 10;
}
return result as SystemSettings;
};
/** 修改当前管理员的密码。 */
export const changePassword = async (currentPassword: string, newPassword: string): Promise<OperationResponse> =>
runContractRequest(/* signal 是本次管理员密码更新请求的超时与取消控制信号。 */ signal => contractClient.POST('/api/v1/session/password', { body: { current_password: currentPassword, new_password: newPassword }, signal }));
/** 修改当前管理员的登录名称与可选密码。 */
export const updateLoginCredentials = async (data: { /** 当前密码用于服务端重新验证身份。 */ current_password: string; /** 新的管理员名称。 */ new_username: string; /** 可选的新密码。 */ new_password?: string }, options?: RequestControlOptions): Promise<OperationResponse> =>
runContractRequest(/* signal 是本次管理员凭据更新请求的超时与取消控制信号。 */ signal => contractClient.PUT('/api/v1/session/credentials', { body: data, signal }), options);
/** 获取系统设置;仅保留敏感值是否已配置的状态,不接收敏感明文。 */
export const getSystemSettings = async (options?: RequestControlOptions): Promise<SystemSettings> => {
// response 是 OpenAPI 约束的脱敏系统设置键值对象。
const response = await runContractRequest(/* signal 是本次系统设置读取请求的超时与取消控制信号。 */ signal => contractClient.GET('/api/v1/settings/system', { signal }), options);
return normalizeSettings(response);
};
/** 保存普通系统配置和敏感设置命令。 */
export const updateSystemSettings = async (settings: Partial<SystemSettings> | SystemSettingsUpdate, options?: RequestControlOptions): Promise<OperationResponse> =>
runContractRequest(/* signal 是本次系统设置更新请求的超时与取消控制信号。 */ signal => contractClient.PUT('/api/v1/settings/system', {
body: normalizeSystemSettingsUpdate(settings),
signal,
}), options);
/** 向服务端请求指定人工智能服务的可用模型列表。 */
export const fetchAIModels = async (baseURL: string, apiKey = '', options?: RequestControlOptions): Promise<string[]> => {
// response 是模型发现接口的具名响应。
const response = await runContractRequest(/* signal 是本次模型发现请求的超时与取消控制信号。 */ signal => contractClient.POST('/api/v1/settings/ai-models', {
body: { base_url: baseURL, api_key: apiKey },
signal,
}), options);
return Array.isArray(response.models) ? response.models : [];
};
/** 在保存登录凭据前读取当前会话状态。 */
export const verifySession = async (options?: RequestControlOptions): Promise<SettingsSessionStatusResponse> =>
runContractRequest(/* signal 是本次设置页会话校验请求的超时与取消控制信号。 */ signal => contractClient.GET('/api/v1/session', { signal }), options);