|
@@ -7,23 +7,23 @@ import type {
|
|
|
|
|
|
|
|
// 创建 axios 适配器
|
|
// 创建 axios 适配器
|
|
|
const axiosFetch = async (url: RequestInfo | URL, init?: RequestInit) => {
|
|
const axiosFetch = async (url: RequestInfo | URL, init?: RequestInit) => {
|
|
|
- const requestHeaders:Record<string, string> = {};
|
|
|
|
|
-
|
|
|
|
|
- if(init?.headers instanceof Headers) {
|
|
|
|
|
|
|
+ const requestHeaders: Record<string, string> = {};
|
|
|
|
|
+
|
|
|
|
|
+ if (init?.headers instanceof Headers) {
|
|
|
init.headers.forEach((value, key) => {
|
|
init.headers.forEach((value, key) => {
|
|
|
requestHeaders[key] = value;
|
|
requestHeaders[key] = value;
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const response = await axios.request({
|
|
|
|
|
- url: url.toString(),
|
|
|
|
|
- method: init?.method || 'GET',
|
|
|
|
|
- headers: requestHeaders,
|
|
|
|
|
- data: init?.body,
|
|
|
|
|
|
|
+ const response = await axios.request({
|
|
|
|
|
+ url: url.toString(),
|
|
|
|
|
+ method: init?.method || 'GET',
|
|
|
|
|
+ headers: requestHeaders,
|
|
|
|
|
+ data: init?.body,
|
|
|
}).catch((error) => {
|
|
}).catch((error) => {
|
|
|
console.log('axiosFetch error', error)
|
|
console.log('axiosFetch error', error)
|
|
|
-
|
|
|
|
|
- if(isAxiosError(error)) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (isAxiosError(error)) {
|
|
|
return {
|
|
return {
|
|
|
status: error.response?.status,
|
|
status: error.response?.status,
|
|
|
statusText: error.response?.statusText,
|
|
statusText: error.response?.statusText,
|
|
@@ -32,7 +32,7 @@ const axiosFetch = async (url: RequestInfo | URL, init?: RequestInit) => {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
throw error;
|
|
throw error;
|
|
|
- })
|
|
|
|
|
|
|
+ })
|
|
|
|
|
|
|
|
const responseHeaders = new Headers();
|
|
const responseHeaders = new Headers();
|
|
|
if (response.headers) {
|
|
if (response.headers) {
|
|
@@ -40,15 +40,15 @@ const axiosFetch = async (url: RequestInfo | URL, init?: RequestInit) => {
|
|
|
responseHeaders.set(key, value);
|
|
responseHeaders.set(key, value);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
// 处理204 No Content响应,不设置body
|
|
// 处理204 No Content响应,不设置body
|
|
|
const body = response.status === 204
|
|
const body = response.status === 204
|
|
|
? null
|
|
? null
|
|
|
: responseHeaders.get('content-type')?.includes('application/json')
|
|
: responseHeaders.get('content-type')?.includes('application/json')
|
|
|
? JSON.stringify(response.data)
|
|
? JSON.stringify(response.data)
|
|
|
: response.data;
|
|
: response.data;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return new Response(
|
|
return new Response(
|
|
|
body,
|
|
body,
|
|
|
{
|
|
{
|
|
@@ -57,7 +57,7 @@ const axiosFetch = async (url: RequestInfo | URL, init?: RequestInit) => {
|
|
|
headers: responseHeaders
|
|
headers: responseHeaders
|
|
|
}
|
|
}
|
|
|
)
|
|
)
|
|
|
-}
|
|
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
|
|
|
export const authClient = hc<AuthRoutes>('/', {
|
|
export const authClient = hc<AuthRoutes>('/', {
|