naweigete-web/service/request.ts

27 lines
645 B
TypeScript
Raw Normal View History

2025-03-12 14:18:25 +08:00
import axios, { type AxiosInstance } from 'axios'
import { handleError } from './handleError'
function createRequestInstance(getServerUrl: () => string): AxiosInstance {
2025-03-19 18:01:04 +08:00
//获取域名
const serverUrl = window.location.origin;
//console.log(serverUrl);
2025-03-12 14:18:25 +08:00
const instance = axios.create({
timeout: 1000 * 60 * 5, // 超时时间
withCredentials: true, // 允许跨域携带cookie
2025-03-19 18:01:04 +08:00
baseURL: serverUrl, // 请求地址
2025-03-12 14:18:25 +08:00
})
instance.interceptors.response.use(
async res => {
return res
},
async err => {
err = await handleError(err)
return Promise.reject(err)
},
)
return instance
}
export default createRequestInstance