diff --git a/README.md b/README.md index 31b21f3..f683c0a 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ +English | [한국어](https://github.com/toss/nestjs-aop/blob/v2.x/readme_kr.md) + Table of Contents @@ -31,7 +33,6 @@ - ## Installation diff --git a/readme_kr.md b/readme_kr.md new file mode 100644 index 0000000..84caa16 --- /dev/null +++ b/readme_kr.md @@ -0,0 +1,137 @@ + + + + + + + + @toss/nestjs-aop · + + + NestJS에 우아하게 AOP를 적용하는 방법 + + NestJS 관리 인스턴스를 모든 데코레이터에서 우아하게 사용하세요 + + + + + + + + + 목차 + + 설치 방법 + 사용 예시 + 참고자료 + 기여하기 + 라이센스 + + + + + + + +## 설치 방법 + +```sh +npm install @toss/nestjs-aop +pnpm add @toss/nestjs-aop +yarn add @toss/nestjs-aop +``` + + + +## 사용 예시 + +#### 1. AopModule Import 하기 +```typescript +@Module({ + imports: [ + // ... + AopModule, + ], +}) +export class AppModule {} +``` + +#### 2. LazyDecorator를 위한 심볼 생성 +```typescript +export const CACHE_DECORATOR = Symbol('CACHE_DECORATOR'); +``` + +#### 3. NestJS 프로바이더로 LazyDecorator 구현하기 +`metadata`는 createDecorator의 두 번째 매개변수입니다. + +```typescript +@Aspect(CACHE_DECORATOR) +export class CacheDecorator implements LazyDecorator { + constructor(private readonly cache: Cache) {} + + wrap({ method, metadata: options }: WrapParams) { + return (...args: any) => { + let cachedValue = this.cache.get(...args); + if (!cachedValue) { + cachedValue = method(...args); + this.cache.set(cachedValue, ...args); + } + return cachedValue; + }; + } +} +``` + +#### 4. 모듈의 프로바이더에 LazyDecoratorImpl 추가하기 +```typescript +@Module({ + providers: [CacheDecorator], +}) +export class CacheModule {} +``` + +#### 5. LazyDecorator의 metadata를 나타내는 데코레이터 생성 +`options`는 wrap 메소드에서 얻을 수 있으며 사용될 수 있습니다. + +```typescript +export const Cache = (options: CacheOptions) => createDecorator(CACHE_DECORATOR, options) +``` + +#### 6. 사용하기! +```typescript +export class SomeService { + @Cache({ + // ...options(metadata value) + }) + some() { + // ... + } +} +``` + + + +## 참고자료 +- https://toss.tech/article/nestjs-custom-decorator +- https://youtu.be/VH1GTGIMHQw?t=2973 + + + + +## 기여하기 +이 프로젝트에는 모든 분들의 기여를 환영합니다. 자세한 기여 가이드는 [CONTRIBUTING.md](CONTRIBUTING.md)를 참고하세요. + + + + +## 라이센스 +MIT © Viva Republica, Inc. [LICENSE](LICENSE) 파일을 참고하세요. + + + + + + + + +
+ NestJS에 우아하게 AOP를 적용하는 방법 + + NestJS 관리 인스턴스를 모든 데코레이터에서 우아하게 사용하세요 + +