From 2033ef3ad01ae3c83dc0e39adddf682852b17939 Mon Sep 17 00:00:00 2001 From: Nick Uraltsev Date: Sat, 17 Sep 2016 12:49:14 -0700 Subject: [PATCH] Adding TypeScript definitions for cancel tokens --- axios.d.ts | 31 +++++++++++++++++++++++++++++++ test/typescript/axios.ts | 31 +++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/axios.d.ts b/axios.d.ts index d672175..88d283a 100644 --- a/axios.d.ts +++ b/axios.d.ts @@ -41,6 +41,7 @@ export interface AxiosRequestConfig { httpAgent?: any; httpsAgent?: any; proxy?: AxiosProxyConfig; + cancelToken?: CancelToken; } export interface AxiosResponse { @@ -66,6 +67,34 @@ export interface Promise { export interface AxiosPromise extends Promise { } +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; + reason?: Cancel; + throwIfRequested(): void; +} + +export interface CancelTokenSource { + token: CancelToken; + cancel: Canceler; +} + export interface AxiosInterceptorManager { use(onFulfilled: (value: V) => V | Promise, onRejected?: (error: any) => any): number; eject(id: number): void; @@ -90,6 +119,8 @@ export interface AxiosStatic extends AxiosInstance { (config: AxiosRequestConfig): AxiosPromise; (url: string, config?: AxiosRequestConfig): AxiosPromise; create(config?: AxiosRequestConfig): AxiosInstance; + Cancel: CancelStatic; + CancelToken: CancelTokenStatic; all(values: (T | Promise)[]): Promise; spread(callback: (...args: T[]) => R): (array: T[]) => R; } diff --git a/test/typescript/axios.ts b/test/typescript/axios.ts index 9f25f1b..39571a9 100644 --- a/test/typescript/axios.ts +++ b/test/typescript/axios.ts @@ -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'; const config: AxiosRequestConfig = { @@ -30,7 +41,8 @@ const config: AxiosRequestConfig = { proxy: { host: '127.0.0.1', port: 9000 - } + }, + cancelToken: new axios.CancelToken((cancel: Canceler) => {}) }; const handleResponse = (response: AxiosResponse) => { @@ -210,3 +222,18 @@ axios.get('/user') axios.get('/user') .catch((error: any) => Promise.resolve('foo')) .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.');