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 implements NestInterceptor | undefined> { intercept(_: ExecutionContext, next: CallHandler): Observable | 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 }; }), ); } }