Global Interceptor
Create Interceptor
For example, we create a Global Interceptor logger
in the module demo-student
1. Cli command
$ vona :create:bean interceptor logger --module=demo-student --boilerplate=global
2. Menu command
TIP
Context Menu - [Module Path]: Vona Aspect/Interceptor Global
Interceptor Definition
export interface IInterceptorOptionsLogger extends IDecoratorInterceptorOptionsGlobal {}
@Interceptor<IInterceptorOptionsLogger>({ global: true })
export class InterceptorLogger extends BeanBase implements IInterceptorExecute {
async execute(_options: IInterceptorOptionsLogger, next: Next) {
const timeBegin = Date.now();
const res = await next();
const timeEnd = Date.now();
console.log('time: ', timeEnd - timeBegin);
return res;
}
}
IInterceptorOptionsLogger
: Defines interceptor parametersexecute
: Outputs execution time
Using Interceptor
Unlike local interceptor, the system automatically loads global interceptors and makes them effective
Interceptor Parameters
You can define parameters for interceptor, allowing for more flexible configuration of interceptor logic
For example, define the prefix
parameter for the logger interceptor to control the output format
1. Defining parameter types
export interface IInterceptorOptionsLogger extends IDecoratorInterceptorOptionsGlobal {
+ prefix: string;
}
2. Providing default values for parameters
@Interceptor<IInterceptorOptionsLogger>({
global: true,
+ prefix: 'time',
})
3. Using Parameters
export interface IInterceptorOptionsLogger extends IDecoratorInterceptorOptionsGlobal {
prefix: string;
}
@Interceptor<IInterceptorOptionsLogger>({
global: true,
prefix: 'time',
})
class InterceptorLogger {
async execute(options: IInterceptorOptionsLogger, next: Next) {
const timeBegin = Date.now();
const res = await next();
const timeEnd = Date.now();
- console.log('time: ', timeEnd - timeBegin);
+ console.log(`${options.prefix}: `, timeEnd - timeBegin);
return res;
}
}
4. Specify parameters when using
You can specify global interceptor parameters for a specific API
class ControllerStudent {
@Web.get()
+ @Aspect.interceptorGlobal('demo-student:logger', { prefix: 'elapsed' })
async findMany() {}
}
- When using interceptor, just provide the parameter value directly
5. App config
Interceptor parameters can be configured in App config
src/backend/config/config/config.ts
// onions
config.onions = {
interceptor: {
'demo-student:logger': {
prefix: 'elapsed',
},
},
};
6. Parameters precedence
Specify parameters when using
> App config
> Default values
Interceptor Order
Since global interceptors ard loaded and enabled by default, VonaJS provides two parameters to control the order in which interceptor is loaded
1. dependencies
For example, the system has a built-in global interceptor a-body:bodyRes
, and we hope that the loading order is as follows: a-body:bodyRes
> Current
@Interceptor({
global: true,
+ dependencies: 'a-body:bodyRes',
prefix: 'time',
})
class InterceptorLogger {}
2. dependents
The order of dependents
is just the opposite of dependencies
. We hope that the loading order is as follows: Current
> a-body:bodyRes
@Interceptor({
global: true,
+ dependents: 'a-body:bodyRes',
prefix: 'time',
})
class InterceptorLogger {}
Interceptor enable/disable
You can control enable/disable
of global interceptor for certain APIs
1. Enable
- Disable for an API
class ControllerStudent {
@Web.get()
+ @Aspect.interceptorGlobal('demo-student:logger', { enable: false })
async findMany() {}
}
- Disable for all APIs
src/backend/config/config/config.ts
// onions
config.onions = {
interceptor: {
'demo-student:logger': {
+ enable: false,
},
},
};
2. Meta
Allows global interceptor to take effect in a specified operating environment
Name | Type | Description |
---|---|---|
flavor | string|string[] | See: Runtime Environments and Flavors |
mode | string|string[] | See: Runtime Environments and Flavors |
instanceName | string|string[] | See: Multi-Instance/Multi-Tenancy |
host | string|string[] | Host |
- Example
@Interceptor({
global: true,
+ meta: {
+ flavor: 'normal',
+ mode: 'dev',
+ instanceName: '',
+ host: 'localhost:7102',
+ },
})
class InterceptorLogger {}
3. match/ignore
You can enable/disable global interceptor for some specific APIs
Name | Type | Description |
---|---|---|
match | string|regexp|(string|regexp)[] | For which APIs to enable |
ignore | string|regexp|(string|regexp)[] | For which APIs to disable |
Inspect
You can directly inspect the currently effective global interceptor list in the Controller action
class ControllerStudent {
@Web.get()
async findMany() {
+ this.bean.onion.interceptor.inspect();
}
}
this.bean.onion
: Get the global Service instanceonion
.interceptor
: Get the Service instance related to the interceptor.inspect
: Output the currently effective global interceptor list
When accessing the findMany
API, the current list of global interceptor in effect will be automatically output to the console, as shown below: