import { load } from "@starfederation/datastar/bundles/datastar-core" import * as plugins from "@starfederation/datastar/plugins" // Function to get CSRF token from meta tag const getCSRFToken = () => { const meta = document.querySelector('meta[name="csrf-token"]') return meta ? meta.getAttribute('content') : null } // Helper function to create CSRF-enabled HTTP method handlers const createCSRFHandler = (method) => ({ ...plugins[method], fn: async (ctx, url, args) => { const csrfToken = getCSRFToken() if (!args) args = {} return plugins[method].fn(ctx, url, { ...args, headers: { ...args.headers, 'X-CSRF-Token': csrfToken } }) } }) // Create CSRF-enabled handlers for all HTTP methods const HTTP_METHODS = ['POST', 'DELETE', 'PUT', 'PATCH'] const csrfHandlers = HTTP_METHODS.reduce((acc, method) => { acc[method] = createCSRFHandler(method) return acc }, {}) // Load all plugins, using CSRF-enabled handlers where available Object.keys(plugins).forEach(key => { if (csrfHandlers[key]) { load(csrfHandlers[key]) } else { load(plugins[key]) } })