Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/toss/nestjs-aop
A way to gracefully apply AOP to nestjs
https://github.com/toss/nestjs-aop
aop nest nestjs nestjs-aop typescript
Last synced: 6 days ago
JSON representation
A way to gracefully apply AOP to nestjs
- Host: GitHub
- URL: https://github.com/toss/nestjs-aop
- Owner: toss
- License: mit
- Created: 2022-12-01T11:25:33.000Z (about 2 years ago)
- Default Branch: v2.x
- Last Pushed: 2025-01-02T08:43:03.000Z (about 1 month ago)
- Last Synced: 2025-01-25T08:02:57.420Z (13 days ago)
- Topics: aop, nest, nestjs, nestjs-aop, typescript
- Language: TypeScript
- Homepage:
- Size: 223 KB
- Stars: 225
- Watchers: 10
- Forks: 24
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
@toss/nestjs-aop ·
A way to gracefully apply AOP to nestjs
Use nestjs managed instances in any decorators gracefully
English | [한국어](https://github.com/toss/nestjs-aop/blob/v2.x/readme_kr.md)
Table of Contents
## Installation
```sh
npm install @toss/nestjs-aop
pnpm add @toss/nestjs-aop
yarn add @toss/nestjs-aop
```## Usage
#### 1. Import AopModule
```typescript
@Module({
imports: [
// ...
AopModule,
],
})
export class AppModule {}
```#### 2. Create symbol for LazyDecorator
```typescript
export const CACHE_DECORATOR = Symbol('CACHE_DECORATOR');
```#### 3. Implement LazyDecorator using nestjs provider
`metadata` is the second parameter of 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. Add LazyDecoratorImpl to providers of module
```typescript
@Module({
providers: [CacheDecorator],
})
export class CacheModule {}
```#### 5. Create decorator that marks metadata of LazyDecorator
`options` can be obtained from the warp method and used.```typescript
export const Cache = (options: CacheOptions) => createDecorator(CACHE_DECORATOR, options)
```#### 6. Use it!
```typescript
export class SomeService {
@Cache({
// ...options(metadata value)
})
some() {
// ...
}
}
```## References
- https://toss.tech/article/nestjs-custom-decorator
- https://youtu.be/VH1GTGIMHQw?t=2973## Contributing
We welcome contribution from everyone in this project. Read [CONTRIBUTING.md](CONTRIBUTING.md) for detailed contribution guide.## License
MIT © Viva Republica, Inc. See [LICENSE](LICENSE) for details.