Skip to content

网络请求

Http-Typedi对发送请求做了一定的包装,并导出了RequestService类供客户端和服务端交互使用

TIP

当您使用的时候,只需要导入RequestService工具类并注入到应用程序即可。

官方写法

现在我们可以在 RequestController 中添加一个简单的官方写法的(RequestService)请求接口。我们使用 Http-Typedi 包提供的 RequestService来实现发送请求。

request.controller.ts

ts
import { RequestConfig, RequestService, PostMapping } from 'http-typedi'

interface TData {
  id: number
}

@Controller('/category')
export class UploadController {
  constructor(readonly requestService: RequestService)
  @PostMapping('update')
  async update<T = TData>(configure: RequestConfig<T>) {
    return await this.requestService.request<T, Record<string, any>>(
      <RequestConfig<T>>configure
    )
  }
}

以上代码客户端通过UploadController直接调用update会直接把RequestConfig<T>一并发送至服务端。至此,我们已经完成了与客户端交互的方法。

自定义请求

同官方推荐写法引入一致

custom.request.ts

ts
import { RequestConfig, RequestService, PostMapping } from 'http-typedi'

interface TData {
  id: number
}

const result = this.requestService.request<TData, Record<string, any>>(<RequestConfig<T>>configure)

以上代码则会直接发送网络请求,需要值得注意的是,由于是自定义请求,configure字段需要严格按照RequestConfig接口定义进行传参。以下是RequestConfig接口的类型

ts
interface RequestConfig<R> {
  url?: string
  method?: import('../../enums').RequestMethod
  headers?: Record<string, any>
  params?: R
  data?: R
  timeout?: number
  customActions?: boolean
  timeoutCallback?(response: ResponseConfig): void
}

如果methodGET,传入params即可,如果不是传入data即可