优化
This commit is contained in:
+3
-7
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user