@aspectjs/core

中文文档
0.5.4发布于 2025/04/12MITTypeScriptESM + CJS零依赖近 30 天下载 2013

Aspect Oriented Programming based on decorators, for browser & node

原始使用文档
文档目录5 节
  1. An AOP framework for Typescript, compatible with Browsers &…
  2. 📜 Abstract
  3. 🎉 Demo:
  4. 🚀 Getting started:
  5. 🔗 Documentation

AspectJS

AspectJS logo

An AOP framework for Typescript, compatible with Browsers & Node.

ci-status coverage report npm version license NPM Downloads bundlejs Latest Release



📜 Abstract

Inspired by the AspectJ java framework, AspectJS leverages ES Decorators to bring Aspect Oriented Programming to Javascript and Typescript.

example

🎉 Demo:

Demo on stackedit.

ECMAScript Decorators are fantastic: they allow developers to hide boilerplate code behind a simple @ sign, keeping the code clean, easy to read, and easy to write. Widely used in popular projects such as Angular, Nest.js or TypeORM, decorators have one major drawback: they come with their own built-in behavior, making interoperability between tools difficult, and preventing them from being repurposed for other uses.

AspectJS takes a different approach, by introducing two important concepts: Annotations and Aspects.

  • An Annotation is essentially an empty decorator that marks a target (class, property, method or parameter) as a candidate for further enhancements.
  • Aspects can be selectively enabled to introduce new behaviors into the annotated elements.
  • without aspects
    const A = function( target: Function) {
       // behaviorA
    }
    const B = function( target: Function) {
       // behaviorB
    }
     
    @A() 
    @B()
    class MyClass {}
    
  • with aspects
    const af = new AnnotationFactory('my.org')
    const A = af.create('A');
    const B = af.create('B');
    
    @A()
    @B()
    class MyClass {}
    
    @Aspect()
    class AB_Aspect {
      // behavior A
      // behavior B
    }
    
    getWeaver().enable(new AB_Aspect())
    

🚀 Getting started:

  • Install the packages

    npm i @aspectjs/core @aspectjs/common
    
  • Create an annotation:

    // toasted.annotation.ts
    import { AnnotationFactory, AnnotationKind } from '@aspectjs/common';
    
    const ANNOTATION_FACTORY = new AnnotationFactory('demo');
    const Toasted = ANNOTATION_FACTORY.create(
      AnnotationKind.METHOD,
      'Toasted', 
      function Toasted() {},
    );
    
  • Use that annotation on a class, a property, a method or a parameter:

    // main.ts
    class Main {
      @Toasted()
      run() {
        console.log('run');
      }
    }
    
  • Declare an aspect triggered by the annotation:

    // toasted.aspect.ts
    import { Around, AroundContext, Aspect, JoinPoint, on } from '@aspectjs/core';
    @Aspect()
    class ToastedAspect {
      @Around(on.methods.withAnnotations(Toasted))
      toast(ctxt: AroundContext, jp: JoinPoint, jpArgs: unknown[]) {
        const result = jp(...jpArgs);
        const text = `${ctxt.target.label} completed successfully`;
        showToast(text);
        return result;
      }
    }
    
  • Enable the aspect

    // aop.ts
    import { getWeaver } from '@aspectjs/core';
    
    getWeaver().enable(new ToastedAspect());
    

🔗 Documentation

For more advanced usage, please read the documentation: https://aspectjs.gitlab.io/.

MIT Licensed

版本与发布记录

@aspectjs/core 共发布 19 个版本,首次发布于 2020/11/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.5.4latest2025/04/121.6 年前
0.5.32024/09/242 年前
0.5.12024/09/232 年前
0.5.02024/09/232 年前
0.4.32024/09/172 年前
0.4.22024/09/172 年前

关于 @aspectjs/core 的常见问题

@aspectjs/core 是什么?

Aspect Oriented Programming based on decorators, for browser & node

如何安装 @aspectjs/core?

在项目目录运行 npm install @aspectjs/core,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@aspectjs/common(^0.5.4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aspectjs/core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aspectjs/core 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@aspectjs/core 可以商用吗?

@aspectjs/core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aspectjs/core 的下载量有多少?

根据 npm 官方下载统计,@aspectjs/core 在 2026/10/07 – 2026/11/05 这 30 天内被下载 2,013 次,日均约 67 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aspectjs/core 还在维护吗?

最新版本 0.5.4 发布于 2025/04/12(1.6 年前);至今共发布 19 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/11/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。