/**
 * CSRF 防护插件
 * 自动为请求添加 CSRF Token
 */ import { PluginPriority } from "./RequestPlugin";
import { getCSRFManager } from "../../utils/csrf";
/**
 * 创建 CSRF 防护插件
 * 
 * @example
 * ```typescript
 * import { createRequestClient } from '@vlian/framework/request';
 * import { createCSRFPlugin } from '@vlian/framework/request/plugin';
 * 
 * const client = await createRequestClient({
 *   plugins: [
 *     createCSRFPlugin(),
 *   ],
 * });
 * ```
 */ export function createCSRFPlugin() {
    return {
        name: 'csrf-protection',
        priority: PluginPriority.HIGH,
        async onBeforeRequest (context) {
            const csrfManager = getCSRFManager();
            if (!csrfManager) {
                // CSRF 管理器未初始化，跳过
                return;
            }
            // 获取 CSRF Token
            const token = await csrfManager.getToken();
            if (token) {
                // 添加 CSRF Token 到请求头
                const headerName = csrfManager.getHeaderName();
                context.request.headers = {
                    ...context.request.headers,
                    [headerName]: token
                };
            }
        }
    };
}

//# sourceMappingURL=csrfPlugin.js.map