juice_network library

Unified network BLoC for Flutter - request coalescing, caching, retry, and interceptors built on the Juice framework.

Quick Start

// Initialize
fetchBloc.send(InitializeFetchEvent(
  config: FetchConfig(baseUrl: 'https://api.example.com'),
));

// Make a request
fetchBloc.send(GetEvent(
  url: '/users/123',
  decode: (raw) => User.fromJson(raw),
  cachePolicy: CachePolicy.cacheFirst,
));

Features

  • Request Coalescing: Multiple requests for the same URL share one network call
  • Cache Policies: networkFirst, cacheFirst, staleWhileRevalidate, etc.
  • Typed Errors: NetworkError, TimeoutError, HttpError, DecodeError
  • Interceptors: Auth, Retry, Logging, ETag
  • Scope Cancellation: Auto-cancel requests when ScopeLifecycleBloc scopes end

Classes

ApiKeyInterceptor
API key authentication interceptor.
AuthInterceptor
Injects authorization headers from a token provider.
CacheManager
Manages HTTP response cache using StorageBloc for persistence.
CacheStats
Cache statistics.
CancelAllEvent
Cancel all inflight requests.
CancelRequestEvent
Cancel a specific request.
CancelScopeEvent
Cancel all requests in a scope.
CertificatePinConfig
Certificate pinning configuration for mobile/desktop.
CleanupExpiredCacheEvent
Clean up expired cache entries.
ClearCacheEvent
Clear all cached responses.
ClearLastErrorEvent
Clear the last error.
DeleteEvent
DELETE request.
ETagInterceptor
Adds conditional request headers for cache validation.
FetchBloc
Unified network BLoC for HTTP requests with caching and coalescing.
FetchConfig
Platform-neutral configuration for FetchBloc.
FetchGroups
Rebuild groups for FetchBloc.
FetchInterceptor
Base class for FetchBloc interceptors.
FetchInterceptorAdapter
Adapter to convert FetchInterceptor to Dio Interceptor.
FetchState
State for FetchBloc.
GetEvent
GET request.
HeadEvent
HEAD request.
HttpDate
Simple HTTP date parser for Retry-After header.
InitializeFetchEvent
Initialize the FetchBloc with configuration.
InMemoryETagCache
Simple ETag cache for in-memory storage.
InterceptorPriority
Recommended priority constants.
InvalidateCacheEvent
Invalidate cache entries.
LoggingInterceptor
Logs requests and responses for debugging.
NetworkStats
Network statistics.
PatchEvent
PATCH request.
PlatformConfig
Platform-specific configuration options.
PostEvent
POST request.
ProxyConfig
Proxy configuration for mobile/desktop.
PruneCacheEvent
Prune cache to target size.
PutEvent
PUT request.
ReconfigureInterceptorsEvent
Reconfigure interceptors on an already-initialized FetchBloc.
RefreshTokenInterceptor
Handles 401 responses with token refresh using singleflight pattern.
RequestCoalescer
Coalesces identical inflight requests into a single network call.
RequestKey
Unique identity for a request, used for coalescing and caching.
RequestStatus
Status of an active or recently completed request.
ResetFetchEvent
Reset FetchBloc to baseline state.
ResetStatsEvent
Reset network statistics.
RetryInterceptor
Implements retry with exponential backoff.
TimingInterceptor
Interceptor to add timing information to requests.
WireCacheRecord
A cached HTTP response stored as raw wire data.

Enums

CachePolicy
Cache policy determining how requests interact with the cache.
RequestPhase
Phase of a request in its lifecycle.
TimeoutType
Type of timeout that occurred.

Extensions

CachePolicyExtension on CachePolicy
Extension methods for CachePolicy.

Exceptions / Errors

CancelledError
Request was cancelled.
ClientError
Client error (4xx status codes).
DecodeError
JSON decode or type conversion failed.
FetchError
Base class for all fetch-related errors.
HttpError
Server returned an error status code.
NetworkError
Network connectivity error - no response received.
ServerError
Server error (5xx status codes).
TimeoutError
Request timed out.