/** * Template Utilities for Dynamic Key Generation * * Provides functionality to parse template strings with placeholders * and resolve them using function parameters at runtime. * * Template Syntax: * - {paramName} - Named parameter access * - {0}, {1} - Positional parameter access * - {user.id} - Nested property access * - {items.0.name} - Array index access * * @example * ```typescript * buildDynamicKey('user:{userId}:profile', ['user123'], ['userId']) * // Returns: 'user:user123:profile' * * buildDynamicKey('{0}:{1}', ['user123', 'profile']) * // Returns: 'user123:profile' * * buildDynamicKey('user:{user.id}:profile', [{ user: { id: 'user123' } }], ['user']) * // Returns: 'user:user123:profile' * ``` */ /** * Token types for template parsing */ export interface TemplateToken { type: 'static' | 'placeholder'; value: string; } /** * Options for template resolution behavior */ export interface KeyTemplateOptions { /** * Behavior when a parameter cannot be resolved * - 'skip': Skip execution and return skipReturnValue (default) * - 'fallback': Use fallbackKey instead * - 'error': Throw an error */ onMissingParam?: 'skip' | 'fallback' | 'error'; /** * Fallback key to use when resolution fails and onMissingParam is 'fallback' * If not provided, defaults to 'ClassName:methodName' */ fallbackKey?: string; } /** * Parse a template string into tokens * Separates static parts from placeholders * * @param template - Template string with placeholders like {paramName} * @returns Array of tokens representing the template structure * * @example * ```typescript * parseTemplate('user:{userId}:profile') * // Returns: [ * // { type: 'static', value: 'user:' }, * // { type: 'placeholder', value: 'userId' }, * // { type: 'static', value: ':profile' } * // ] * ``` */ export declare function parseTemplate(template: string): TemplateToken[]; /** * Resolve a single placeholder value from function arguments * * Supports: * - Positional: {0}, {1}, {2} * - Named path segments: {userId}, {user.id}, {data.items.0.name} * * Resolution strategy: * 1. If first segment is a number, use positional access * 2. If first segment matches a property in any argument, start from there * 3. Otherwise, use the first segment as a "label" and try to navigate * the remaining path from the first object argument * * @param placeholder - The placeholder content without braces * @param args - Function arguments array * @param paramNames - Optional parameter names from reflection * @returns The resolved value or undefined if not found */ export declare function resolvePlaceholder(placeholder: string, args: any[], paramNames?: string[]): string | undefined; /** * Build a dynamic key from a template and function arguments * * @param template - Template string with placeholders * @param args - Function arguments array * @param paramNames - Optional parameter names from reflection * @param options - Resolution options * @param context - Context for error messages (className:methodName) * @returns The resolved key string * @throws Error if resolution fails and onMissingParam is 'error' */ export declare function buildDynamicKey(template: string, args: any[], paramNames: string[] | undefined, options: KeyTemplateOptions, context: string): string; /** * Extract parameter names from a function using reflection * This requires 'emitDecoratorMetadata' to be enabled in tsconfig * * @param target - Target object (class prototype) * @param propertyKey - Method name * @returns Array of parameter names or undefined if not available */ export declare function extractParameterNames(target: any, propertyKey: string | symbol): string[] | undefined; /** * Check if a string is a template (contains placeholders) * * @param str - String to check * @returns True if the string contains placeholder syntax */ export declare function isTemplate(str: string): boolean; /** * Validate a template string for syntax errors * * @param template - Template string to validate * @returns Object with valid flag and error message if invalid */ export declare function validateTemplate(template: string): { valid: boolean; error?: string; };