Middleware
Using middleware in Aurelia Store to intercept and transform state changes
How Middleware Works
Creating Middleware
import { IStateMiddleware } from '@aurelia/state';
// Basic middleware (no settings)
const loggingMiddleware: IStateMiddleware<MyState> = (state, action, settings) => {
console.log('Action dispatched:', action);
console.log('Current state:', state);
// Return undefined to pass through without changes
};
// Middleware with settings
interface ValidationSettings {
strict: boolean;
}
const validationMiddleware: IStateMiddleware<MyState, ValidationSettings> = (state, action, settings) => {
if (settings.strict && !isValidAction(action)) {
return false; // Block the action
}
// Pass through
};Registering Middleware
Common Middleware Patterns
Logging Middleware
Validation Middleware
Transform Middleware
Async Middleware
Analytics Middleware
Middleware Execution Order
Blocking Actions
Type Safety
Best Practices
Last updated
Was this helpful?