访问或将变量传递给Nestjs中的拦截器
我是Nestjs的新手,一直在研究一些解决方案,但没有找到方法。 我的API返回用户列表,如果存在或从DB中读取用户,则可以从缓存中读取用户,并且基于此,我需要设置标题。 我希望拦截器能够处理这一部分。
我的控制器
@UseInterceptors(CacheOrServiceHeaderInterceptor)
@Get('users')
public async users(
@Query('id') clinicianId: number,
@Query('name') specialtyName: string,
){
const setFlags: MetaFlags = {setCacheHeader: false, setServiceHeader: false};
const data = await this.service.getUsers(id, name, setFlags);
return data;
}
我的服务类
export interface MetaFlags {
setServiceHeader: boolean
setCacheHeader?: boolean,
}
:
:
public async getUsers(....){
// code that handles whether cache is used or a service is used to get users
and based on it, the metaFlags are set. setCacheHeader = true if read from cache or setServiceHeader = true if read from DB.
我的拦截器看起来像是这样的
@Injectable()
export class CacheOrServiceHeaderInterceptor implements NestInterceptor {
public intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next
.handle()
.pipe(
map((data) => {
const req = context.switchToHttp().getRequest();
// this is static but I want a condition here if
if(MetaFlags.setCacheHeader)
// set xyz header
else set abc header
req.res.set('x-api-key', 'pretty secure');
return data;
}
)
);}}
方法可以将这些标志传递给拦截器或使拦截器读取这些标志值?
I am new to NestJS and have been looking at a couple of solutions but haven't found a way.
My API returns list of users, the users are either read from the cache if present or from the DB and based on this I need to set the headers.
I want the interceptor to handle this part.
My Controller
@UseInterceptors(CacheOrServiceHeaderInterceptor)
@Get('users')
public async users(
@Query('id') clinicianId: number,
@Query('name') specialtyName: string,
){
const setFlags: MetaFlags = {setCacheHeader: false, setServiceHeader: false};
const data = await this.service.getUsers(id, name, setFlags);
return data;
}
My service class
export interface MetaFlags {
setServiceHeader: boolean
setCacheHeader?: boolean,
}
:
:
public async getUsers(....){
// code that handles whether cache is used or a service is used to get users
and based on it, the metaFlags are set. setCacheHeader = true if read from cache or setServiceHeader = true if read from DB.
My interceptor looks like this
@Injectable()
export class CacheOrServiceHeaderInterceptor implements NestInterceptor {
public intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next
.handle()
.pipe(
map((data) => {
const req = context.switchToHttp().getRequest();
// this is static but I want a condition here if
if(MetaFlags.setCacheHeader)
// set xyz header
else set abc header
req.res.set('x-api-key', 'pretty secure');
return data;
}
)
);}}
Is there a way to pass these flags to the interceptor or make the interceptor read these flag values ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议返回应将哪些标题设置为控制器返回服务的一部分。如果您最终使用全球状态,则可以在阅读拦截器并将错误的标头读回端点之前获得多个端点覆盖全局状态的请求。类似于
拦截器的
MAP
,您可以读取data
对象,然后删除data
属性属性,而不是返回它此标题
字段到客户端,和读取标题
字段,以便您知道要在响应上设置哪个标头。这样的事情:I would suggest returning what header should be set as a part of the service of controller's return. If you end up using a global state, you could get multiple requests to the endpoint overwriting the global state before you read it in the interceptor and send back the wrong header. Something like
Now in the
map
of your interceptor, you can read thedata
object and pull off thedata
property to return only it and not expose thisheader
field to the client, and read theheader
field so you know which header to set on the response. Something like this: