From 6f5a9c8c3124453cd713693631696c3b64911dde Mon Sep 17 00:00:00 2001 From: MidoriKurage Date: Sun, 6 Sep 2026 11:46:50 +0800 Subject: [PATCH] feat(frontend): expose HTTP status on API responses via $httpStatus --- frontend/src/utils/request.ts | 80 +++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/frontend/src/utils/request.ts b/frontend/src/utils/request.ts index 438a3780..7f95107e 100644 --- a/frontend/src/utils/request.ts +++ b/frontend/src/utils/request.ts @@ -10,6 +10,40 @@ const t = (key: string) => i18n.global.t(key) // API基础URL const BASE_URL = getApiBaseUrl(); +/** + * Response payload augmented with the HTTP status code. + * + * `$httpStatus` lets callers distinguish outcomes that share a success shape. + * Defined as a non-enumerable property, so it stays invisible to object spread, + * JSON.stringify and Object.keys and never leaks into downstream payloads. + * + * Guaranteed only for JSON responses (objects/arrays). Blob, string and SSE + * stream responses do not carry it at runtime, so only read `$httpStatus` + * when the payload is known to be an object. + */ +export type WithStatus = T & { + /** HTTP status code of the response. Non-enumerable. See {@link WithStatus}. */ + readonly $httpStatus: number +}; + +const HTTP_STATUS_KEY = '$httpStatus'; + +/** + * Attach the non-enumerable `$httpStatus` property to a response payload + * in place and return it. Primitives pass through untouched. + * See {@link WithStatus} for where the property is guaranteed. + */ +function withHttpStatus(data: T, status: number): T { + if (data !== null && typeof data === 'object') { + Object.defineProperty(data, HTTP_STATUS_KEY, { + value: status, + enumerable: false, + configurable: true, + writable: false, + }); + } + return data; +} // 创建Axios实例 const instance = axios.create({ @@ -115,9 +149,9 @@ instance.interceptors.response.use( // 根据业务状态码处理逻辑 const { status, data } = response; if (status >= 200 && status < 300) { - return data; + return withHttpStatus(data, status); } else { - return Promise.reject(data); + return Promise.reject(withHttpStatus(data, status)); } }, async (error: any) => { @@ -133,7 +167,7 @@ instance.interceptors.response.use( const msg = typeof data === 'object' ? (typeof data?.error === 'string' ? data.error : (data?.error?.message || data?.message)) : data; - return Promise.reject({ status, message: msg || t('error.invalidCredentials') }); + return Promise.reject(withHttpStatus({ status, message: msg || t('error.invalidCredentials') }, status)); } // Embed 调试页/挂件:无 JWT 时直接拒绝,勿走 refresh → /login @@ -142,7 +176,7 @@ instance.interceptors.response.use( const msg = typeof data === 'object' ? (typeof data?.error === 'string' ? data.error : (data?.error?.message || data?.message)) : data; - return Promise.reject({ status, message: msg || t('error.invalidCredentials') }); + return Promise.reject(withHttpStatus({ status, message: msg || t('error.invalidCredentials') }, status)); } // 如果是401错误且不是刷新token的请求,尝试刷新token @@ -215,15 +249,16 @@ instance.interceptors.response.use( } // 处理 Nginx 413 Request Entity Too Large - if (error.response.status === 413) { + const ERR_ENTITY_TOO_LARGE = 413; + if (error.response.status === ERR_ENTITY_TOO_LARGE) { const skillUpload = isSkillBundleUploadUrl(error.config?.url) - return Promise.reject({ - status: 413, + return Promise.reject(withHttpStatus({ + status: ERR_ENTITY_TOO_LARGE, message: skillUpload ? i18n.global.t('settings.sandbox.skillBundleTooLarge', { size: MAX_SKILL_BUNDLE_SIZE_MB }) : i18n.global.t('error.fileSizeExceeded', { size: MAX_FILE_SIZE_MB }), success: false - }); + }, ERR_ENTITY_TOO_LARGE)); } const { status, data } = error.response; @@ -242,16 +277,16 @@ instance.interceptors.response.use( } else if (typeof data === 'string') { errorMessage = data; } - return Promise.reject({ - status, + return Promise.reject(withHttpStatus({ + status, message: errorMessage, ...(typeof data === 'object' ? data : {}) - }); + }, status)); } ); -export function get(url: string, config?: any): Promise { - return instance.get(url, config) as unknown as Promise; +export function get(url: string, config?: any): Promise> { + return instance.get(url, config) as unknown as Promise>; } export async function getDown(url: string): Promise { @@ -266,7 +301,7 @@ export function postUpload( data = {}, onUploadProgress?: (progressEvent: any) => void, config: any = {}, -): Promise { +): Promise> { return instance.post(url, data, { ...config, headers: { @@ -279,6 +314,7 @@ export function postUpload( } export function postChat(url: string, data = {}): Promise { + // SSE stream: body is a string, so no `$httpStatus` is attached (see WithStatus). return instance.post(url, data, { headers: { "Content-Type": "text/event-stream;charset=utf-8", @@ -287,18 +323,18 @@ export function postChat(url: string, data = {}): Promise { }) as unknown as Promise; } -export function post(url: string, data = {}, config?: any): Promise { - return instance.post(url, data, config) as unknown as Promise; +export function post(url: string, data = {}, config?: any): Promise> { + return instance.post(url, data, config) as unknown as Promise>; } -export function put(url: string, data = {}, config?: any): Promise { - return instance.put(url, data, config) as unknown as Promise; +export function put(url: string, data = {}, config?: any): Promise> { + return instance.put(url, data, config) as unknown as Promise>; } -export function patch(url: string, data = {}, config?: any): Promise { - return instance.patch(url, data, config) as unknown as Promise; +export function patch(url: string, data = {}, config?: any): Promise> { + return instance.patch(url, data, config) as unknown as Promise>; } -export function del(url: string, data?: any): Promise { - return instance.delete(url, { data }) as unknown as Promise; +export function del(url: string, data?: any): Promise> { + return instance.delete(url, { data }) as unknown as Promise>; }