typescript自定义axios服务端返回数据接口

shims-axios.d.ts

import { AxiosInstance, AxiosRequestConfig, AxiosPromise } from "axios";
/**
 * 自定义扩展axios模块
 * @author Maybe
 */
declare module "axios" {
  export interface AxiosInstance {
    request>(
      config: AxiosRequestConfig
    ): Promise;
    get>(
      url: string,
      config?: AxiosRequestConfig
    ): Promise;
    delete>(
      url: string,
      config?: AxiosRequestConfig
    ): Promise;
    head>(
      url: string,
      config?: AxiosRequestConfig
    ): Promise;
    options>(
      url: string,
      config?: AxiosRequestConfig
    ): Promise;
    post>(
      url: string,
      data?: any,
      config?: AxiosRequestConfig
    ): Promise;
    put>(
      url: string,
      data?: any,
      config?: AxiosRequestConfig
    ): Promise;
    patch>(
      url: string,
      data?: any,
      config?: AxiosRequestConfig
    ): Promise;
  }
}

req-res.d.ts

namespace ReqRes {
  /**
   * 定义接口返回的固定格式
   * { code => 状态码, msg => '响应信息', data => 数据 }
   */
  export interface ResponseResult {
    code: number;
    msg: string;
    data: T;
  }
  /**
   * 列表数据接口
   */
  export interface ListData {
    list: [];
    page: number;
    size: number;
    total: number;
  }
}

test-api.ts

import http from "@/utils/http";

export function redirect(params?: any) {
  return http.request({
    url: "/redirect",
    method: "get",
    params,
  });
}

export function list(params?: any) {
  return http.request({
    url: "/list",
    method: "get",
    params,
  });
}

你可能感兴趣的:(axios,typescript)