feat: 前端接口代理

This commit is contained in:
Gsh
2025-06-19 23:45:22 +08:00
parent bc91a8cff2
commit a89e11d132
12 changed files with 133 additions and 7 deletions

View File

@@ -12,12 +12,37 @@ interface BaseResponse {
rows: never;
}
// 修改 BaseResponse 类型,使其兼容裸数组
type BaseResponse<T = any> =
| { code: number; data: T; msg: string } // 标准格式
| T[]; // 裸数组格式
export const request = hookFetch.create<BaseResponse, 'data' | 'rows'>({
baseURL: import.meta.env.VITE_API_URL,
// baseURL: import.meta.env.VITE_API_URL,
baseURL: '', // 留空或使用'/'
headers: {
'Content-Type': 'application/json',
},
plugins: [sseTextDecoderPlugin({ json: true, prefix: 'data:' })],
plugins: [
sseTextDecoderPlugin({ json: true, prefix: 'data:' }),
{
name: 'adapt-array-response',
afterResponse: async (response) => {
// 如果是数组格式,手动包裹成 { data: [...] }
if (Array.isArray(response.result)) {
return {
...response,
result: {
code: 200,
msg: 'success',
data: response.result,
},
};
}
return response;
},
},
],
});
function jwtPlugin(): HookFetchPlugin<BaseResponse> {