1
1
This commit is contained in:
@ -8,19 +8,29 @@ const apiRequest = async <T>(
|
|||||||
endpoint: string,
|
endpoint: string,
|
||||||
data: Record<string, any>
|
data: Record<string, any>
|
||||||
): Promise<{ success: boolean; data: T[]; message: string }> => {
|
): Promise<{ success: boolean; data: T[]; message: string }> => {
|
||||||
const response = await fetch(`${API_CONFIG.BASE_URL}${endpoint}`, {
|
const controller = new AbortController();
|
||||||
method: 'POST',
|
const timeout = setTimeout(() => controller.abort(), 15000);
|
||||||
headers: {
|
try {
|
||||||
'Content-Type': 'application/json',
|
const response = await fetch(`${API_CONFIG.BASE_URL}${endpoint}`, {
|
||||||
},
|
method: 'POST',
|
||||||
body: JSON.stringify(data),
|
headers: {
|
||||||
});
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
if (!response.ok) {
|
body: JSON.stringify(data),
|
||||||
throw new Error(`HTTP error! status: ${response.status}`);
|
signal: controller.signal,
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
} catch (e: any) {
|
||||||
|
if (e.name === 'AbortError') {
|
||||||
|
throw new Error('请求超时');
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.json();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user