17 lines
661 B
TypeScript
17 lines
661 B
TypeScript
const API_BASE = import.meta.env.VITE_SUPPLIED_ADMIN_API_BASE || 'http://127.0.0.1:8888/api/admin'
|
|
|
|
const normalizeBase = (base: string) => base.replace(/\/$/, '')
|
|
|
|
export const postAdmin = async <T>(name: string, body: Record<string, unknown>): Promise<T> => {
|
|
const response = await fetch(`${normalizeBase(API_BASE)}/${name}`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(body)
|
|
})
|
|
const payload = await response.json().catch(() => ({}))
|
|
if (!response.ok || payload.success === false) {
|
|
throw new Error(payload.error?.message || payload.msg || '请求失败')
|
|
}
|
|
return payload.data as T
|
|
}
|