npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nestjs-architects/aop

v0.2.0

Published

A library that makes aspect-oriented programming simpler

Downloads

6,661

Readme

@nestjs-architects/aop

version downloads

Profit

This package will help you make your services clean and focused on the feature. Many non-functional requirements can be easily added using Aspect-Oriented Programming.

Learn more

Aspect-oriented programming with NestJS

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,
    targetObject: Record<string, (arg: unknown) => Promise<unknown> | unknown>
  ): 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

Adding aspects to services created manually

There are cases when you would like to create a new service dynamically, not just inject it.

For such services you can use AspectsApplier.

import { AspectsApplier } from '@nestjs-architects/aop';
import { Injectable } from '@nestjs/common';

export class Speaker {
  @Logging()
  speak() {
    console.log('I am not a singleton');
  }
}

@Injectable()
export class AppService {
  constructor(private readonly aspectsApplier: AspectsApplier) {}
  speak() {
    const service = new Speaker();
    this.aspectsApplier.applyToProvider(service);
    service.speak();
  }
}