Inspired by rails action controller filters.
This decorator patches before/after hooks into every method (referred to in API as actions) defined by a class.
npm i hook-decorator --save
import { Hook } from 'hook-decorator';
@Hook({
beforeAction: {
callback: (classInstance: Logger) => console.log(classInstance.pre),
},
afterAction: {
callback: (classInstance: Logger) => console.log(classInstance.post),
},
})
class Logger {
pre = 'hello';
post = '!';
log(): void {
console.log('world');
}
}
const logger = new Logger();
logger.log();
// hello
// world
// !
export interface HookConfig {
beforeAction?: Hook;
afterAction?: Hook;
options?: HookConfigOptions;
}
interface Hook {
callback: Callback;
only?: string[];
except?: string[];
}
type Callback = (classInstance?: any) => any;
interface HookConfigOptions {
patchAngular: boolean;
}
Angular lifecycle methods are called differently from other methods. Hook decorator offers experimental support for these methods via the patchAngular
option in HookConfigOptions
.
MIT