mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-21 06:32:20 +03:00
* fix(frontend): preserve request cancellation and schema failures * fix(frontend): limit schema failures to query boundaries * fix(frontend): keep invalid settings recoverable Keep settings payload validation tolerant so values accepted by the backend remain editable, while paged clients still fail closed. Add an AbortSignal.any fallback and make timeout tests event-driven. --------- Co-authored-by: PathGao <gaoyanbo@gaoyanbodeMacBook-Air.local>
25 lines
690 B
TypeScript
25 lines
690 B
TypeScript
import type { z } from 'zod';
|
|
import { Msg } from '@/utils';
|
|
|
|
interface ParseMsgOptions {
|
|
strict?: boolean;
|
|
}
|
|
|
|
export function parseMsg<T extends z.ZodType>(
|
|
msg: Msg<unknown>,
|
|
schema: T,
|
|
context: string,
|
|
options: ParseMsgOptions = {},
|
|
): Msg<z.infer<T>> {
|
|
if (!msg.success || msg.obj == null) {
|
|
return msg as Msg<z.infer<T>>;
|
|
}
|
|
const result = schema.safeParse(msg.obj);
|
|
if (!result.success) {
|
|
console.warn(`[zod] ${context} response failed validation`, result.error.issues);
|
|
if (options.strict) throw new Error(`${context} response failed validation`);
|
|
return msg as Msg<z.infer<T>>;
|
|
}
|
|
return new Msg<z.infer<T>>(msg.success, msg.msg, result.data);
|
|
}
|