hook-decorator
v1.0.4
Published
A hook decorator for es6 classes
Downloads
10
Readme
hook-decorator
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.
Installation
npm i hook-decorator --save
Usage
Example
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
// !
Configuration
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 lifecycles
Angular invokes lifecycle methods via internal data structures associated with the component. Because of this, overriding the lifecycle methods inside the class prototype does not change run time behaviour.
Include the options: { patchAngular: true }
in your config to patch these internal methods.
License
MIT