2.1.4 • Published 2 months ago

@toss/nestjs-aop v2.1.4

Weekly downloads
-
License
MIT
Repository
-
Last release
2 months ago

Installation

npm install @toss/nestjs-aop
pnpm add @toss/nestjs-aop
yarn add @toss/nestjs-aop

Usage

1. Import AopModule

@Module({
  imports: [
    // ...
    AopModule,
  ],
})
export class AppModule {}

2. Create symbol for LazyDecorator

export const CACHE_DECORATOR = Symbol('CACHE_DECORATOR');

3. Implement LazyDecorator using nestjs provider

metadata is the second parameter of createDecorator.

@Aspect(CACHE_DECORATOR)
export class CacheDecorator implements LazyDecorator<any, CacheOptions> {
  constructor(private readonly cache: Cache) {}

  wrap({ method, metadata: options }: WrapParams<any, CacheOptions>) {
    return (...args: any) => {
      let cachedValue = this.cache.get(...args);
      if (!cachedValue) { 
        cachedValue = method(...args);
        this.cache.set(cachedValue, ...args);
      }
      return cachedValue;
    };
  }
}

4. Add LazyDecoratorImpl to providers of module

@Module({
  providers: [CacheDecorator],
})
export class CacheModule {}

5. Create decorator that marks metadata of LazyDecorator

options can be obtained from the warp method and used.

export const Cache = (options: CacheOptions) => createDecorator(CACHE_DECORATOR, options)

6. Use it!

export class SomeService {
  @Cache({
    // ...options(metadata value)
  })
  some() {
    // ...
  }
}

References

Contributing

We welcome contribution from everyone in this project. Read CONTRIBUTING.md for detailed contribution guide.

License

MIT © Viva Republica, Inc. See LICENSE for details.

2.1.4

2 months ago

2.1.3

5 months ago

2.1.2

6 months ago

2.1.1

6 months ago

2.1.0

10 months ago

2.0.3

11 months ago

2.0.2

1 year ago

2.0.0-next.6

1 year ago

2.0.0-next.7

1 year ago

2.0.0-next.4

1 year ago

2.0.0-next.5

1 year ago

2.0.0-next.2

1 year ago

2.0.0-next.3

1 year ago

2.0.0-next.1

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago