If you don't know what AOP is check this out
$ npm i @nestjs-architects/aop
Create your own advice (additional piece of code executed along with the original method)
import { AdviceProvider } from '@nestjs-architects/aop';
interface LoggingOptions {
format: 'JSON' | 'TEXT';
}
class LoggingAdvice implements AdviceProvider {
async attach(
originalMethod: Function,
args: unknown[],
options: LoggingOptions
): Promise<unknown> {
console.log('Before...');
const result = await originalMethod(...args);
console.log('After...');
return result;
}
}
Define a decorator and attach it to your methods
import { SetMetadata } from '@nestjs/common';
const LOGGING_KEY = 'LOGGING';
export const Logging = (options: LoggingOptions) =>
SetMetadata(LOGGING_KEY, options);
@Injectable()
export class AppService {
@Logging()
getHello(): string {
console.log('Initial method called');
return 'Hello World!';
}
}
Register both as your own aspect
import { AopModule, AspectsRegistry } from '@nestjs-architects/aop';
import { Module } from '@nestjs/common';
@Module({
imports: [AopModule],
providers: [LoggingAdvice],
})
export class LoggingModule {
constructor(
private readonly registry: AspectsRegistry,
private readonly loggingAdvice: LoggingAdvice
) {
this.registry.addAspect(LOGGING_KEY, this.loggingAdvice);
}
}
Now, every time the decoreted method is called the additional code provided by you is executed too.