使用提供程序在拦截器中传递自定义参数
Pass custom parameters in interceptor with provider
我用提供者创建了一个拦截器并将其绑定到 "rpc"
export class RPCProvider implements Provider<Interceptor> {
constructor(
@inject(CertifierBindings.CERTIFIER)
public certifierModule: CertifierModule
) { }
value() {
return this.intercept.bind(this);
}
async intercept<T>(
invocationCtx: InvocationContext,
next: () => ValueOrPromise<T>,
) {
// i want to pass some parameters in here
// ...
return await next();
}
}
application.ts
this.bind('rpc').toProvider(RPCProvider);
我可以这样使用它:
@intercept('rpc')
@authenticate('basic', {
scope: []
})
@post('/test/v1/anything')
async test(): Promise<any> {
return await this.dbMain.col("Maintainer").aggregateAndToArray([]);
}
但是每次用的时候怎么传参呢?像这样:
@intercept(rpc({
a:1, // <=
b:2
}))
@authenticate('basic', {
scope: []
})
@post('/test/v1/anything')
async test(): Promise<any> {
return await this.dbMain.col("Maintainer").aggregateAndToArray([]);
}
export const MyInterceptor = function (
...args: any[] // <= pass custom parameters
): Interceptor {
return async function (invocationCtx, next) {
// get binding with `invocationCtx`
const bindingObject = await invocationCtx.get<any>('binding-key');
}
}
我用提供者创建了一个拦截器并将其绑定到 "rpc"
export class RPCProvider implements Provider<Interceptor> {
constructor(
@inject(CertifierBindings.CERTIFIER)
public certifierModule: CertifierModule
) { }
value() {
return this.intercept.bind(this);
}
async intercept<T>(
invocationCtx: InvocationContext,
next: () => ValueOrPromise<T>,
) {
// i want to pass some parameters in here
// ...
return await next();
}
}
application.ts
this.bind('rpc').toProvider(RPCProvider);
我可以这样使用它:
@intercept('rpc')
@authenticate('basic', {
scope: []
})
@post('/test/v1/anything')
async test(): Promise<any> {
return await this.dbMain.col("Maintainer").aggregateAndToArray([]);
}
但是每次用的时候怎么传参呢?像这样:
@intercept(rpc({
a:1, // <=
b:2
}))
@authenticate('basic', {
scope: []
})
@post('/test/v1/anything')
async test(): Promise<any> {
return await this.dbMain.col("Maintainer").aggregateAndToArray([]);
}
export const MyInterceptor = function (
...args: any[] // <= pass custom parameters
): Interceptor {
return async function (invocationCtx, next) {
// get binding with `invocationCtx`
const bindingObject = await invocationCtx.get<any>('binding-key');
}
}