美文网首页
4-3、响应数据支持泛型

4-3、响应数据支持泛型

作者: Eileen_1d88 | 来源:发表于2019-12-09 13:32 被阅读0次
需求

通常情况下,我们会把后端返回数据格式单独放入一个接口中:

// 请求接口数据
export interface ResponseData<T = any> {
  /**
   * 状态码
   * @type { number }
   */
  code: number

  /**
   * 数据
   * @type { T }
   */
  result: T

  /**
   * 消息
   * @type { string }
   */
  message: string
}

我们可以把API抽成单独的模块:

import { ResponseData } from './interface.ts';

export function getUser<T>() {
  return axios.get<ResponseData<T>>('/somepath')
    .then(res => res.data)
    .catch(err => console.error(err))
}
接口添加泛型参数
interface AxiosResponse<T = any> {
  status: number,
  statusText: string,
  config: AxiosRequestConfig,
  headers: any,
  // 本来想说要定义成XMLHttpRequest,但是官方的axios是node端也适用的,
  // 那么在node端,就不是XMLHttpRequest类型了,所以在此纠正过来
  request: any,
  // request: XMLHttpRequest,
  data: T
}

interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {}
interface Axios {
  request<T = any>(config: AxiosRequestConfig): AxiosPromise<T>
  get<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>
  head<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>
  delete<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>
  options<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>
  put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>
  post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>
  patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>
}

interface AxiosInstance extends Axios {
  <T = any>(config: AxiosRequestConfig): AxiosPromise<T>
  <T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>
}
demo
import axios from '../../src/index'
interface ResponseData<T = any> {
  result: T,
  code: string,
  message: string
}
interface User {
  name: string,
  age: number
}

function getUser<T>() {
  return axios<ResponseData<T>>('/api/extend/get')
}
getUser<User>().then(res => {
  console.log('res', res)
})

添加这样一个对泛型的支持,其实在程序运行的时候,不会有什么左右,猜想可能是能够在使用的时候,允许应用方通过这个泛型的定义,来声明期望得到的数据类型吧

相关文章

  • 4-3、响应数据支持泛型

    需求 通常情况下,我们会把后端返回数据格式单独放入一个接口中: 我们可以把API抽成单独的模块: 接口添加泛型参数...

  • TypeScript(三)

    五. 泛型 泛型定义:泛型就是解决类、接口、方法的复用性,以及对不特定数据类型的支持。 可以支持不特定的数据类型,...

  • 5_泛型

    泛型概述 泛型就是一个标签:<数据类型> 泛型可以在编译阶段结束,只能操作某种数据类型 泛型和集合都智能支持引用数...

  • 泛型

    一.泛型的概念 二.支持泛型的类 不支持泛型的类 支持泛型的类 三.通配符 1.? extends 2.? sup...

  • P13 Dart 泛型 泛型方法 泛型类 泛型接口

    1.泛型 //泛型就是解决 类 接口 方法的复用性//以及对不特定数据类型的支持(类型校验) //2.泛型类 //...

  • 解决GSON转Long型变为科学计数法或整形变double的问题

    假如有这么个响应结果Response,当不指定泛型T的时候,泛型T中的数据成员容易出现Long型变为科学计数...

  • TypeScript 泛型初探

    我们想要组件不仅能支持现有数据类型,也要支持未来数据类型,这时泛型就提供了十分灵活的功能。通俗的讲,泛型就是解决类...

  • Typescript - 基础(四)

    泛型 1、什么是泛型 当需要一个函数或类支持多种数据类型时,具有很高的灵活性,此时泛型就出现了。 泛型的含义为:不...

  • Dart 泛型 泛型方法 泛型类 泛型接口

    通俗理解:泛型就是解决 类 接口 方法的复用性、以及对不特定数据类型的支持(类型校验) 1.定义一个泛型,泛型方法...

  • Kotlin 泛型

    Kotlin 支持泛型, 语法和 Java 类似。例如,泛型类: 泛型函数: 类型变异 Java 的泛型中,最难理...

网友评论

      本文标题:4-3、响应数据支持泛型

      本文链接:https://www.haomeiwen.com/subject/hitegctx.html