@eklabdev/blingts4
Version:
A comprehensive TypeScript decorators library implementing Stage 2 decorators for TS 4.9+
103 lines (102 loc) • 3.72 kB
JavaScript
/**
* Executes a function before the decorated method
* @param fn Function to execute before the method
*/
export function EffectBefore(fn) {
return function (target, propertyKey, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args) {
const effectContext = {
functionName: propertyKey,
className: this.constructor?.name || 'Unknown',
args,
};
const beforeResult = fn(effectContext);
const methodResult = originalMethod.apply(this, args);
if (beforeResult instanceof Promise) {
return beforeResult.then(() => methodResult);
}
return methodResult;
};
return descriptor;
};
}
/**
* Executes a function after the decorated method
* @param fn Function to execute after the method
*/
export function EffectAfter(fn) {
return function (target, propertyKey, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args) {
const methodResult = originalMethod.apply(this, args);
if (methodResult instanceof Promise) {
return methodResult.then(result => {
const effectContext = {
functionName: propertyKey,
className: this.constructor?.name || 'Unknown',
args,
result,
};
return Promise.resolve(fn(effectContext)).then(() => result);
});
}
const effectContext = {
functionName: propertyKey,
className: this.constructor?.name || 'Unknown',
args,
result: methodResult,
};
const afterResult = fn(effectContext);
if (afterResult instanceof Promise) {
return afterResult.then(() => methodResult);
}
return methodResult;
};
return descriptor;
};
}
/**
* Executes a function when the decorated method throws an error
* @param fn Function to execute on error
*/
export function EffectError(fn) {
return function (target, propertyKey, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args) {
try {
const methodResult = originalMethod.apply(this, args);
if (methodResult instanceof Promise) {
return methodResult.catch(error => {
const effectContext = {
functionName: propertyKey,
className: this.constructor?.name || 'Unknown',
args,
error,
};
return Promise.resolve(fn(effectContext)).then(() => {
throw error;
});
});
}
return methodResult;
}
catch (error) {
const effectContext = {
functionName: propertyKey,
className: this.constructor?.name || 'Unknown',
args,
error: error,
};
const errorResult = fn(effectContext);
if (errorResult instanceof Promise) {
return errorResult.then(() => {
throw error;
});
}
throw error;
}
};
return descriptor;
};
}