Giter Site home page Giter Site logo

nestjs-aop's Introduction


Logo

@toss/nestjs-aop · npm version

A way to gracefully apply AOP to nestjs
Use nestjs managed instances in any decorators gracefully


English | 한국어

Table of Contents
  1. Installation
  2. Usage
  3. References
  4. Contributing
  5. License

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.

Toss

nestjs-aop's People

Contributors

whitekiwi avatar eeeasycode avatar jongwooo avatar icecreamparlor avatar jurampark avatar smilecc avatar istiopaxx avatar jeean avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.