hook-decorator
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

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 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

Dependencies (0)

    Dev Dependencies (9)

    Package Sidebar

    Install

    npm i hook-decorator

    Weekly Downloads

    1

    Version

    1.0.4

    License

    MIT

    Unpacked Size

    7.11 kB

    Total Files

    4

    Last publish

    Collaborators

    • aleksanderbodurri