Author:Gorit Date:2022年3月16日 Refer: Refactor axios using TypeScript
use TypeScript implement axios,so called ts-axios
- typescript libary starter
axios.request(config)
axios.get(url[,config])
axios.delete(url[, config])
axios.head(url[, config])
axios.options(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])
// overide
axios({
url: '/extend/post',
method: "post",
data: {
msg: 'Hello'
}
})
axios('/extend/post', {
method: 'post',
data: {
msg: 'hello'
}
})
// For example, the same datastructure return value as the backend
interface ResponseData<T=any> {
code: number,
message: string,
data: T
}
// the same pojo、vo、entity and so on
interface User {
name: string,
age: number
}
function getUser<T>() {
return axios<ResponseData<T>>('/extend/user')
.then(res => res.data)
.catch(err => console.log(err));
}
async function test() {
const user = await getUser<User>();
if (user) {
console.log(user.data.name);
}
}
test();
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from '../../src/index';
const http = axios.create({
headers: {
"Content-Type": "application/json,charset=UTF-8"
}
})
http.interceptors.request.use(function (config: AxiosRequestConfig) {
// 在发送请求之前做些什么
return config;
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
});
http.interceptors.response.use(function (response) {
// 2xx 范围内的状态码都会触发该函数。
// 对响应数据做点什么
return response;
}, function (error) {
// 超出 2xx 范围的状态码都会触发该函数。
// 对响应错误做点什么
return Promise.reject(error);
});
import axios, { Canceler } from '../../src/index';
const CancelToken = axios.CancelToken;
const source = CancelToken.source();
axios.get("/cancel/get", {
cancelToken: source.token
}).catch(function(e) {
if (axios.isCancel(e)) {
console.log("Request Canceled", e.message);
}
});
setTimeout(() => {
}, 100);
let cancel: Canceler;
axios.get("/cancel/get", {
cancelToken: new CancelToken(c => {
cancel = c;
})
}).catch(function(e) {
if (axios.isCancel(e)) {
console.log("Request canceled");
}
})
- main
The most complete functionality
just implement params process
base on v1.1, here are implement features
- body params process
- handle exception
base on v1.2, here are implement features
- ts-axios interface extension
- response generic
- implement request and response interceptor
base v1.3, here are implement features
- rewrite config
- implement request interceptors and response interceptors
- static method of axios.create() and refine transform
base v1.4
- implement CancelToken
base v1.5
- add withCredential support
- add XSRF support
- implement two features for v1.6 2022/7/5
- implement with cancelToken 2022/4/3
- implement transformRequest, transformReponse and axios.crate() 2022/3/30
- implement interceptors 2022/3/27
- use mixed-type object to extend axios's interface, response generic 2022/3/19
- finish base axios function whitch including handleing params and body params. handle exceptions 2022/3/18
- implement buildURL function, and handle params process 2022/3/17
- init project on 2022/3/16