/
githubmirror
/
novu
Обзор
Документация
Войти
/
githubmirror
/
novu
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
next
apps/webhook/src/shared/framework/response.interceptor.ts
36 строк
1 KB
Adam Chmara
chore(root): apply biome formatter & linter fixes NV-6436 (#8838)
31 июл 2025, 14:59
Не верифицирован
31 июл 2025, 14:59
855fb8f
Код
Авторство
О чём код?
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common'; import { instanceToPlain } from 'class-transformer'; import { isArray, isObject } from 'lodash'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; export interface Response<T> { data: T; } @Injectable() export class ResponseInterceptor<T> implements NestInterceptor<T, Response<T>> { intercept(context, next: CallHandler): Observable<Response<T>> { if (context.getType() === 'graphql') return next.handle(); return next.handle().pipe( map((data) => { return { data: isObject(data) ? this.transformResponse(data) : data, }; }) ); } private transformResponse(response) { if (isArray(response)) { return response.map((item) => this.transformToPlain(item)); } return this.transformToPlain(response); } private transformToPlain(plainOrClass) { return plainOrClass && plainOrClass.constructor !== Object ? instanceToPlain(plainOrClass) : plainOrClass; } }