GithubHelp home page GithubHelp logo

akwodkiewicz / nestjs-aop Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nestjs-architects/aop

0.0 0.0 0.0 425 KB

A library that makes aspect-oriented programming easier

JavaScript 7.65% TypeScript 92.35%

nestjs-aop's Introduction

@nestjs-architects/aop

version downloads

If you don't know what AOP is check this out

Usage

$ 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);
  }
}

Profit

Now, every time the decoreted method is called the additional code provided by you is executed too.

showcase

Learn more

Aspect-oriented programming with NestJS

nestjs-aop's People

Contributors

akwodkiewicz avatar sikora00 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.