All files xapi-response-interceptor.ts

100% Statements 24/24
100% Branches 6/6
100% Functions 2/2
100% Lines 24/24

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 291x       1x 1x 4x 1x 3x 3x 3x 3x 2x   2x 1x 1x 1x 1x 1x 2x 1x 1x 1x 3x 3x 3x 1x  
import { CallHandler, ExecutionContext, Injectable, Logger, NestInterceptor } from '@nestjs/common';
import { XapiRoot, writeString } from '@xapi-js/core';
import { Observable, map } from 'rxjs';
 
@Injectable()
export class XapiResponseInterceptor implements NestInterceptor<XapiRoot, Promise<string>> {
  private readonly logger: Logger = new Logger(XapiResponseInterceptor.name);
  intercept(context: ExecutionContext, next: CallHandler): Observable<Promise<string>> {
    this.logger.debug(`XapiResponseInterceptor Intercepting response`);
    return next.handle().pipe(
      map(async (value) => {
        if (value instanceof XapiRoot) {
          try {
            // Serialize XapiRoot output to XML string
            const xmlOutput = await writeString(value as XapiRoot);
            return xmlOutput!!;
          } catch (error) {
            this.logger.error(`Failed to serialize XapiRoot to XML string: ${error.message}`);
            throw error; // Re-throw the original error
          }
        }
        else {
          throw new Error('Handler did not return an XapiRoot instance');
        }
      })
    )
  }
}