0. 系列文章
1.使用Typescript重構axios(一)——寫在最前面
2.使用Typescript重構axios(二)——項目起手,跑通流程
3.使用Typescript重構axios(三)——實現基礎功能:處理get請求url參數
4.使用Typescript重構axios(四)——實現基礎功能:處理post請求參數
5.使用Typescript重構axios(五)——實現基礎功能:處理請求的header
6.使用Typescript重構axios(六)——實現基礎功能:獲取響應數據
7.使用Typescript重構axios(七)——實現基礎功能:處理響應header
8.使用Typescript重構axios(八)——實現基礎功能:處理響應data
9.使用Typescript重構axios(九)——異常處理:基礎版
10.使用Typescript重構axios(十)——異常處理:增強版
11.使用Typescript重構axios(十一)——接口擴展
12.使用Typescript重構axios(十二)——增加參數
13.使用Typescript重構axios(十三)——讓響應數據支持泛型
14.使用Typescript重構axios(十四)——實現攔截器
15.使用Typescript重構axios(十五)——默認配置
16.使用Typescript重構axios(十六)——請求和響應數據配置化
17.使用Typescript重構axios(十七)——增加axios.create
18.使用Typescript重構axios(十八)——請求取消功能:總體思路
19.使用Typescript重構axios(十九)——請求取消功能:實現第二種使用方式
20.使用Typescript重構axios(二十)——請求取消功能:實現第一種使用方式
21.使用Typescript重構axios(二十一)——請求取消功能:添加axios.isCancel接口
22.使用Typescript重構axios(二十二)——請求取消功能:收尾
23.使用Typescript重構axios(二十三)——添加withCredentials屬性
24.使用Typescript重構axios(二十四)——防御XSRF攻擊
25.使用Typescript重構axios(二十五)——文件上傳下載進度監控
26.使用Typescript重構axios(二十六)——添加HTTP授權auth屬性
27.使用Typescript重構axios(二十七)——添加請求狀態碼合法性校驗
28.使用Typescript重構axios(二十八)——自定義序列化請求參數
29.使用Typescript重構axios(二十九)——添加baseURL
30.使用Typescript重構axios(三十)——添加axios.getUri方法
31.使用Typescript重構axios(三十一)——添加axios.all和axios.spread方法
32.使用Typescript重構axios(三十二)——寫在最后面(總結)
1. 前言
雖然我們現在已經實現了官方axios
請求取消功能的兩種使用方式,但是官方axios
上還有個isCancel
接口還未實現。該接口接收一個異常對象e
作為參數,用來判斷該異常是否是由取消請求導致的,如果是的話該異常對象就應該是請求取消的原因;該接口實現起來也不難,我們可以創建一個取消原因Cancel
類,而把請求取消的原因作為該類的實例,這樣我們在捕獲異常的時候只需判斷異常對象是不是Cancel
類的實例,如果是的話,那么它就是請求取消原因,否則就是其他異常。OK,思路就是這樣,接下來,我們就來實現它。
2. 定義Cancel接口類型
創建cancel
類之前,我們先在src/types/index.ts
中定義接口類型,如下:
export interface Cancel {
message?: string
}
export interface CancelStatic {
new(message?: string): Cancel
}
我們先定義了Cancel
類的實例對象類型Cancel
,它里面只有一個屬性,那就是取消原因message
,接着還定義了Cancel
類的類類型,它里面包含了構造函數屬性,構造函數接收取消原因作為參數,返回Cancel
類的實例對象。
3. 創建Cancel類
接口類型定義好之后,我們就來創建Cancel
類,我們在src/cancel
目錄下創建Cancel.ts
文件,在該文件內創建Cancel
類,如下:
export default class Cancel {
message: string;
constructor(message:string){
this.message = message
}
}
該類的實現非常簡單,就是實例化一個取消原因對象,該對象的message
屬性就是請求的取消原因。
4. 創建isCancel方法
創建好Cancel
類之后,我們還應該創建一個isCancel
函數,該函數用來判斷異常對象是不是取消原因對象,返回true
或false
。我們在src/cancel
目錄下創建isCancel.ts
文件,在該文件內創建isCancel
函數,如下:
import Cancel from "./Cancel";
export default function isCancel(val:any):boolean {
return val instanceof Cancel
}
判斷異常對象是不是取消原因對象,我們只需判斷它是不是Cancel
類的實例即可。
5. 添加Cancel和isCancel接口
創建好isCancel
函數后,最后,我們將其添加到axios
混合對象上,添加之前,還是要先在axios
混合對象接口定義中添加isCancel
屬性,如下:
export interface AxiosStatic extends AxiosInstance {
create(config?: AxiosRequestConfig): AxiosInstance;
CancelToken: CancelTokenStatic;
Cancel: CancelStatic;
isCancel: (value: any) => boolean;
}
添加好接口類型以后,我們就可以在src/axios.ts
中給axios
混合對象添加Cancel
和isCancel
接口了,如下:
import Cancel from "./cancel/Cancel";
import isCancel from "./cancel/isCancel";
axios.Cancel = Cancel;
axios.isCancel = isCancel;
6. 修改之前的取消原因類型
我們現在創建取消原因Cancel
類,所以我們需要將之前寫的取消原因message
從string
類型改成Cancel
類型。需要改動如下幾個地方:
src/types/index.ts
:
export interface CancelToken {
promise: Promise<Cancel>
reason?: Cancel
}
src/cancel/CancelToken.ts
import { Canceler, CancelExecutor, CancelTokenSource} from "../types";
import Cancel from "./Cancel";
interface ResolvePromise {
(reason?: Cancel): void;
}
export default class CancelToken {
promise: Promise<Cancel>;
reason?: Cancel;
constructor(executor: CancelExecutor) {
let resolvePromise: ResolvePromise;
this.promise = new Promise<Cancel>(resolve => {
resolvePromise = resolve;
});
executor(message => {
if (this.reason) {
return;
}
this.reason = new Cancel(message);
resolvePromise(this.reason);
});
}
static source(): CancelTokenSource {
let cancel!: Canceler;
let token = new CancelToken(c => {
cancel = c;
});
return {
cancel,
token
};
}
}
修改完之后,isCancel
接口就算實現完畢了,接下來,我們就編寫demo
測試效果如何。
7. demo編寫
我們繼續沿用上篇文章的demo
,只需在src/examples/cancel/app.ts
文件中捕獲異常的地方添加上本篇文章實現的異常對象判斷即可,如下:
axios
.get("/api/cancel", {
cancelToken: new CancelToken(c => {
cancel = c;
})
})
.catch(function(e) {
// 新增
if (axios.isCancel(e)){
console.log(`請求取消原因:${e.message}`);
}
});
然后運行項目,我們打開 chrome
瀏覽器,訪問 http://localhost:8000/ 即可訪問我們的 demo
了,我們點擊 cancel
,通過F12
的控制台中,我們看到取消原因已經被打印出來了。
(完)