@energica-city/shared-amplify-utils
Version:
Shared utilities for AWS Amplify projects
46 lines • 2.72 kB
TypeScript
import type { WebSocketModelInitializerConfig, WebSocketInputWithModels, WebSocketResponse } from './types';
import type { Middleware } from '../middlewareChain';
import type { AmplifyModelType } from '../../queries/types';
/**
* Create a WebSocket model initializer middleware
*
* This middleware initializes Amplify Data model query factories for WebSocket handlers.
* It handles lazy initialization with caching, timeout protection, optional in-memory caching,
* and automatic retry logic on initialization failures.
*
* **Initialization Flow:**
* 1. Delegates to ClientManager.initializeQueries() for all initialization logic
* 2. ClientManager handles global state management and caching automatically
* 3. Initializes Amplify client and query factories for specified entities
* 4. Adds initialized models to input object for subsequent middleware
* 5. Handles initialization failures with proper error responses
*
* **Caching Behavior:**
* - ClientManager handles all caching and state management globally
* - Models are initialized once per clientKey and cached automatically
* - Concurrent requests are handled efficiently by ClientManager
* - Optional in-memory caching with LRU eviction for query results
*
* **Error Handling:**
* - Initialization timeout: Returns 500 error response
* - Amplify client errors: Returns 500 error response with details
* - Post-initialization errors: Re-throws to be handled by error middleware
*
* **Performance Considerations:**
* - First request bears initialization cost (typically 100-500ms)
* - Subsequent requests use ClientManager's cached models (near-zero overhead)
* - Timeout protection prevents hanging Lambda functions
* - Optional query caching improves read performance
* - No duplicate state management - relies on ClientManager singleton
*
* @template TSchema - Amplify Data schema type with models property
* @template TTypes - Record of available Amplify model types
* @template TSelected - Selected model types to initialize (subset of TTypes)
* @template TReturn - Expected return type of the WebSocket handler
* @param config - Configuration for model initialization
* @returns Middleware function that adds initialized models to the input
*/
export declare function createWebSocketModelInitializer<TSchema extends {
models: Record<string, unknown>;
}, TTypes extends Record<string, AmplifyModelType>, TSelected extends keyof TTypes & string = keyof TTypes & string, TReturn extends WebSocketResponse = WebSocketResponse>(config: WebSocketModelInitializerConfig<TSchema, TTypes, TSelected>): Middleware<WebSocketInputWithModels<TTypes, TSelected>, TReturn>;
//# sourceMappingURL=WebSocketModelInitializer.d.ts.map