/** * ThreadTS Universal - Decorator Utilities * * Shared utilities for creating decorators that work with both * legacy (experimentalDecorators) and Stage-3 decorator syntax. * * @module decorators/utils * @author ThreadTS Universal Team */ export type AnyFunction = (...args: any[]) => any; /** * Type for Stage-3 decorator context. * Provides metadata about the decorated element. */ export interface DecoratorContext { /** The kind of element being decorated */ kind: 'method' | 'getter' | 'setter' | 'field' | 'class' | 'accessor'; /** The name of the decorated element */ name: string | symbol; /** Whether the element is static */ static: boolean; /** Whether the element is private */ private: boolean; /** Access object for getting/setting the value */ access?: { get?(): unknown; set?(value: unknown): void; }; /** Function to add an initializer */ addInitializer?(initializer: () => void): void; } /** * Return type for decorated methods. * Can be the wrapped function or a property descriptor. */ export type DecoratorReturnType = T | PropertyDescriptor | void; /** * Helper to create method decorators compatible with both legacy and Stage-3 syntax. * * This factory function abstracts away the differences between the two decorator * syntaxes, allowing decorator authors to focus on the decoration logic. * * @template T - The function type being decorated * @param decoratorLogic - Function that wraps the original method * @returns A decorator function that works with both syntaxes * * @example * ```typescript * // Creating a simple logging decorator * function logCalls() { * return createMethodDecorator((originalMethod, methodName) => { * return async function(...args: unknown[]) { * console.log(`Calling ${methodName}`); * const result = await originalMethod.apply(this, args); * console.log(`${methodName} returned:`, result); * return result; * } as typeof originalMethod; * }); * } * ``` */ export declare function createMethodDecorator(decoratorLogic: (originalMethod: T, methodName: string) => T): (targetOrMethod: unknown, propertyKeyOrContext?: string | symbol | DecoratorContext, descriptor?: PropertyDescriptor) => DecoratorReturnType; /** * Helper to create method decorators that also receive the class name. * Compatible with both legacy and Stage-3 syntax. * * This is useful for logging and observability decorators that need * to know which class the method belongs to. * * @template T - The function type being decorated * @param decoratorLogic - Function that wraps the original method, receiving class name * @returns A decorator function that works with both syntaxes * * @example * ```typescript * function logMethod() { * return createMethodDecoratorWithClass((method, methodName, className) => { * return async function(...args: unknown[]) { * console.log(`[${className}.${methodName}] called`); * return method.apply(this, args); * } as typeof method; * }); * } * ``` */ export declare function createMethodDecoratorWithClass(decoratorLogic: (originalMethod: T, methodName: string, className: string) => T): (targetOrMethod: unknown, propertyKeyOrContext?: string | symbol | DecoratorContext, descriptor?: PropertyDescriptor) => DecoratorReturnType; /** * Helper to create class decorators compatible with both legacy and Stage-3 syntax. * * @template T - The class constructor type * @param decoratorLogic - Function that wraps the original class * @returns A decorator function that works with both syntaxes */ export declare function createClassDecorator object>(decoratorLogic: (originalClass: T, className: string) => T): (target: T) => T; /** * Type guard to check if a value is an async function. * * @param fn - The value to check * @returns true if the value is an async function */ export declare function isAsyncFunction(fn: unknown): fn is (...args: unknown[]) => Promise; /** * Ensures a function returns a Promise. * Wraps synchronous functions to return Promise.resolve(result). * * @param fn - The function to wrap * @returns A function that always returns a Promise */ export declare function ensureAsync(fn: T): (...args: Parameters) => Promise>; //# sourceMappingURL=utils.d.ts.map