Upon reviewing the code at this link:
https://github.com/nestjs/nest/tree/master/packages/common
One can see that the interface ArgumentsHost
has been defined, but the contents of its methods are not explicitly defined.
However, when looking at the following code snippet:
https://docs.nestjs.com/exception-filters
It is evident that ArgumentsHost
is used and the method switchToHttp()
functions correctly.
How is it that these methods work without having specific definitions? Is there a key point that I am missing as a beginner in this context? Any insights or opinions would be greatly appreciated.
Thank you.
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';
import { Request, Response } from 'express';
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const status = exception.getStatus();
response
.status(status)
.json({
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
});
}
}