【问题标题】:Where the method looks undefined in nestjs interface该方法在nestjs接口中看起来未定义的位置
【发布时间】:2020-10-21 14:18:38
【问题描述】:

当我看到以下代码时,

https://github.com/nestjs/nest/tree/master/packages/common

例如ArgumentsHost是由接口定义的。

但是方法的内容好像没有定义。

 * @publicApi
 */
export interface ArgumentsHost {
  /**
   * Returns the array of arguments being passed to the handler.
   */
  getArgs<T extends Array<any> = any[]>(): T;
  /**
   * Returns a particular argument by index.
   * @param index index of argument to retrieve
   */
  getArgByIndex<T = any>(index: number): T;
  /**
   * Switch context to RPC.
   * @returns interface with methods to retrieve RPC arguments
   */
  switchToRpc(): RpcArgumentsHost;
  /**
   * Switch context to HTTP.
   * @returns interface with methods to retrieve HTTP arguments
   */
  switchToHttp(): HttpArgumentsHost;
  /**
   * Switch context to WebSockets.
   * @returns interface with methods to retrieve WebSockets arguments
   */
  switchToWs(): WsArgumentsHost;
  /**
   * Returns the current execution context type (string)
   */
  getType<TContext extends string = ContextType>(): TContext;
}

但是当我看到以下说明时,

https://docs.nestjs.com/exception-filters

ArgumentsHost 被使用,switchToHttp() 运行良好。

为什么这些方法在没有特定定义的情况下有效?

我错过了大问题吗?由于我是初学者,如果有人有意见,请告诉我。

谢谢

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,
      });
  }
}

【问题讨论】:

    标签: typescript nestjs


    【解决方案1】:

    这实际上并不是 Nest 特有的,而是一般的 TypeScript(和其他语言)。 该接口定义了一个类型,您的catch 方法指定它的第二个参数必须是具有该类型的某个值。 这意味着任何具有正确“形状”的值都可以用作第二个参数。

    事实证明,Nest 库本身负责为您定义的 catch 方法提供值,并且当它这样做时,它必须提供 ArgumentsHost 接口的具体“实例”。 作为此类实例的示例,请参见ExecutionContextHost 的定义。 (事实证明,ExecutionContextHost 实际上实现了一个不同 接口,ExecutionContext,但是ExecutionContext 扩展 ArgumentsHost,所以它是一个有效的实例)。

    这是编写函数的一种非常通用且强大的技术,只要求其输入具有某种“形状”,即某些属性或方法。

    例如,想象编写一个带有render 函数的图形库,该函数将表示形状的对象数组作为输入并将它们绘制到屏幕上。 这些形状应该有什么类型? 我们可能希望允许多种形状(如正方形、圆形,甚至更奇特的东西),而这些形状可能非常不同。 但是,只要每个形状都知道如何绘制自己,那么我们就可以像这样编写我们的render 函数:

    function render(shapes: Drawable[]) {
      shapes.forEach(shape => shape.draw());
    }
    
    interface Drawable {
      draw: () => void;
    }
    

    因此,任何具有合适的draw 方法的对象都可以作为参数数组的一部分传递给render,不管它包含什么。

    回到你的问题,draw 方法不是由Drawable 接口定义的;相反,Drawable 接口是一种约束其他类型的方式:它就像一种筛子,只允许某些东西通过。

    draw 方法由接口的具体实例定义。 例如:

    class Square extends Drawable {
      public x: number;
      public y: number;
      public sideLength: number;
    
      draw() {
        // Drawing logic here
      }
    }
    

    【讨论】:

    • 谢谢,我明白了!所以有问题的代码,host.switchTohttp()。它指的是ExecutionContextHost?但是我怎样才能找到ExecutionContextHost?我无法通过阅读 ArgumentsHost 代码找到 ExecutionContextHost。
    • 太棒了!一般来说,我不知道找到特定类型实例的任何可靠方法,但如果您可以访问源代码,这里有一个非常有效的策略。我要尝试的第一件事是搜索"implements ArgumentsHost"(如果您在 GitHub 存储库中搜索,引号很重要)。这将返回任何直接实现接口的类。正如我们所料,没有结果。接下来,我们可以通过搜索"extends ArgumentsHost" 找到任何扩展 ArgumentsHost 的接口。在这里我们发现ExecutionContext 确实如此。
    • 然后我们重复,这次搜索"implements ExecutionContext"。我们终于找到了一个实例!这个搜索告诉我们ExecutionContextHost 实现了ExecutionContext 接口。所以(至少据我所知),它涉及到一些“翻找”。
    猜你喜欢
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 2021-07-10
    相关资源
    最近更新 更多