import type { ArgumentsHost } from '@nestjs/common'; import type { ITransport } from '../types'; /** * This class is used to detect current protocol (transport type). * Mostly used in global enhancers to decide: "to work of not to work" */ export class ProtocolDetector { constructor(private transports: ITransport[]) {} /** * Detects transport's protocol by ArgumentHost (context) */ getProtocol(context: ArgumentsHost): string | undefined { return this.getTransport(context)?.getProtocol(); } /** * Checks that transport's protocol matches the provided one. * (just convenience method) */ matchProtocol(context: ArgumentsHost, protocol: string): boolean { return this.getProtocol(context) === protocol; } private getTransport(context: ArgumentsHost): ITransport | undefined { return this.transports.findLast((x) => x.matchByContext(context)); } }