• Stars
    star
    219
  • Rank 180,696 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 2 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A way to gracefully apply AOP to nestjs

Logo

@toss/nestjs-aop ยท npm version

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


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

More Repositories

1

es-toolkit

A modern JavaScript utility library that's 2-3 times faster and up to 97% smallerโ€”a major upgrade to lodash.
TypeScript
6,401
star
2

slash

A collection of TypeScript/JavaScript packages to build high-quality web services.
JavaScript
2,709
star
3

es-hangul

A modern JavaScript library for handling Hangul characters.
TypeScript
1,283
star
4

suspensive

Manage asynchronous operations, timing, error handling, detecting intersection of elements, and caching easily and declaratively
TypeScript
505
star
5

tossface

ํ† ์Šค์˜ ์ด๋ชจ์ง€ ํฐํŠธ, ํ† ์ŠคํŽ˜์ด์Šค
308
star
6

overlay-kit

The simplest and most intuitive way to manage overlays in React.
TypeScript
272
star
7

yarn-plugin-workspace-since

๋ชจ๋…ธ๋ ˆํฌ๋ฅผ ์œ„ํ•œ yarn berry plugin
TypeScript
184
star
8

use-funnel

A powerful and safe step-by-step state management library
TypeScript
164
star
9

toss-ui

TypeScript
99
star
10

yarn-standard-workspace

EJS
44
star
11

tuid

Kotlin
24
star
12

packlint

Lint Package.json in large JavaScript Monorepos
TypeScript
19
star
13

toss-login-android-sdk

ํ† ์Šค ๋กœ๊ทธ์ธ Android SDK ๋ฅผ ์œ„ํ•œ ๋ชจ๋…ธ๋ ˆํฌ์ž…๋‹ˆ๋‹ค.
Kotlin
9
star
14

http-auth-parser

HTTP Authorization header parser
Java
7
star
15

cache

Cache library for Kotlin/JVM
Kotlin
7
star
16

toss-sdk-ios

Swift
6
star
17

toss-cert-java-sdk

Java
5
star
18

toss-cert-examples

C#
2
star
19

toss-cert-cps

ํ† ์Šค์ธ์ฆ ์ธ์ฆ์—…๋ฌด์ค€์น™
2
star
20

f5day-hackathon-flutter

Dart
2
star