Angular 注入 DI 超级药剂的正确类型是什么?

What is the proper type for Angular DI injected superagent?

我正在尝试在用户 Angular DI (ngx) 的 node.js 应用程序中注入 Superagent。

import * as request from 'superagent';
 ... 
 {
    provide: request,
    useFactory: () =>
      request.agent()
        .timeout({deadline: 10000, timeout: 60000}),
  },

但是我property超时does not exist on SuperAgent<SuperAgentRequest>。我尝试关注 these docs,但 SuperAgent 似乎不是正确的类型。 request.agent() 的类型应该是 request.Request

我错过了什么?我如何正确提供 request.Request 以便我可以全局配置它以使用相同的(注入的)实例?

在 Superagent 类型中可以看到,request.agent() returns SuperAgent<SuperAgentRequest> that doesn't have timeout method,这就是错误消息所说的。

虽然 timeout 方法存在于 Request type that is a promise of a response. This is the reason why this error was thrown. There's no request and no response. Superagent documentation 中,但为 timeout 提供了一个示例:

request
  .get('/big-file?network=slow')
  .timeout({
    response: 5000,  // Wait 5 seconds for the server to start sending,
    deadline: 60000, // but allow 1 minute for the file to finish loading.
  })

文档指出代理实例有 methods that set defaults,因此缺少类型。没有 deadline 方法,用 timeout 也没有意义,因为这是 deadline timeout .

superagent 打字应该在本地增加,或者改进并 PR 到 DefinitelyTyped 存储库,或者就地修复:

 {
    provide: request,
    useFactory: () =>
      <SuperAgent<SuperAgentRequest>>request.agent()
        ['timeout'](30)
  }

我希望增强类型类似于(使用正则表达式处理的原始 Request 接口):

custom.d.ts

import * as request from 'superagent';

type CallbackHandler = (err: any, res: request.Response) => void;
type Serializer = (obj: any) => string;
type BrowserParser = (str: string) => any;
type NodeParser = (res: request.Response, callback: (err: Error | null, body: any) => void) => void;
type Parser = BrowserParser | NodeParser;

declare module "superagent" {
    interface ConfigurableSuperAgent<Req extends request.SuperAgentRequest> extends request.SuperAgent<Req> {
        accept(type: string): this;
        auth(user: string, name: string): this;
        buffer(val?: boolean): this;
        ca(cert: Buffer): this;
        cert(cert: Buffer | string): this;
        key(cert: Buffer | string): this;
        ok(callback: (res: Response) => boolean): this;
        on(name: 'error', handler: (err: any) => void): this;
        on(name: 'progress', handler: (event: ProgressEvent) => void): this;
        on(name: string, handler: (event: any) => void): this;
        parse(parser: Parser): this;
        pfx(cert: Buffer | string): this;
        query(val: object | string): this;
        redirects(n: number): this;
        retry(count?: number, callback?: CallbackHandler): this;
        serialize(serializer: Serializer): this;
        set(field: object): this;
        set(field: string, val: string): this;
        timeout(ms: number | { deadline?: number, response?: number }): this;
        type(val: string): this;
        use(fn: Plugin): this;
    }

    interface SuperAgentStatic extends request.SuperAgent<request.SuperAgentRequest> {
        agent(): ConfigurableSuperAgent<request.SuperAgentRequest>;
    }
}