用Nestjs Interceptor修改响应

发布于 2025-02-11 14:59:44 字数 1076 浏览 2 评论 0 原文

我有用于修改图标路径的拦截器:

@Injectable()
export class GetProgramIconInterceptor implements NestInterceptor {
    intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> | Promise<Observable<any>> {
        return next.handle().pipe(
            map(data => {
                return {
                    ...data,
                    icon: generalFUnctions.getApplicationIcon(data.icon)
                };
            }),
        );
    }
}

当我在查找方法中使用它时,它可以正常工作并修改图标路径。

@UseInterceptors(GetProgramIconInterceptor)
async find(id: string): Promise<Application> {
    const application = await this.repository.findById(id);
     if (!application) throw new ApplicationNotFoundException();
     return application;
}

当我将此拦截器用于Findall方法时,它无法正常工作,也不会修改路径。

@UseInterceptors(GetProgramIconInterceptor)
async findAll(): Promise<Application[]> {
     return this.repository.find();
}

我知道Findall方法的结果是一个数组,我可以为其创建另一个拦截器。有什么方法可以用一个拦截器处理

I have Interceptor for modify icon path like this:

@Injectable()
export class GetProgramIconInterceptor implements NestInterceptor {
    intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> | Promise<Observable<any>> {
        return next.handle().pipe(
            map(data => {
                return {
                    ...data,
                    icon: generalFUnctions.getApplicationIcon(data.icon)
                };
            }),
        );
    }
}

and when I use this in my find method, it works correctly and modifies the icon path.

@UseInterceptors(GetProgramIconInterceptor)
async find(id: string): Promise<Application> {
    const application = await this.repository.findById(id);
     if (!application) throw new ApplicationNotFoundException();
     return application;
}

and when I use this Interceptor to the findAll method it doesn't work correctly and doesn't modify the path.

@UseInterceptors(GetProgramIconInterceptor)
async findAll(): Promise<Application[]> {
     return this.repository.find();
}

I know the result of the findAll method is an array and I can create another Interceptor for it. Is there any way to handle it with one Interceptor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城月光淡如水﹏ 2025-02-18 14:59:45

由于您正在处理存储库模型功能( find and findbyid )的承诺(并且尚不可观察到的)结果,因此,将管道输送到可观察到的时,这意味着您正在观察存储库 repository []

附加A codesandbox 我创建了可能是单个对象或对象数组的资源的映射。

这里的魔术只是确保我们有条件表达式,何时资源为数组,

map((repositoryOrRespositories) => {
  if (Array.isArray(repositoryOrRespositories)) {
    return item.map((repository) => ({
      ...repository,
      icon: generalFUnctions.getApplicationIcon(repository.icon)
    }));
  } else {
    return { ...repositoryOrRespositories, icon: generalFUnctions.getApplicationIcon(repositoryOrRespositories.icon) };
  }
})

如果存储库模型的结果是可观察的,则必须使用rxjs中的 iif 表达式。

Since you are working with promises (And not already observable) results from the repository model functions (find and findById) when piping into the observable it means you are observing over repository or repository[]

Attaching a codesandbox of an example i have created mapping over resource that is possibly single object or array of objects.

The magic here is just making sure we have a conditional expression for when the resource is array or not

map((repositoryOrRespositories) => {
  if (Array.isArray(repositoryOrRespositories)) {
    return item.map((repository) => ({
      ...repository,
      icon: generalFUnctions.getApplicationIcon(repository.icon)
    }));
  } else {
    return { ...repositoryOrRespositories, icon: generalFUnctions.getApplicationIcon(repositoryOrRespositories.icon) };
  }
})

If the results from the repository model were observables you had to use iif expressions from rxjs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文