创建入口文件
import { AxiosRequestConfig } from './types/index'import xhr from './xhr'const axios = (params: AxiosRequestConfig) => { xhr(params)}export default axios
定义 AxiosRequestConfig 接口类型
export type Method = | 'get' | 'GET' | 'delete' | 'DELETE' | 'head' | 'HEAD' | 'options' | 'OPTIONS' | 'post' | 'POST' | 'put' | 'PUT' | 'patch' | 'PATCH';export interface AxiosRequestConfig { url: string method: Method data?: any params?: any}
定义 xhr 方法
import { AxiosRequestConfig } from "./types";export default function xhr(config: AxiosRequestConfig) { const { url, method = 'get', data = null, params = null } = config const request = new XMLHttpRequest() request.open(method.toLowerCase(), url, true) request.send(data)}