All Projects → eggjs → egg-aop

eggjs / egg-aop

Licence: MIT license
AOP plugin for eggjs, add DI, AOP support.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to egg-aop

egg-exporter
Egg.js 的 Prometheus 指标收集插件,附带 Grafana 看板。
Stars: ✭ 24 (-45.45%)
Mutual labels:  egg, egg-plugin
egg-kafka-node
kafka plugin for egg.js
Stars: ✭ 36 (-18.18%)
Mutual labels:  egg, egg-plugin
Cool Admin Api
cool-admin-api 是基于egg.js、typeorm、jwt等封装的api开发脚手架、快速开发api接口
Stars: ✭ 188 (+327.27%)
Mutual labels:  egg, egg-plugin
egg-rbac
Role Based Access Control for eggjs
Stars: ✭ 32 (-27.27%)
Mutual labels:  egg, egg-plugin
Ray.di
Guice style dependency injection framework for PHP
Stars: ✭ 175 (+297.73%)
Mutual labels:  aop, di
Egg Multipart
multipart plugin for egg
Stars: ✭ 145 (+229.55%)
Mutual labels:  egg, egg-plugin
Egg Socket.io
socket.io plugin for eggjs.
Stars: ✭ 209 (+375%)
Mutual labels:  egg, egg-plugin
Egg Schedule
Schedule plugin for egg
Stars: ✭ 76 (+72.73%)
Mutual labels:  egg, egg-plugin
CNeptune
CNeptune improve productivity & efficiency by urbanize .net module with meta-code to lay foundation for frameworks
Stars: ✭ 30 (-31.82%)
Mutual labels:  aop, di
Egg Redis
redis plugin for egg
Stars: ✭ 234 (+431.82%)
Mutual labels:  egg, egg-plugin
Egg View Vue
vue view plugin for egg
Stars: ✭ 136 (+209.09%)
Mutual labels:  egg, egg-plugin
egg-grpc
grpc plugin for egg
Stars: ✭ 79 (+79.55%)
Mutual labels:  egg, egg-plugin
Egg Router Plus
The missing router feature for eggjs
Stars: ✭ 117 (+165.91%)
Mutual labels:  egg, egg-plugin
Egg Oauth2 Server
🌟 OAuth2 server plugin for egg.js based on node-oauth2-server
Stars: ✭ 174 (+295.45%)
Mutual labels:  egg, egg-plugin
Egg Passport
passport plugin for egg
Stars: ✭ 98 (+122.73%)
Mutual labels:  egg, egg-plugin
Egg Security
Security plugin for egg, force performance too.
Stars: ✭ 204 (+363.64%)
Mutual labels:  egg, egg-plugin
Egg View Ejs
egg view plugin for ejs.
Stars: ✭ 54 (+22.73%)
Mutual labels:  egg, egg-plugin
Egg View React Ssr
Egg React Server Side Render (SSR) Plugin
Stars: ✭ 55 (+25%)
Mutual labels:  egg, egg-plugin
Egg Validate
validate plugin for egg
Stars: ✭ 224 (+409.09%)
Mutual labels:  egg, egg-plugin
Bear.sunday
A resource-oriented application framework
Stars: ✭ 230 (+422.73%)
Mutual labels:  aop, di

egg-aop

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Add DI, AOP support for eggjs.

DI

Quick overview

import { Service, Context } from 'egg';
import { context, lazyInject } from 'egg-aop';

@context() // or @application()
export class TestService extends Service {
  get() {
    /* code */
  }
}

export class Controller {
  @lazyInject()
  private testService: TestService;
  
  demo() {
    this.testService.get();
  }
}

API

decorators

  • @context(keyType?: any)

    Declaration of life cycle of an instance, is context level. You can provide a class type or from metadata by TypeScript emit.

  • @application(keyType?: any)

    Declaration of life cycle of an instance, is context level. You can provide a class type or from metadata by TypeScript emit.

  • @inject(keyType?: any)

    Inject component when the class is instantiated.

  • @lazyInject(keyType?: any)

    Inject component when accessing the property.

functions

  • getInstance<T = any>(clsType: any, app: any, ctx: any): T

    You can use this function to manually get the component instance.

  • setCreateInstanceHook(func: CreateInstanceHookFunction)

    You can use this function to interception every new component instance.

    type CreateInstanceHookFunction = (instance: any, app: any, ctx?: any) => any;

typeLoader

typeLoader is an instance of IocContext, it stores all type's classes. You can use this to affect DI behavior.

AOP

Quick overview

function logging(type: string) {
  return aspect({
    // before method running
    before: (context) => { /* log code */ },
    // after method running
    after: (context) => { /* log code */ },
    // when method throw error
    onError: (context) => { /* log code */ },
  })
}

class DemoService {
  @logging('create')
  createData() {
    /* code */
  }
}

/* FunctionContext type define */
export interface FunctionContext<T = any> {
  readonly inst: T;
  readonly functionName: string;
  args: any[];
  ret: any;
  err: Error;
}

API

functions

  • aspect<T = any>(point: AspectPoint<T> = {})

    You can use this to intercept method, this function provides before / after / error cross-sections.

    interface AspectPoint<T = any> {
      before?: (context: FunctionContext<T>) => void;
      after?: (context: FunctionContext<T>) => void;
      error?: (context: FunctionContext<T>) => void;
    }

    The param context is the function's execution context. It includes inst / args / ret. You can replace them to affect the function execution.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].