All files / src/platform/gateway/interceptors api-deprecation.interceptor.ts

0% Statements 0/18
0% Branches 0/10
0% Functions 0/2
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42                                                                                   
import {
  Injectable,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
  Logger,
} from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { Observable } from 'rxjs';
import { DEPRECATED_API_KEY } from '../decorators/api-governance.decorators';
 
@Injectable()
export class ApiDeprecationInterceptor implements NestInterceptor {
  private readonly logger = new Logger(ApiDeprecationInterceptor.name);
 
  constructor(private readonly reflector: Reflector) {}
 
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const deprecationInfo = this.reflector.get<{
      message?: string;
      replacedBy?: string;
    }>(DEPRECATED_API_KEY, context.getHandler());
 
    if (deprecationInfo) {
      const response = context.switchToHttp().getResponse();
      response.setHeader('Deprecation', 'true');
      if (deprecationInfo.message) {
        response.setHeader('Sunset', deprecationInfo.message);
      }
      if (deprecationInfo.replacedBy) {
        response.setHeader(
          'Link',
          `<${deprecationInfo.replacedBy}>; rel="successor-version"`,
        );
      }
      this.logger.warn(`Deprecated API hit: ${context.getHandler().name}`);
    }
 
    return next.handle();
  }
}