58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
|
|
// Response interface
|
|||
|
|
interface ApiResponse<T = any> {
|
|||
|
|
success: boolean,
|
|||
|
|
error: string,
|
|||
|
|
data: T,
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export async function apiWrapper<T>(url: string, data: Record<string, any>): Promise<T | undefined> {
|
|||
|
|
try {
|
|||
|
|
// 自动编码为 key=value&key2=value2 格式
|
|||
|
|
const params = new URLSearchParams();
|
|||
|
|
Object.entries(data).forEach(([key, value]) => {
|
|||
|
|
params.append(key, String(value));
|
|||
|
|
});
|
|||
|
|
// 发起请求
|
|||
|
|
const response = await fetch(url, {
|
|||
|
|
method: "POST",
|
|||
|
|
mode: "cors",
|
|||
|
|
cache: "no-cache",
|
|||
|
|
credentials: "same-origin",
|
|||
|
|
headers: {
|
|||
|
|
// 明确指定内容类型
|
|||
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|||
|
|
},
|
|||
|
|
redirect: "follow",
|
|||
|
|
referrerPolicy: "no-referrer",
|
|||
|
|
body: params.toString(),
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 检查 HTTP 状态码 (fetch 只有在网络故障时才会 reject,HTTP 404/500 不会)
|
|||
|
|
if (!response.ok) {
|
|||
|
|
console.error(`HTTP failed: ${response.status}`);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 解析 JSON body
|
|||
|
|
// 注意:response.json() 返回的是一个 Promise,所以需要 await
|
|||
|
|
const payload = await response.json() as ApiResponse<T>;
|
|||
|
|
|
|||
|
|
// 检查API返回结果
|
|||
|
|
if (payload.success) {
|
|||
|
|
return payload.data;
|
|||
|
|
} else {
|
|||
|
|
console.error(`API failed: ${payload.error}`);
|
|||
|
|
return undefined;
|
|||
|
|
}
|
|||
|
|
} catch (error) {
|
|||
|
|
// 统一错误处理
|
|||
|
|
console.error(`Fetch failed: ${error}`);
|
|||
|
|
return undefined;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export async function boolApiWrapper<U>(url: string, data: Record<string, any>): Promise<boolean> {
|
|||
|
|
const rv = await apiWrapper<null>(url, data);
|
|||
|
|
return rv !== undefined;
|
|||
|
|
}
|
|||
|
|
|