mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Adding TypeScript definitions for cancel tokens
This commit is contained in:
Vendored
+31
@@ -41,6 +41,7 @@ export interface AxiosRequestConfig {
|
|||||||
httpAgent?: any;
|
httpAgent?: any;
|
||||||
httpsAgent?: any;
|
httpsAgent?: any;
|
||||||
proxy?: AxiosProxyConfig;
|
proxy?: AxiosProxyConfig;
|
||||||
|
cancelToken?: CancelToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosResponse {
|
export interface AxiosResponse {
|
||||||
@@ -66,6 +67,34 @@ export interface Promise<V> {
|
|||||||
export interface AxiosPromise extends Promise<AxiosResponse> {
|
export interface AxiosPromise extends Promise<AxiosResponse> {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CancelStatic {
|
||||||
|
new (message?: string): Cancel;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Cancel {
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Canceler {
|
||||||
|
(message?: string): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CancelTokenStatic {
|
||||||
|
new (executor: (cancel: Canceler) => void): CancelToken;
|
||||||
|
source(): CancelTokenSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CancelToken {
|
||||||
|
promise: Promise<Cancel>;
|
||||||
|
reason?: Cancel;
|
||||||
|
throwIfRequested(): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CancelTokenSource {
|
||||||
|
token: CancelToken;
|
||||||
|
cancel: Canceler;
|
||||||
|
}
|
||||||
|
|
||||||
export interface AxiosInterceptorManager<V> {
|
export interface AxiosInterceptorManager<V> {
|
||||||
use(onFulfilled: (value: V) => V | Promise<V>, onRejected?: (error: any) => any): number;
|
use(onFulfilled: (value: V) => V | Promise<V>, onRejected?: (error: any) => any): number;
|
||||||
eject(id: number): void;
|
eject(id: number): void;
|
||||||
@@ -90,6 +119,8 @@ export interface AxiosStatic extends AxiosInstance {
|
|||||||
(config: AxiosRequestConfig): AxiosPromise;
|
(config: AxiosRequestConfig): AxiosPromise;
|
||||||
(url: string, config?: AxiosRequestConfig): AxiosPromise;
|
(url: string, config?: AxiosRequestConfig): AxiosPromise;
|
||||||
create(config?: AxiosRequestConfig): AxiosInstance;
|
create(config?: AxiosRequestConfig): AxiosInstance;
|
||||||
|
Cancel: CancelStatic;
|
||||||
|
CancelToken: CancelTokenStatic;
|
||||||
all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
|
all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
|
||||||
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
|
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,15 @@
|
|||||||
import axios, { AxiosRequestConfig, AxiosResponse, AxiosError, AxiosInstance, AxiosAdapter } from '../../';
|
import axios, {
|
||||||
|
AxiosRequestConfig,
|
||||||
|
AxiosResponse,
|
||||||
|
AxiosError,
|
||||||
|
AxiosInstance,
|
||||||
|
AxiosAdapter,
|
||||||
|
Cancel,
|
||||||
|
CancelToken,
|
||||||
|
CancelTokenSource,
|
||||||
|
Canceler
|
||||||
|
} from '../../';
|
||||||
|
|
||||||
import { Promise } from 'es6-promise';
|
import { Promise } from 'es6-promise';
|
||||||
|
|
||||||
const config: AxiosRequestConfig = {
|
const config: AxiosRequestConfig = {
|
||||||
@@ -30,7 +41,8 @@ const config: AxiosRequestConfig = {
|
|||||||
proxy: {
|
proxy: {
|
||||||
host: '127.0.0.1',
|
host: '127.0.0.1',
|
||||||
port: 9000
|
port: 9000
|
||||||
}
|
},
|
||||||
|
cancelToken: new axios.CancelToken((cancel: Canceler) => {})
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleResponse = (response: AxiosResponse) => {
|
const handleResponse = (response: AxiosResponse) => {
|
||||||
@@ -210,3 +222,18 @@ axios.get('/user')
|
|||||||
axios.get('/user')
|
axios.get('/user')
|
||||||
.catch((error: any) => Promise.resolve('foo'))
|
.catch((error: any) => Promise.resolve('foo'))
|
||||||
.then((value: string) => {});
|
.then((value: string) => {});
|
||||||
|
|
||||||
|
// Cancellation
|
||||||
|
|
||||||
|
const source: CancelTokenSource = axios.CancelToken.source();
|
||||||
|
|
||||||
|
axios.get('/user', {
|
||||||
|
cancelToken: source.token
|
||||||
|
}).catch((thrown: AxiosError | Cancel) => {
|
||||||
|
if (thrown instanceof axios.Cancel) {
|
||||||
|
const cancel: Cancel = thrown;
|
||||||
|
console.log(cancel.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
source.cancel('Operation has been canceled.');
|
||||||
|
|||||||
Reference in New Issue
Block a user