prevision / apps /prevision-api /src /common /interceptors /response.interceptor.ts
github-actions[bot]
Automated Deploy: 23110475c742a5d4f03930b55bca09042215c1dd
1fe7075
Raw History Blame Contribute Delete
865 Bytes
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { map, type Observable } from 'rxjs';
import { Paginated } from '../dto/paginated.dto.js';
import type { ApiSuccess } from '../interfaces/api-response.interface.js';
/** Every successful response becomes `{ success: true, data, meta? }`; empty (204) responses stay empty. */
@Injectable()
export class ResponseInterceptor<T> implements NestInterceptor<T, ApiSuccess<unknown> | undefined> {
intercept(_: ExecutionContext, next: CallHandler<T>): Observable<ApiSuccess<unknown> | undefined> {
return next.handle().pipe(
map((body) => {
if (body === undefined) return undefined;
if (body instanceof Paginated) return { success: true, data: body.items, meta: body.meta };
return { success: true, data: body };
}),
);
}
}