mono/packages/core/dist/aspects.d.ts
2025-01-28 13:42:22 +01:00

88 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export declare enum SIGNALS {
BEFORE = "BEFORE",
AFTER = "AFTER",
AROUND = "AROUND",
ERROR = "ERROR"
}
type AnyFunction = (...args: any[]) => any;
/**
* If a function returns a Promise<T>, then its awaited type is T.
* Otherwise, it's just ReturnType<T>.
*/
type AwaitedReturn<T extends AnyFunction> = T extends (...args: any[]) => Promise<infer U> ? U : ReturnType<T>;
/**
* BEFORE advice:
* - Receives `context` (the `this` of the function)
* - Receives the original `args`
* - Can return either nothing (`void`) or new arguments (`Parameters<T>`)
* - Can be async, returning a Promise that resolves to new args or `void`
*/
export type BeforeAdvice<T extends AnyFunction> = (context: ThisParameterType<T>, args: Parameters<T>) => void | Parameters<T> | Promise<void | Parameters<T>>;
/**
* AFTER advice:
* - Receives `context`, the original functions final (awaited) result,
* and the original arguments
* - Can return a new result (sync or async)
*/
export type AfterAdvice<T extends AnyFunction> = (context: ThisParameterType<T>, result: AwaitedReturn<T>, args: Parameters<T>) => AwaitedReturn<T> | Promise<AwaitedReturn<T>>;
/**
* AROUND advice:
* - Provides a `proceed(...args)` function that calls the original method
* - You can call `proceed` any number of times, or skip it
* - Supports both sync and async usage
*/
export type AroundAdvice<T extends AnyFunction> = (proceed: (...args: Parameters<T>) => ReturnType<T>, context: ThisParameterType<T>, args: Parameters<T>) => ReturnType<T>;
/**
* ERROR advice:
* - Intercepts errors thrown by the original method (sync or async)
* - Can return a fallback result or rethrow
*/
export type ErrorAdvice<T extends AnyFunction> = (error: unknown, context: ThisParameterType<T>, args: Parameters<T>) => ReturnType<T> | void;
interface AspectOptions<T extends SIGNALS, A> {
type: T;
advice: A;
}
/**
* The core aspect(...) function
* - Returns a decorator if used in that style
* - Otherwise, can wrap a function directly
*/
declare function aspect<T extends SIGNALS, A>({ type, advice }: AspectOptions<T, A>): (target: any, _name?: string, descriptor?: PropertyDescriptor) => any;
/**
* `before`:
* Decorator usage => @before((ctx, args) => ...)
* Direct usage => myFn = before(myFn, (ctx, args) => ...)
*/
export declare function before<T extends AnyFunction>(advice: BeforeAdvice<T>): (target: any, name?: string, descriptor?: PropertyDescriptor) => any;
export declare function before<T extends AnyFunction>(fn: T, advice: BeforeAdvice<T>): T;
/**
* `after`:
* Decorator usage => @after((ctx, result, args) => ...)
* Direct usage => myFn = after(myFn, (ctx, result, args) => ...)
*/
export declare function after<T extends AnyFunction>(advice: AfterAdvice<T>): (target: any, name?: string, descriptor?: PropertyDescriptor) => any;
export declare function after<T extends AnyFunction>(fn: T, advice: AfterAdvice<T>): T;
/**
* `around`:
* Decorator usage => @around((proceed, ctx, args) => ...)
* Direct usage => myFn = around(myFn, (proceed, ctx, args) => ...)
*/
export declare function around<T extends AnyFunction>(advice: AroundAdvice<T>): (target: any, name?: string, descriptor?: PropertyDescriptor) => any;
export declare function around<T extends AnyFunction>(fn: T, advice: AroundAdvice<T>): T;
/**
* `error`:
* Decorator usage => @error((err, ctx, args) => ...)
* Direct usage => myFn = error(myFn, (err, ctx, args) => ...)
*/
export declare function error<T extends AnyFunction>(advice: ErrorAdvice<T>): (target: any, name?: string, descriptor?: PropertyDescriptor) => any;
export declare function error<T extends AnyFunction>(fn: T, advice: ErrorAdvice<T>): T;
declare const _default: {
SIGNALS: typeof SIGNALS;
before: typeof before;
after: typeof after;
around: typeof around;
error: typeof error;
aspect: typeof aspect;
};
export default _default;