leptos_query_rs/
lib.rs

1//! Leptos Query - A React Query inspired data fetching library for Leptos
2//!
3//! This library provides a powerful and flexible way to manage server state
4//! in Leptos applications, with features like caching, background updates,
5//! optimistic updates, and more.
6//!
7//! ## Features
8//!
9//! - **Declarative Data Fetching**: Write queries as simple functions
10//! - **Automatic Caching**: Built-in cache with configurable stale times
11//! - **Background Updates**: Keep data fresh with background refetching
12//! - **Optimistic Updates**: Update UI immediately with rollback on error
13//! - **Error Handling**: Comprehensive error handling with retry logic
14//! - **Type Safety**: Full type safety with Rust's type system
15//! - **WASM Compatible**: Works in both native and web environments
16//!
17//! ## Quick Start
18//!
19//! ```rust
20//! use leptos::prelude::*;
21//! use leptos_query_rs::*;
22//! use serde::{Deserialize, Serialize};
23//!
24//! #[derive(Clone, Debug, Serialize, Deserialize)]
25//! struct User {
26//!     id: u32,
27//!     name: String,
28//!     email: String,
29//! }
30//!
31//! async fn fetch_user(id: u32) -> Result<User, QueryError> {
32//!     // Your async function here
33//!     Ok(User {
34//!         id,
35//!         name: "John Doe".to_string(),
36//!         email: "john@example.com".to_string(),
37//!     })
38//! }
39//!
40//! #[component]
41//! fn UserProfile(user_id: u32) -> impl IntoView {
42//!     let user_query = use_query(
43//!         move || QueryKey::new(&["user", &user_id.to_string()]),
44//!         move || async move { fetch_user(user_id).await },
45//!         QueryOptions::default(),
46//!     );
47//!
48//!     view! {
49//!         <div>
50//!             {move || {
51//!                 if let Some(user) = user_query.data.get() {
52//!                     format!("User: {}", user.name)
53//!                 } else if user_query.is_loading.get() {
54//!                     "Loading...".to_string()
55//!                 } else {
56//!                     "No user found".to_string()
57//!                 }
58//!             }}
59//!         </div>
60//!     }
61//! }
62//! ```
63
64use leptos::prelude::*;
65
66pub mod client;
67pub mod query;
68pub mod mutation;
69pub mod retry;
70pub mod types;
71pub mod dedup;
72pub mod infinite;
73pub mod persistence;
74pub mod optimistic;
75pub mod devtools;
76pub mod sync;
77
78// Re-export main types and functions
79pub use client::{QueryClient, SerializedData, CacheEntry};
80pub use query::{use_query, QueryOptions, QueryResult};
81pub use mutation::{use_mutation, MutationOptions, MutationResult};
82pub use retry::{QueryError, RetryConfig, execute_with_retry};
83pub use types::{QueryKey, QueryStatus, QueryMeta, QueryKeyPattern, QueryObserverId};
84pub use infinite::{use_infinite_query, InfiniteQueryOptions, InfiniteQueryResult, Page, PageInfo};
85pub use persistence::{PersistenceManager, PersistenceConfig, StorageBackend};
86#[cfg(feature = "persistence")]
87pub use persistence::{LocalStorageBackend, IndexedDBBackend};
88pub use optimistic::{OptimisticManager, OptimisticConfig, OptimisticUpdate, OptimisticStats};
89pub use devtools::{DevToolsManager, DevToolsConfig, DevToolsServer, QueryMetrics, NetworkRequest, CacheOperation, DevToolsEvent, DevToolsExport};
90pub use sync::{SyncManager, ConflictResolutionStrategy, NetworkStatus, SyncResult};
91
92/// Provide the QueryClient context to the app
93#[component]
94pub fn QueryClientProvider(
95    children: Children,
96) -> impl IntoView {
97    let client = QueryClient::new();
98    provide_context(client);
99    
100    children()
101}