/
githubmirror
/
novu
Обзор
Документация
Войти
/
githubmirror
/
novu
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
next
apps/api/src/app/shared/framework/response.interceptor.ts
57 строк
2 KB
Dima Grossman
chore(root): upgrade NestJS packages to v11 fixes NV-7746 (#11218)
24 май 2026, 10:57
Не верифицирован
24 май 2026, 10:57
90c7b53
Код
Авторство
О чём код?
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() as string) === 'graphql') return next.handle(); return next.handle().pipe( map((data) => { if (this.returnWholeObject(data)) { return { ...data, data: isObject(data.data) ? this.transformResponse(data.data) : data.data, }; } return { data: isObject(data) ? this.transformResponse(data) : data, }; }) ); } /** * This method is used to determine if the entire object should be returned or just the data property * for paginated results that already contain the data wrapper, true. * for single entity result that *could* contain data object, false. * @param data * @private */ private returnWholeObject(data) { const isPaginatedResult = data?.data; const isEntityObject = data?._id || data?.id; return isPaginatedResult && !isEntityObject; } 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; } }