diff --git a/server/src/interceptors/http-response.interceptor.ts b/server/src/interceptors/http-response.interceptor.ts new file mode 100644 index 00000000..22484ea3 --- /dev/null +++ b/server/src/interceptors/http-response.interceptor.ts @@ -0,0 +1,27 @@ +import { + CallHandler, + ExecutionContext, + Injectable, + NestInterceptor, +} from '@nestjs/common'; +import { map, Observable } from 'rxjs'; +export interface Response { + data: T; +} + +@Injectable() +export class TransformHttpResponseInterceptor + implements NestInterceptor> +{ + intercept( + context: ExecutionContext, + next: CallHandler, + ): Observable> { + return next.handle().pipe( + map((data) => { + const status = context.switchToHttp().getResponse().statusCode; + return { data, success: true, status }; + }), + ); + } +} diff --git a/server/src/main.ts b/server/src/main.ts index 61492e15..09e2a342 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -5,6 +5,7 @@ import { NestFastifyApplication, } from '@nestjs/platform-fastify'; import { ValidationPipe } from '@nestjs/common'; +import { TransformHttpResponseInterceptor } from './interceptors/http-response.interceptor'; async function bootstrap() { const app = await NestFactory.create( @@ -22,6 +23,8 @@ async function bootstrap() { }), ); + app.useGlobalInterceptors(new TransformHttpResponseInterceptor()); + await app.listen(3000); } bootstrap();