egg-aop
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -7,2 +7,6 @@ export declare type Throwable = Error | any; | ||
} | ||
export declare function aspect<T = any>(point?: AspectPoint<T>): (target: any, key: string, descriptor: any) => void; | ||
export declare function aspect<T = any>(point?: AspectPoint<T>): (target: any, key: string, descriptor: any) => { | ||
configurable: boolean; | ||
get(): any; | ||
set(value: Function): void; | ||
}; |
@@ -59,3 +59,3 @@ "use strict"; | ||
let fn = funcWrapper(point, descriptor.value || target[key]); | ||
Object.defineProperty(target, key, { | ||
const value = { | ||
configurable: true, | ||
@@ -68,5 +68,7 @@ get() { | ||
} | ||
}); | ||
}; | ||
Object.defineProperty(target, key, value); | ||
return value; | ||
}; | ||
} | ||
exports.aspect = aspect; |
@@ -1,2 +0,1 @@ | ||
export { inject, lazyInject } from 'power-di/helper'; | ||
import { InstanceSource } from './getInstance'; | ||
@@ -28,1 +27,11 @@ /** | ||
export declare function application(keyType?: any): (target: any) => void; | ||
/** | ||
* inject | ||
* type: class or string | ||
*/ | ||
export declare function inject(type?: any): (target: any, key: string) => void; | ||
/** | ||
* lazy inject | ||
* type: class or string | ||
*/ | ||
export declare function lazyInject(type?: any): (target: any, key: string) => void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var helper_1 = require("power-di/helper"); | ||
exports.inject = helper_1.inject; | ||
exports.lazyInject = helper_1.lazyInject; | ||
const helper_1 = require("power-di/helper"); | ||
const getInstance_1 = require("./getInstance"); | ||
const typeLoader_1 = require("./typeLoader"); | ||
const appctx_1 = require("./appctx"); | ||
const decorators_1 = require("power-di/lib/helper/decorators"); | ||
/** | ||
@@ -44,1 +45,46 @@ * register component | ||
exports.application = application; | ||
/** | ||
* inject | ||
* type: class or string | ||
*/ | ||
function inject(type) { | ||
return (target, key) => { | ||
helper_1.inject({ type })(target, key); | ||
const clsType = decorators_1.getClsTypeByDecorator(type, target, key); | ||
let value; | ||
Object.defineProperty(target, key, { | ||
configurable: true, | ||
get() { | ||
if (value !== undefined) { | ||
return value; | ||
} | ||
const ctx = appctx_1.getCtx(this); | ||
const app = appctx_1.getApp(this) || (ctx && ctx.app); | ||
value = getInstance_1.getInstance(clsType, app, ctx); | ||
return target[key]; | ||
} | ||
}); | ||
}; | ||
} | ||
exports.inject = inject; | ||
/** | ||
* lazy inject | ||
* type: class or string | ||
*/ | ||
function lazyInject(type) { | ||
return (target, key) => { | ||
helper_1.lazyInject({ type })(target, key); | ||
const clsType = decorators_1.getClsTypeByDecorator(type, target, key); | ||
const defaultValue = target[key]; | ||
Object.defineProperty(target, key, { | ||
configurable: true, | ||
get: function () { | ||
const ctx = appctx_1.getCtx(this); | ||
const app = appctx_1.getApp(this) || (ctx && ctx.app); | ||
const value = getInstance_1.getInstance(clsType, app, ctx); | ||
return value !== undefined ? value : defaultValue; | ||
}, | ||
}); | ||
}; | ||
} | ||
exports.lazyInject = lazyInject; |
@@ -1,2 +0,1 @@ | ||
import { IocContext } from 'power-di'; | ||
export declare type InstanceSource = 'Context' | 'Application'; | ||
@@ -6,3 +5,2 @@ export declare type CreateInstanceHookFunction = (instance: any, app: any, ctx?: any) => any; | ||
export declare const contextTypeSymbol: unique symbol; | ||
export declare function injectInstance(ioc: IocContext, inst: any, app: any, ctx: any): void; | ||
export declare function getInstance<T = any>(clsType: any, app: any, ctx: any): T; |
@@ -13,8 +13,2 @@ "use strict"; | ||
exports.contextTypeSymbol = Symbol('contextType'); | ||
function injectInstance(ioc, inst, app, ctx) { | ||
ioc.inject(inst, (_globalType, typeCls) => { | ||
return getInstance(typeCls, app, ctx); | ||
}); | ||
} | ||
exports.injectInstance = injectInstance; | ||
function getInstance(clsType, app, ctx) { | ||
@@ -55,3 +49,2 @@ let ioc = undefined; | ||
app.iocContext.register(value, clsType); | ||
injectInstance(app.iocContext, value, app, ctx); | ||
} | ||
@@ -84,5 +77,4 @@ value = new Proxy(value, { | ||
ioc.register(value, clsType); | ||
injectInstance(ioc, value, app, ctx); | ||
return value; | ||
} | ||
exports.getInstance = getInstance; |
@@ -6,4 +6,4 @@ export * from './decorators'; | ||
export * from './aspect'; | ||
import 'chair'; | ||
declare module 'chair' { | ||
import 'egg'; | ||
declare module 'egg' { | ||
interface Context { | ||
@@ -10,0 +10,0 @@ getComponent<T = any>(clsType: any): T; |
@@ -12,2 +12,2 @@ "use strict"; | ||
tslib_1.__exportStar(require("./aspect"), exports); | ||
require("chair"); | ||
require("egg"); |
{ | ||
"name": "egg-aop", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "aop for egg.", | ||
@@ -9,5 +9,7 @@ "main": "lib/index.js", | ||
"app/**/*.js", | ||
"app/**/*.ts", | ||
"config/**/*.js", | ||
"config/**/*.ts", | ||
"lib/**/*.js", | ||
"**/*.d.ts" | ||
"lib/**/*.ts" | ||
], | ||
@@ -17,28 +19,37 @@ "eggPlugin": { | ||
}, | ||
"egg": { | ||
"typescript": true | ||
}, | ||
"dependencies": { | ||
"power-di": "^1.4.6", | ||
"power-di": "^1.4.8", | ||
"tslib": "^1.9.0" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^2.2.40", | ||
"@types/node": "^9.6.5", | ||
"@types/supertest": "^2.0.0", | ||
"autod": "^3.0.1", | ||
"egg-bin": "^4.3.7", | ||
"egg-bin": "^4.7.0", | ||
"egg-ci": "^1.8.0", | ||
"egg-mock": "^3.14.0", | ||
"reflect-metadata": "^0.1.12", | ||
"egg-mock": "^3.17.0", | ||
"egg-ts-helper": "^1.4.2", | ||
"egg": "^2.7.1", | ||
"rimraf": "^2.6.2", | ||
"supertest": "^3.0.0", | ||
"typescript": "^2.7.1" | ||
"tslib": "^1.9.0", | ||
"tslint": "^4.0.0", | ||
"typescript": "^2.8.1" | ||
}, | ||
"engines": { | ||
"node": ">=8.0.0" | ||
"node": ">=8.9.0" | ||
}, | ||
"scripts": { | ||
"test": "npm run test-local", | ||
"test-local": "egg-bin test", | ||
"cov": "egg-bin cov", | ||
"test": "npm run lint -- --fix && npm run test-local", | ||
"test-local": "egg-bin test -r egg-ts-helper/register", | ||
"cov": "egg-bin cov -r egg-ts-helper/register", | ||
"lint": "tslint .", | ||
"ci": "npm run cov", | ||
"autod": "autod", | ||
"ts": "rimraf app/**/*.js lib/**/*.js && tsc", | ||
"prepublish": "npm run ts", | ||
"postpublish": "node scripts/published.js" | ||
"prepublish": "npm run ts" | ||
}, | ||
@@ -52,6 +63,10 @@ "ci": { | ||
}, | ||
"eslintIgnore": [ | ||
"coverage" | ||
], | ||
"keywords": [ | ||
"egg", | ||
"egg-aop", | ||
"ts", | ||
"egg", | ||
"egg-framework" | ||
"aop" | ||
], | ||
@@ -58,0 +73,0 @@ "author": "zhang740", |
# egg-aop | ||
## Quick overview | ||
Add DI, AOP support for eggjs. | ||
### DI | ||
## DI | ||
### Quick overview | ||
```ts | ||
@@ -10,3 +12,3 @@ import { Service, Context } from 'egg'; | ||
@context() | ||
@context() // or @application() | ||
export class TestService extends Service { | ||
@@ -28,5 +30,19 @@ get() { | ||
## API | ||
### API | ||
#### aspect | ||
#### decoratros | ||
- `@context(keyType?: any)` | ||
- `@application(keyType?: any)` | ||
- `@inject(keyType?: any)` | ||
- `@lazyInject(keyType?: any)` | ||
#### functions | ||
- `getInstance<T = any>(clsType: any, app: any, ctx: any): T` | ||
- `setCreateInstanceHook(func: CreateInstanceHookFunction)` | ||
#### typeLoader | ||
## AOP | ||
### Quick overview | ||
```ts | ||
@@ -39,2 +55,4 @@ function logging(type: string) { | ||
after: (inst, ret) => { /* log code */ }, | ||
// when method throw error | ||
onError: (inst, err) => { /* log code */ }, | ||
}) | ||
@@ -51,6 +69,5 @@ } | ||
#### getInstance | ||
### API | ||
#### setCreateInstanceHook | ||
#### typeLoader | ||
#### functions | ||
- `aspect<T = any>(point: AspectPoint<T> = {})` |
27227
31
726
70
14
Updatedpower-di@^1.4.8