/**
 * 验证插件
 * 
 * 提供请求参数和响应数据验证能力（基于 Zod）
 */ import { RequestError, RequestErrorType, RequestErrorCode } from "../types";
import { PluginPriority } from "./RequestPlugin";
/**
 * 检查是否为 Zod Schema
 */ function isZodSchema(schema) {
    return schema !== null && typeof schema === 'object' && 'parse' in schema && 'safeParse' in schema && typeof schema.parse === 'function' && typeof schema.safeParse === 'function';
}
/**
 * 创建验证插件
 * 
 * @param config - 验证插件配置
 * @returns 验证插件实例
 */ export function createValidationPlugin(config = {}) {
    const { enabled = true, onValidationError } = config;
    return {
        name: 'validation',
        priority: PluginPriority.HIGH,
        async onBeforeRequest (context) {
            if (!enabled) {
                return;
            }
            const validationConfig = context.request.validation;
            if (!validationConfig) {
                return;
            }
            // 验证请求参数
            const requestSchema = validationConfig.requestSchema;
            if (requestSchema && isZodSchema(requestSchema)) {
                try {
                    // 验证查询参数和请求体
                    const requestData = {
                        query: context.request.query,
                        body: context.request.body
                    };
                    const zodSchema = requestSchema;
                    const result = zodSchema.safeParse(requestData);
                    if (!result.success) {
                        const error = new RequestError('请求参数验证失败', RequestErrorType.VALIDATION_ERROR, RequestErrorCode.REQUEST_VALIDATION_FAILED, {
                            request: context.request,
                            context: {
                                validationErrors: result.error.errors
                            }
                        });
                        if (validationConfig.onValidationError) {
                            validationConfig.onValidationError(error);
                        } else if (onValidationError) {
                            onValidationError(error);
                        }
                        throw error;
                    }
                // 如果验证通过，可以使用验证后的数据更新请求
                // 这里可以根据需要更新 context.request
                } catch (error) {
                    // 如果不是验证错误，重新抛出
                    if (error instanceof RequestError && error.type === RequestErrorType.VALIDATION_ERROR) {
                        throw error;
                    }
                    // 其他错误继续抛出
                    throw error;
                }
            }
        },
        async onAfterRequest (context) {
            if (!enabled || !context.response) {
                return;
            }
            const validationConfig = context.request.validation;
            if (!validationConfig) {
                return;
            }
            // 验证响应数据
            const responseSchema = validationConfig.responseSchema;
            if (responseSchema && isZodSchema(responseSchema)) {
                try {
                    const zodSchema = responseSchema;
                    const result = zodSchema.safeParse(context.response.data);
                    if (!result.success) {
                        const error = new RequestError('响应数据验证失败', RequestErrorType.VALIDATION_ERROR, RequestErrorCode.RESPONSE_VALIDATION_FAILED, {
                            request: context.request,
                            response: context.response,
                            context: {
                                validationErrors: result.error.errors
                            }
                        });
                        if (validationConfig.onValidationError) {
                            validationConfig.onValidationError(error);
                        } else if (onValidationError) {
                            onValidationError(error);
                        }
                        throw error;
                    }
                    // 如果验证通过，可以使用验证后的数据更新响应
                    if (result.success && result.data !== context.response.data) {
                        return {
                            ...context.response,
                            data: result.data
                        };
                    }
                } catch (error) {
                    // 如果不是验证错误，重新抛出
                    if (error instanceof RequestError && error.type === RequestErrorType.VALIDATION_ERROR) {
                        throw error;
                    }
                    // 其他错误继续抛出
                    throw error;
                }
            }
        }
    };
}

//# sourceMappingURL=validation.js.map