XuqmGroup-RNSDK/packages/common/src/api/diagnostics.ts

59 行
1.4 KiB
TypeScript

2026-07-18 08:35:36 +08:00
import type { AxiosError, AxiosResponse } from 'axios'
import type { z } from 'zod'
type SafeIssue = {
code: string
message: string
path: string
}
export type SafeApiDiagnostic = {
code?: string
httpStatus?: number
issues?: SafeIssue[]
method?: string
path?: string
type: 'AxiosError' | 'Response' | 'ValidationError'
}
/**
* / bodyheaders URL
* userIdsessionId
*/
function requestShape(response: AxiosResponse<unknown>) {
return {
httpStatus: response.status,
method: response.config.method?.toUpperCase(),
path: response.config.url,
}
}
export function createResponseDiagnostic(response: AxiosResponse<unknown>): SafeApiDiagnostic {
return { type: 'Response', ...requestShape(response) }
}
export function createValidationDiagnostic(
response: AxiosResponse<unknown>,
issues: z.ZodIssue[],
): SafeApiDiagnostic {
return {
type: 'ValidationError',
...requestShape(response),
issues: issues.map(issue => ({
code: issue.code,
message: issue.message,
path: issue.path.join('.'),
})),
}
}
export function createAxiosDiagnostic(error: AxiosError): SafeApiDiagnostic {
return {
type: 'AxiosError',
code: error.code,
httpStatus: error.response?.status,
method: error.config?.method?.toUpperCase(),
path: error.config?.url,
}
}