Skip to main content

deckyfx_dioxus_ipc_bridge/
plugin.rs

1//! Plugin system for extending bridge functionality
2//!
3//! Plugins can inject custom JavaScript, intercept requests/responses,
4//! and add middleware to the IPC bridge.
5
6use crate::request::IpcRequest;
7use crate::response::{IpcError, IpcResponse};
8
9/// Trait for implementing bridge plugins
10///
11/// Plugins can extend the bridge with custom functionality such as:
12/// - Logging and monitoring
13/// - Authentication and authorization
14/// - Request/response transformation
15/// - Custom JavaScript injection
16///
17/// # Example
18/// ```rust,ignore
19/// use dioxus_ipc_bridge::plugin::BridgePlugin;
20///
21/// struct LoggingPlugin;
22///
23/// impl BridgePlugin for LoggingPlugin {
24///     fn name(&self) -> &str {
25///         "logging"
26///     }
27///
28///     fn inject_js(&self) -> Option<String> {
29///         Some("console.log('[Plugin] Logging enabled');".to_string())
30///     }
31///
32///     fn on_request(&self, req: &mut IpcRequest) -> Result<(), IpcError> {
33///         println!("Request: {} {}", req.method, req.url);
34///         Ok(())
35///     }
36/// }
37/// ```
38pub trait BridgePlugin: Send + Sync {
39    /// Plugin identifier name
40    fn name(&self) -> &str;
41
42    /// Optional JavaScript code to inject during bridge initialization
43    ///
44    /// This code will be executed after the core bridge is set up
45    fn inject_js(&self) -> Option<String> {
46        None
47    }
48
49    /// Called before a request is processed
50    ///
51    /// Plugins can modify the request or return an error to reject it
52    fn on_request(&self, _req: &mut IpcRequest) -> Result<(), IpcError> {
53        Ok(())
54    }
55
56    /// Called after a response is generated
57    ///
58    /// Plugins can modify the response before it's sent to JavaScript
59    fn on_response(&self, _res: &mut IpcResponse) -> Result<(), IpcError> {
60        Ok(())
61    }
62}
63
64/// Trait for implementing middleware
65///
66/// Middleware provides a more flexible way to intercept and transform
67/// requests and responses with a chain-of-responsibility pattern.
68///
69/// # Example
70/// ```rust,ignore
71/// use dioxus_ipc_bridge::prelude::*;
72///
73/// struct AuthMiddleware {
74///     token: String,
75/// }
76///
77/// impl Middleware for AuthMiddleware {
78///     fn handle(
79///         &self,
80///         req: &EnrichedRequest,
81///         next: &dyn Fn(&EnrichedRequest) -> Result<IpcResponse, IpcError>,
82///     ) -> Result<IpcResponse, IpcError> {
83///         // Check auth token
84///         if req.headers.get("Authorization") != Some(&self.token) {
85///             return Err(IpcError::Unauthorized);
86///         }
87///         // Continue chain
88///         next(req)
89///     }
90/// }
91/// ```
92pub trait Middleware: Send + Sync {
93    /// Process a request through the middleware chain
94    ///
95    /// # Arguments
96    /// * `req` - The enriched request with parsed parameters
97    /// * `next` - Function to call the next middleware or route handler
98    fn handle(
99        &self,
100        req: &crate::request::EnrichedRequest,
101        next: &dyn Fn(&crate::request::EnrichedRequest) -> Result<IpcResponse, IpcError>,
102    ) -> Result<IpcResponse, IpcError>;
103}