refactor(web): update API service, view script and add tests
Frontend: - services/api.js: update API service client - views/scripts/EmployeeManagementView.js: update employee management view script - tests/api-request.test.mjs: update API request tests Scripts: - create_employee.sh: add employee creation script
This commit is contained in:
@@ -90,18 +90,78 @@ export function getRuntimeApiBaseUrl() {
|
||||
return runtimeApiBaseUrl
|
||||
}
|
||||
|
||||
function buildUrl(path) {
|
||||
if (!path.startsWith('/')) {
|
||||
return `${runtimeApiBaseUrl}/${path}`
|
||||
}
|
||||
|
||||
return `${runtimeApiBaseUrl}${path}`
|
||||
}
|
||||
|
||||
export async function apiRequest(path, options = {}) {
|
||||
const {
|
||||
contentType = 'application/json',
|
||||
responseType = 'json',
|
||||
function buildUrl(path) {
|
||||
if (!path.startsWith('/')) {
|
||||
return `${runtimeApiBaseUrl}/${path}`
|
||||
}
|
||||
|
||||
return `${runtimeApiBaseUrl}${path}`
|
||||
}
|
||||
|
||||
function formatValidationLocation(loc) {
|
||||
if (!Array.isArray(loc)) {
|
||||
return ''
|
||||
}
|
||||
|
||||
return loc
|
||||
.filter((item) => item !== 'body')
|
||||
.map((item) => String(item || '').trim())
|
||||
.filter(Boolean)
|
||||
.join('.')
|
||||
}
|
||||
|
||||
function resolveErrorMessage(payload, fallback = '接口请求失败,请稍后重试。') {
|
||||
const detail = payload?.detail
|
||||
|
||||
if (typeof detail === 'string' && detail.trim()) {
|
||||
return detail.trim()
|
||||
}
|
||||
|
||||
if (Array.isArray(detail) && detail.length) {
|
||||
const messages = detail
|
||||
.map((item) => {
|
||||
if (typeof item === 'string' && item.trim()) {
|
||||
return item.trim()
|
||||
}
|
||||
|
||||
if (!item || typeof item !== 'object') {
|
||||
return ''
|
||||
}
|
||||
|
||||
const message = String(item.msg || item.message || '').trim()
|
||||
const location = formatValidationLocation(item.loc)
|
||||
|
||||
if (location && message) {
|
||||
return `${location}: ${message}`
|
||||
}
|
||||
|
||||
return message
|
||||
})
|
||||
.filter(Boolean)
|
||||
|
||||
if (messages.length) {
|
||||
return messages.join(';')
|
||||
}
|
||||
}
|
||||
|
||||
if (detail && typeof detail === 'object') {
|
||||
const message = String(detail.message || detail.msg || '').trim()
|
||||
if (message) {
|
||||
return message
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof payload?.message === 'string' && payload.message.trim()) {
|
||||
return payload.message.trim()
|
||||
}
|
||||
|
||||
return fallback
|
||||
}
|
||||
|
||||
export async function apiRequest(path, options = {}) {
|
||||
const {
|
||||
contentType = 'application/json',
|
||||
responseType = 'json',
|
||||
headers: customHeaders,
|
||||
...fetchOptions
|
||||
} = options
|
||||
@@ -130,28 +190,28 @@ export async function apiRequest(path, options = {}) {
|
||||
if (!response.ok) {
|
||||
let payload = null
|
||||
|
||||
try {
|
||||
payload = await response.json()
|
||||
} catch {
|
||||
payload = null
|
||||
}
|
||||
|
||||
throw new Error(payload?.detail || '接口请求失败,请稍后重试。')
|
||||
}
|
||||
|
||||
return response.blob()
|
||||
}
|
||||
try {
|
||||
payload = await response.json()
|
||||
} catch {
|
||||
payload = null
|
||||
}
|
||||
|
||||
throw new Error(resolveErrorMessage(payload))
|
||||
}
|
||||
|
||||
return response.blob()
|
||||
}
|
||||
|
||||
let payload = null
|
||||
try {
|
||||
payload = await response.json()
|
||||
} catch {
|
||||
payload = null
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(payload?.detail || '接口请求失败,请稍后重试。')
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
} catch {
|
||||
payload = null
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(resolveErrorMessage(payload))
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user