Name: egg-aop
Owner: egg
Description: AOP plugin for eggjs, add DI, AOP support.
Created: 2018-03-15 06:38:39.0
Updated: 2018-05-07 17:20:49.0
Pushed: 2018-04-19 11:50:50.0
Size: 67
Language: TypeScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Add DI, AOP support for eggjs.
rt { Service, Context } from 'egg';
rt { context, lazyInject } from 'egg-aop';
text() // or @application()
rt class TestService extends Service {
t() {
/* code */
rt class Controller {
azyInject()
ivate testService: TestService;
mo() {
this.testService.get();
@context(keyType?: any)
Declaration life cycle of instance, is context level. You can provide a class type or from metadata by TypeScript emit.
@application(keyType?: any)
Declaration life cycle of instance, is context level. You can provide a class type or from metadata by TypeScript emit.
@inject(keyType?: any)
Inject component when the class instantiation.
@lazyInject(keyType?: any)
Inject component when access the property.
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
is a instance of IocContext, this stored all type's classes. You can use this to affect DI behavior.
tion logging(type: string) {
turn aspect({
// before method running
before: (context) => { /* log code */ },
// after method running
after: (context) => { /* log code */ },
// when method throw error
onError: (context) => { /* log code */ },
s DemoService {
ogging('create')
eateData() {
/* code */
unctionContext type define */
rt interface FunctionContext<T = any> {
adonly inst: T;
adonly functionName: string;
gs: any[];
t: any;
r: Error;
aspect<T = any>(point: AspectPoint<T> = {})
You can use this to interception method, this function provide before
/ after
/ error
cross-section.
rface AspectPoint<T = any> {
re?: (context: FunctionContext<T>) => void;
r?: (context: FunctionContext<T>) => void;
r?: (context: FunctionContext<T>) => void;
The param context
is the function's execution context. It include inst
/ args
/ ret
. You can replace them to affect the function execute.