Comparing version 0.5.0 to 0.5.1
0.5.0 / 2018-04-19 | ||
0.5.1 / 2018-06-26 | ||
================== | ||
* refactor: aspect function params. | ||
BREAKING CHANGE: | ||
- aspect function param sign. | ||
0.4.2 / 2018-04-19 | ||
================== | ||
* fix: inject obejct define by metadata. | ||
0.4.1 / 2018-04-19 | ||
================== | ||
* docs: update doc outline. | ||
* test: add tests. | ||
* feat: add remove/clear instance hook method. | ||
* refactor: optimization type definition of getInstance. |
@@ -1,7 +0,9 @@ | ||
import { IocContext } from 'power-di'; | ||
import { IocContext, GetReturnType } from 'power-di'; | ||
export declare type InstanceSource = 'Context' | 'Application'; | ||
export declare type CreateInstanceHookFunction = (instance: any, app: any, ctx?: any) => any; | ||
export declare function setCreateInstanceHook(func: CreateInstanceHookFunction): void; | ||
export declare function removeCreateInstanceHook(func: CreateInstanceHookFunction): void; | ||
export declare function clearCreateInstanceHook(): void; | ||
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; | ||
export declare function getInstance<T = undefined, KeyType = any>(clsType: KeyType, app: any, ctx: any): GetReturnType<T, KeyType>; |
@@ -12,2 +12,13 @@ "use strict"; | ||
exports.setCreateInstanceHook = setCreateInstanceHook; | ||
function removeCreateInstanceHook(func) { | ||
const index = ciHooks.indexOf(func); | ||
if (index >= 0) { | ||
ciHooks.splice(index, 1); | ||
} | ||
} | ||
exports.removeCreateInstanceHook = removeCreateInstanceHook; | ||
function clearCreateInstanceHook() { | ||
ciHooks.splice(0); | ||
} | ||
exports.clearCreateInstanceHook = clearCreateInstanceHook; | ||
exports.contextTypeSymbol = Symbol('contextType'); | ||
@@ -25,3 +36,3 @@ function injectInstance(ioc, inst, app, ctx) { | ||
if (autoRegisterToCtx && !typeLoader_1.typeLoader.has(clsType)) { | ||
typeLoader_1.register(clsType, clsType, 'Context'); | ||
typeLoader_1.register(clsType, clsType, clsType[exports.contextTypeSymbol] || 'Context'); | ||
} | ||
@@ -28,0 +39,0 @@ const targetClsType = typeLoader_1.typeLoader.get(clsType); |
import { IocContext } from 'power-di'; | ||
import { IocContext, GetReturnType } from 'power-di'; | ||
import { getGlobalType } from 'power-di/utils'; | ||
@@ -16,2 +16,11 @@ import { setApp, setCtx, ctxSymbol } from './appctx'; | ||
} | ||
export function removeCreateInstanceHook(func: CreateInstanceHookFunction) { | ||
const index = ciHooks.indexOf(func); | ||
if (index >= 0) { | ||
ciHooks.splice(index, 1); | ||
} | ||
} | ||
export function clearCreateInstanceHook() { | ||
ciHooks.splice(0); | ||
} | ||
@@ -26,3 +35,3 @@ export const contextTypeSymbol = Symbol('contextType'); | ||
export function getInstance<T = any>(clsType: any, app: any, ctx: any): T { | ||
export function getInstance<T = undefined, KeyType = any>(clsType: KeyType, app: any, ctx: any): GetReturnType<T, KeyType> { | ||
let ioc: IocContext = undefined; | ||
@@ -33,4 +42,4 @@ | ||
if (autoRegisterToCtx && !typeLoader.has(clsType)) { | ||
register(clsType, clsType, 'Context'); | ||
if (autoRegisterToCtx && !typeLoader.has(clsType as any)) { | ||
register(clsType, clsType, (clsType as any)[contextTypeSymbol] || 'Context'); | ||
} | ||
@@ -52,3 +61,3 @@ | ||
let value = ioc.get<T>(clsType); | ||
let value: any = ioc.get(clsType); | ||
if (value) { return value; } | ||
@@ -98,5 +107,5 @@ | ||
ioc.register(value, clsType); | ||
ioc.register(value, clsType as any); | ||
injectInstance(ioc, value, app, ctx); | ||
return value; | ||
} |
@@ -7,9 +7,10 @@ export * from './decorators'; | ||
import 'egg'; | ||
import { GetReturnType } from 'power-di'; | ||
declare module 'egg' { | ||
interface Context { | ||
getComponent<T = any>(clsType: any): T; | ||
getComponent<T = undefined, KeyType = any>(clsType: KeyType): GetReturnType<T, KeyType>; | ||
} | ||
interface Application { | ||
getComponent<T = any>(clsType: any): T; | ||
getComponent<T = undefined, KeyType = any>(clsType: KeyType): GetReturnType<T, KeyType>; | ||
} | ||
} |
@@ -8,9 +8,10 @@ export * from './decorators'; | ||
import 'egg'; | ||
import { GetReturnType } from 'power-di'; | ||
declare module 'egg' { | ||
export interface Context { | ||
getComponent<T = any>(clsType: any): T; | ||
getComponent<T = undefined, KeyType= any>(clsType: KeyType): GetReturnType<T, KeyType>; | ||
} | ||
export interface Application { | ||
getComponent<T = any>(clsType: any): T; | ||
getComponent<T = undefined, KeyType= any>(clsType: KeyType): GetReturnType<T, KeyType>; | ||
} | ||
} |
{ | ||
"name": "egg-aop", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "aop for egg.", | ||
@@ -22,14 +22,15 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"power-di": "^1.4.8", | ||
"power-di": "^1.4.11", | ||
"tslib": "^1.9.0" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^2.2.40", | ||
"@types/mocha": "^5.1.0", | ||
"@types/node": "^9.6.5", | ||
"@types/supertest": "^2.0.0", | ||
"@types/supertest": "^2.0.4", | ||
"autod": "^3.0.1", | ||
"co": "^4.6.0", | ||
"egg-bin": "^4.7.0", | ||
"egg-ci": "^1.8.0", | ||
"egg-mock": "^3.17.0", | ||
"egg-ts-helper": "^1.4.2", | ||
"egg-ts-helper": "^1.6.0", | ||
"egg": "^2.7.1", | ||
@@ -39,4 +40,4 @@ "rimraf": "^2.6.2", | ||
"tslib": "^1.9.0", | ||
"tslint": "^4.0.0", | ||
"typescript": "^2.8.1" | ||
"tslint": "^5.9.1", | ||
"typescript": "~2.8.1" | ||
}, | ||
@@ -43,0 +44,0 @@ "engines": { |
# egg-aop | ||
[![NPM version][npm-image]][npm-url] | ||
[![build status][travis-image]][travis-url] | ||
[![Test coverage][codecov-image]][codecov-url] | ||
[![David deps][david-image]][david-url] | ||
[![Known Vulnerabilities][snyk-image]][snyk-url] | ||
[![npm download][download-image]][download-url] | ||
[npm-image]: https://img.shields.io/npm/v/egg-aop.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/egg-aop | ||
[travis-image]: https://img.shields.io/travis/eggjs/egg-aop.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/eggjs/egg-aop | ||
[codecov-image]: https://codecov.io/github/eggjs/egg-aop/coverage.svg?branch=master | ||
[codecov-url]: https://codecov.io/github/eggjs/egg-aop?branch=master | ||
[david-image]: https://img.shields.io/david/eggjs/egg-aop.svg?style=flat-square | ||
[david-url]: https://david-dm.org/eggjs/egg-aop | ||
[snyk-image]: https://snyk.io/test/npm/egg-aop/badge.svg?style=flat-square | ||
[snyk-url]: https://snyk.io/test/npm/egg-aop | ||
[download-image]: https://img.shields.io/npm/dm/egg-aop.svg?style=flat-square | ||
[download-url]: https://npmjs.org/package/egg-aop | ||
Add DI, AOP support for eggjs. | ||
@@ -33,12 +53,32 @@ | ||
- `@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. | ||
#### 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 a instance of IocContext, this stored all type's classes. You can use this to affect DI behavior. | ||
## AOP | ||
@@ -80,1 +120,13 @@ | ||
- `aspect<T = any>(point: AspectPoint<T> = {})` | ||
You can use this to interception method, this function provide `before` / `after` / `error` cross-section. | ||
```ts | ||
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 include `inst` / `args` / `ret`. You can replace them to affect the function execute. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33085
827
131
15
Updatedpower-di@^1.4.11