This commit is contained in:
2026-08-06 18:02:02 +08:00
parent 9aa79bef6d
commit 20f8a436e0
6 changed files with 69 additions and 31 deletions
+3 -7
View File
@@ -1,3 +1,5 @@
import type { ApiFieldErrors } from '@/api/errorDetails'
export interface ApiEnvelope<T> {
code: string
message: string
@@ -7,7 +9,7 @@ export interface ApiEnvelope<T> {
timestamp: string
}
export type ApiFieldErrors = Record<string, string>
export type { ApiFieldErrors } from '@/api/errorDetails'
export class ApiError extends Error {
constructor(
@@ -27,9 +29,3 @@ export function isApiEnvelope(value: unknown): value is ApiEnvelope<unknown> {
const record = value as Record<string, unknown>
return typeof record.code === 'string' && typeof record.message === 'string' && 'data' in record
}
export function asFieldErrors(value: unknown): ApiFieldErrors | undefined {
if (!value || typeof value !== 'object' || Array.isArray(value)) return undefined
const entries = Object.entries(value).filter((entry): entry is [string, string] => typeof entry[1] === 'string')
return entries.length > 0 ? Object.fromEntries(entries) : undefined
}