feat: 前端接口代理
This commit is contained in:
@@ -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> {
|
||||
|
||||
Reference in New Issue
Block a user