UNPKG

@wizecorp/stratusjs

Version:
63 lines 2.47 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import React from 'react'; /** * Compose multiple middleware together */ export function composeMiddleware(...middlewares) { return (Component) => { return middlewares.reduceRight((acc, middleware) => middleware(acc), Component); }; } export const withAuth = (options = {}) => { const { isAuthenticated = () => typeof window !== 'undefined' && !!localStorage.getItem('token'), redirectTo = '/login', fallback = null } = options; return (Component) => { return (props) => { // Client-side verification only if (typeof window === 'undefined') { return fallback; } if (!isAuthenticated()) { // Use Stratus router instead of window.location window.location.replace(redirectTo); return fallback; } return _jsx(Component, { ...props }); }; }; }; /** * Layout middleware (simplified version) */ export const withLayout = (Layout) => { return (Component) => { return (props) => (_jsx(Layout, { children: _jsx(Component, { ...props }) })); }; }; export const withErrorBoundary = (options = {}) => { const { fallback: Fallback, onError } = options; return (Component) => { return (props) => { const [error, setError] = React.useState(null); React.useEffect(() => { const handleError = (event) => { setError(new Error(event.message)); onError?.(new Error(event.message), { stack: event.error?.stack }); }; window.addEventListener('error', handleError); return () => window.removeEventListener('error', handleError); }, []); if (error) { if (Fallback) { return _jsx(Fallback, { error: error, retry: () => setError(null) }); } return (_jsxs("div", { children: [_jsx("h2", { children: "Error" }), _jsx("p", { children: error.message }), _jsx("button", { onClick: () => setError(null), children: "Retry" })] })); } return _jsx(Component, { ...props }); }; }; }; // Utility to easily create custom middleware export const createMiddleware = (fn) => { return (...args) => (Component) => fn(Component, ...args); }; //# sourceMappingURL=index.js.map