forked from wangziqi/gongxue-base
15 lines
658 B
TypeScript
15 lines
658 B
TypeScript
import type { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
|
|
import { Injectable } from '@nestjs/common';
|
|
import type { FastifyReply } from 'fastify';
|
|
import type { Observable } from 'rxjs';
|
|
import { map } from 'rxjs/operators';
|
|
import { withResponseMeta } from '../core/http.js';
|
|
|
|
@Injectable()
|
|
export class ApiEnvelopeInterceptor implements NestInterceptor {
|
|
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
|
|
const reply = context.switchToHttp().getResponse<FastifyReply>();
|
|
return next.handle().pipe(map(body => withResponseMeta(body, String(reply.getHeader('x-request-id') || ''))));
|
|
}
|
|
}
|