English | 한국어
Table of Contents
npm install @toss/nestjs-aop
pnpm add @toss/nestjs-aop
yarn add @toss/nestjs-aop
@Module({
imports: [
// ...
AopModule,
],
})
export class AppModule {}
export const CACHE_DECORATOR = Symbol('CACHE_DECORATOR');
metadata
is passed as the second argument to createDecorator
and is made available in the WrapParams
for the wrap
method.
@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;
};
}
}
@Module({
providers: [CacheDecorator],
})
export class CacheModule {}
options
can be obtained from the wrap
method and used.
export const Cache = (options: CacheOptions) => createDecorator(CACHE_DECORATOR, options)
export class SomeService {
@Cache({
// ...options(metadata value)
})
some() {
// ...
}
}
If you’re testing with NestJS’s TestingModule, don’t forget to call the init method.
import { Test } from '@nestjs/testing';
const module = await Test.createTestingModule({
// ...
}).compile();
await module.init();
We welcome contributions from everyone to this project. Read CONTRIBUTING.md for detailed contribution guide.
MIT © Viva Republica, Inc. See LICENSE for details.