阶段二:迁移登录账号与风控接口到生成契约
This commit is contained in:
1 parent
fba0915fd3
commit
5fc32239f6
33 files changed
+939
-145
No files matched your search
@@ -0,0 +1,81 @@
|
||||
import createClient from 'openapi-fetch';
|
||||
|
||||
import { ApiError, controlledSignal, notifyAuthExpired, type RequestControlOptions } from '../http/client';
|
||||
|
||||
import type { paths } from './generated/schema';
|
||||
|
||||
// contractRequestTimeoutMs 是非上传契约请求沿用的默认超时,保持旧 HTTP client 的用户体验。
|
||||
const contractRequestTimeoutMs = 30_000;
|
||||
|
||||
// ContractRequestOptions 是类型化 operation 运行时使用的取消、超时与登录态控制参数。
|
||||
export type ContractRequestOptions = RequestControlOptions & {
|
||||
// skipAuthLogout 防止登录和首次初始化失败时错误清理现有页面状态。
|
||||
skipAuthLogout?: boolean;
|
||||
};
|
||||
|
||||
// ContractResult 是 openapi-fetch 对单个 operation 的成功数据、错误数据和原始响应封装。
|
||||
type ContractResult<T> = {
|
||||
// data 是成功状态码且符合 OpenAPI 响应类型时解析出的数据。
|
||||
data?: T;
|
||||
// error 是非成功状态码解析出的统一错误 envelope 或未知载荷。
|
||||
error?: unknown;
|
||||
// response 是保留状态码和响应头的底层响应对象。
|
||||
response: Response;
|
||||
};
|
||||
|
||||
// contractFetch 为 openapi-fetch 注入现有浏览器 Cookie 与请求地址兼容语义。
|
||||
const contractFetch: typeof fetch = async (input, init) => {
|
||||
// response 是使用既有 Cookie 策略发出的 HTTP 响应。
|
||||
// requestURL 是 openapi-fetch 根据 baseUrl 生成的绝对地址;测试环境需还原为相对路径以复用既有 fetch fixture。
|
||||
const requestURL = new URL(typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url);
|
||||
// request 是合并 openapi-fetch 已构造请求与可选 init 后的完整请求对象。
|
||||
const request = input instanceof Request ? input : new Request(input, init);
|
||||
// body 是恢复为文本的 JSON 请求载荷;GET/HEAD 不能携带请求体。
|
||||
const body = request.method === 'GET' || request.method === 'HEAD' ? undefined : await request.clone().text();
|
||||
// fetchInput 是测试环境还原后的相对 API 地址,浏览器环境继续保留绝对 origin。
|
||||
const fetchInput = requestURL.origin === 'http://localhost' ? `${requestURL.pathname}${requestURL.search}` : input;
|
||||
// response 是使用旧 client 等价方法、请求体、取消信号和 Cookie 策略发出的 HTTP 响应。
|
||||
const response = await fetch(fetchInput, {
|
||||
method: request.method,
|
||||
headers: request.headers,
|
||||
body,
|
||||
signal: request.signal,
|
||||
credentials: 'include',
|
||||
});
|
||||
return response;
|
||||
};
|
||||
|
||||
// contractClient 是唯一持有生成 paths 类型并执行版本化 HTTP operation 的共享实例。
|
||||
// contractBaseUrl 是浏览器生产环境的当前 origin;Node 测试使用可被 fetch fixture 还原的占位 origin。
|
||||
const contractBaseUrl = typeof location === 'undefined' ? 'http://localhost' : location.origin;
|
||||
|
||||
export const contractClient = createClient<paths>({ baseUrl: contractBaseUrl, fetch: contractFetch });
|
||||
|
||||
// runContractRequest 执行类型化 operation,并恢复旧 client 的超时、取消和 ApiError 行为。
|
||||
export async function runContractRequest<T>(
|
||||
execute: (signal: AbortSignal) => Promise<ContractResult<T>>,
|
||||
options: ContractRequestOptions = {},
|
||||
): Promise<T> {
|
||||
// control 统一管理外部 AbortSignal 与默认请求超时。
|
||||
const control = controlledSignal(options.signal, options.timeoutMs ?? contractRequestTimeoutMs);
|
||||
try {
|
||||
// result 是 openapi-fetch 返回的成功或错误响应封装。
|
||||
const result = await execute(control.signal);
|
||||
if (result.response.status === 401 && !options.skipAuthLogout) notifyAuthExpired();
|
||||
if (result.error !== undefined) {
|
||||
throw new ApiError(result.response.status, result.error);
|
||||
}
|
||||
if (result.data === undefined) {
|
||||
throw new ApiError(result.response.status, undefined);
|
||||
}
|
||||
return result.data;
|
||||
} catch (error /* error 是底层 fetch、响应解析或 ApiError 抛出的原始失败原因。 */) {
|
||||
// error 是底层 fetch、响应解析或 ApiError 抛出的原始失败原因。
|
||||
if (control.signal.aborted) {
|
||||
throw new Error(options.signal?.aborted ? '请求已取消' : '请求超时,请稍后重试');
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
control.cleanup();
|
||||
}
|
||||
}
|
||||
@@ -1893,6 +1893,95 @@ export interface components {
|
||||
AccountStatusRequest: {
|
||||
enabled: boolean;
|
||||
};
|
||||
AccountSettingsUpdateRequest: {
|
||||
cookie?: string;
|
||||
remark?: string;
|
||||
auto_confirm?: boolean;
|
||||
pause_duration?: number;
|
||||
username?: string;
|
||||
login_password?: string;
|
||||
clear_password?: boolean;
|
||||
show_browser?: boolean;
|
||||
channel_ids?: number[];
|
||||
};
|
||||
AccountLoginInfoRequest: {
|
||||
username?: string;
|
||||
login_password?: string;
|
||||
clear_password?: boolean;
|
||||
show_browser?: boolean;
|
||||
};
|
||||
LongLoginSettingsRequest: {
|
||||
enabled: boolean;
|
||||
};
|
||||
LongLoginResponse: {
|
||||
can_open_long_login: boolean;
|
||||
enabled: boolean;
|
||||
};
|
||||
AutoConfirmRequest: {
|
||||
auto_confirm: boolean;
|
||||
};
|
||||
AutoConfirmResponse: {
|
||||
auto_confirm: boolean;
|
||||
};
|
||||
AccountRemarkRequest: {
|
||||
remark: string;
|
||||
};
|
||||
PauseDurationRequest: {
|
||||
pause_duration: number;
|
||||
};
|
||||
PauseDurationResponse: {
|
||||
pause_duration: number;
|
||||
paused_until: number;
|
||||
paused: boolean;
|
||||
};
|
||||
CookieSettingsResponse: {
|
||||
success: boolean;
|
||||
paused_until: number;
|
||||
paused: boolean;
|
||||
};
|
||||
CookieProfileResponse: {
|
||||
success: boolean;
|
||||
id: string;
|
||||
nickname: string;
|
||||
avatar_url: string;
|
||||
profile_error: string;
|
||||
};
|
||||
RuntimeStatus: {
|
||||
/** @enum {string} */
|
||||
state: "starting" | "connecting" | "online" | "reconnecting" | "auth_expired" | "verification_required" | "runtime_conflict" | "error" | "stopped" | "disabled";
|
||||
message?: string;
|
||||
connected: boolean;
|
||||
failures: number;
|
||||
/** Format: date-time */
|
||||
updated_at: string;
|
||||
};
|
||||
RuntimeStatusMap: {
|
||||
[key: string]: components["schemas"]["RuntimeStatus"];
|
||||
};
|
||||
AIModelsRequest: {
|
||||
base_url: string;
|
||||
api_key?: string;
|
||||
};
|
||||
AIModelsResponse: {
|
||||
models: string[];
|
||||
};
|
||||
SecretSettingChange: {
|
||||
/** @enum {string} */
|
||||
action: "retain" | "replace" | "clear";
|
||||
value?: string;
|
||||
};
|
||||
SystemSettingValueMap: {
|
||||
[key: string]: string | number | boolean;
|
||||
};
|
||||
SystemSettingsUpdateRequest: {
|
||||
values?: components["schemas"]["SystemSettingValueMap"];
|
||||
secrets?: {
|
||||
[key: string]: components["schemas"]["SecretSettingChange"];
|
||||
};
|
||||
};
|
||||
SystemSettingsResponse: {
|
||||
[key: string]: string | number | boolean;
|
||||
};
|
||||
SessionStatusResponse: {
|
||||
authenticated: boolean;
|
||||
initialized: boolean;
|
||||
@@ -2571,7 +2660,7 @@ export interface operations {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ApiResponse"];
|
||||
"application/json": components["schemas"]["AutoConfirmResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
@@ -2630,7 +2719,11 @@ export interface operations {
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["AutoConfirmRequest"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description 成功 */
|
||||
200: {
|
||||
@@ -2638,7 +2731,7 @@ export interface operations {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ApiResponse"];
|
||||
"application/json": components["schemas"]["OperationResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
@@ -2697,7 +2790,11 @@ export interface operations {
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["AccountLoginInfoRequest"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description 成功 */
|
||||
200: {
|
||||
@@ -2705,7 +2802,7 @@ export interface operations {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ApiResponse"];
|
||||
"application/json": components["schemas"]["OperationResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
@@ -2772,7 +2869,7 @@ export interface operations {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ApiResponse"];
|
||||
"application/json": components["schemas"]["LongLoginResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
@@ -2831,7 +2928,11 @@ export interface operations {
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["LongLoginSettingsRequest"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description 成功 */
|
||||
200: {
|
||||
@@ -2839,7 +2940,7 @@ export interface operations {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ApiResponse"];
|
||||
"application/json": components["schemas"]["LongLoginResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
@@ -2906,7 +3007,7 @@ export interface operations {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ApiResponse"];
|
||||
"application/json": components["schemas"]["PauseDurationResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
@@ -2965,7 +3066,11 @@ export interface operations {
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["PauseDurationRequest"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description 成功 */
|
||||
200: {
|
||||
@@ -2973,7 +3078,7 @@ export interface operations {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ApiResponse"];
|
||||
"application/json": components["schemas"]["CookieSettingsResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
@@ -3040,7 +3145,7 @@ export interface operations {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ApiResponse"];
|
||||
"application/json": components["schemas"]["CookieProfileResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
@@ -3099,7 +3204,11 @@ export interface operations {
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["AccountRemarkRequest"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description 成功 */
|
||||
200: {
|
||||
@@ -3107,7 +3216,7 @@ export interface operations {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ApiResponse"];
|
||||
"application/json": components["schemas"]["OperationResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
@@ -3166,7 +3275,11 @@ export interface operations {
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["AccountSettingsUpdateRequest"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description 成功 */
|
||||
200: {
|
||||
@@ -3174,7 +3287,7 @@ export interface operations {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ApiResponse"];
|
||||
"application/json": components["schemas"]["CookieSettingsResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
@@ -3375,7 +3488,7 @@ export interface operations {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ApiResponse"];
|
||||
"application/json": components["schemas"]["RuntimeStatusMap"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
@@ -10301,7 +10414,11 @@ export interface operations {
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["AIModelsRequest"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description 成功 */
|
||||
200: {
|
||||
@@ -10309,7 +10426,7 @@ export interface operations {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ApiResponse"];
|
||||
"application/json": components["schemas"]["AIModelsResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
@@ -10573,7 +10690,7 @@ export interface operations {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ApiResponse"];
|
||||
"application/json": components["schemas"]["SystemSettingsResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
@@ -10630,7 +10747,11 @@ export interface operations {
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["SystemSettingsUpdateRequest"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description 成功 */
|
||||
200: {
|
||||
@@ -10638,7 +10759,7 @@ export interface operations {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ApiResponse"];
|
||||
"application/json": components["schemas"]["OperationResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
|
||||
Reference in new issue
Block a user