AleksanderBodurri / hook-decorator

Class decorator to hook before/after functions into method calls.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

npm version

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 lifecycle methods are called differently from other methods. Hook decorator offers experimental support for these methods via the patchAngular option in HookConfigOptions.

License

MIT

About

Class decorator to hook before/after functions into method calls.

License:MIT License


Languages

Language:TypeScript 100.0%