|
@@ -10,11 +10,11 @@ const API_BASE_URL = process.env.TARO_APP_API_BASE_URL || 'http://localhost:3000
|
|
|
// 创建自定义fetch函数,适配Taro.request
|
|
// 创建自定义fetch函数,适配Taro.request
|
|
|
const taroFetch: typeof fetch = async (input, init) => {
|
|
const taroFetch: typeof fetch = async (input, init) => {
|
|
|
const url = typeof input === 'string' ? input : input.url
|
|
const url = typeof input === 'string' ? input : input.url
|
|
|
- const method = init?.method || 'GET'
|
|
|
|
|
|
|
+ const method = init.method || 'GET'
|
|
|
|
|
|
|
|
const requestHeaders: Record<string, string> = {};
|
|
const requestHeaders: Record<string, string> = {};
|
|
|
|
|
|
|
|
- if (init?.headers instanceof Headers) {
|
|
|
|
|
|
|
+ if (init.headers instanceof Headers) {
|
|
|
init.headers.forEach((value, key) => {
|
|
init.headers.forEach((value, key) => {
|
|
|
requestHeaders[key] = value;
|
|
requestHeaders[key] = value;
|
|
|
})
|
|
})
|
|
@@ -24,7 +24,7 @@ const taroFetch: typeof fetch = async (input, init) => {
|
|
|
const options: Taro.request.Option = {
|
|
const options: Taro.request.Option = {
|
|
|
url,
|
|
url,
|
|
|
method: method as any,
|
|
method: method as any,
|
|
|
- data: init?.body,
|
|
|
|
|
|
|
+ data: init.body,
|
|
|
header: requestHeaders
|
|
header: requestHeaders
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -50,7 +50,7 @@ const taroFetch: typeof fetch = async (input, init) => {
|
|
|
// 处理204 No Content响应,不设置body
|
|
// 处理204 No Content响应,不设置body
|
|
|
const body = response.statusCode === 204
|
|
const body = response.statusCode === 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;
|
|
|
|
|
|