Comparing version 0.3.4 to 0.3.5
@@ -0,5 +1,7 @@ | ||
export declare type Throwable = Error | any; | ||
export interface AspectPoint<T = any> { | ||
before?: (inst: T, args?: any[]) => void; | ||
after?: (inst: T, ret?: any) => void; | ||
before?: (inst: T, args?: any[]) => any[] | void; | ||
after?: (inst: T, ret?: any) => any | void; | ||
onError?: (inst: T, error: Error) => Throwable | void; | ||
} | ||
export declare function aspect<T = any>(point?: AspectPoint<T>): (target: any, key: string, descriptor: any) => void; |
@@ -11,6 +11,14 @@ "use strict"; | ||
newFn = function* (...args) { | ||
point.before && point.before(this, args); | ||
const result = yield fn.apply(this, args); | ||
point.after && point.after(this, result); | ||
return result; | ||
args = point.before && point.before(this, args) || args; | ||
try { | ||
const result = yield fn.apply(this, args); | ||
point.after && point.after(this, result); | ||
return result; | ||
} | ||
catch (error) { | ||
if (point.onError) { | ||
error = point.onError(this, error) || error; | ||
} | ||
throw error; | ||
} | ||
}; | ||
@@ -21,11 +29,27 @@ } | ||
newFn = function (...args) { | ||
point.before && point.before(this, args); | ||
const result = fn.apply(this, args); | ||
args = point.before && point.before(this, args) || args; | ||
let result = fn.apply(this, args); | ||
if (result instanceof Promise) { | ||
result.then((ret) => { | ||
result = result.then((ret) => { | ||
point.after && point.after(this, ret); | ||
return ret; | ||
}); | ||
if (point.onError) { | ||
result = result | ||
.catch(error => { | ||
error = point.onError(this, error) || error; | ||
throw error; | ||
}); | ||
} | ||
} | ||
else { | ||
point.after && point.after(this, result); | ||
try { | ||
point.after && point.after(this, result); | ||
} | ||
catch (error) { | ||
if (point.onError) { | ||
error = point.onError(this, error) || error; | ||
} | ||
throw error; | ||
} | ||
} | ||
@@ -32,0 +56,0 @@ return result; |
{ | ||
"name": "egg-aop", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"description": "aop for egg.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,17 +0,14 @@ | ||
# egg-typed-di | ||
# egg-aop | ||
## Quick overview | ||
### Service | ||
### DI | ||
```ts | ||
import { Service, serviceMetadata, Context } from 'egg-typed'; | ||
import { Service, Context } from 'egg'; | ||
import { context, lazyInject } from 'egg-aop'; | ||
@context() | ||
export class TestService extends Service { | ||
get(id: string | number) { | ||
return { | ||
id, | ||
name: this.app.config.test + '_' + id, | ||
}; | ||
get() { | ||
/* code */ | ||
} | ||
@@ -21,5 +18,36 @@ } | ||
export class Controller { | ||
@lazyInject() | ||
private testService: TestService; | ||
demo() { | ||
this.testService.get(); | ||
} | ||
} | ||
``` | ||
## API | ||
#### aspect | ||
```ts | ||
function logging(type: string) { | ||
return aspect({ | ||
// before method running | ||
before: (inst, args) => { /* log code */ }, | ||
// after method running | ||
after: (inst, ret) => { /* log code */ }, | ||
}) | ||
} | ||
class DemoService { | ||
@logging('create') | ||
createData() { | ||
/* code */ | ||
} | ||
} | ||
``` | ||
#### getInstance | ||
#### setCreateInstanceHook | ||
#### typeLoader |
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
14079
344
53