From fbbdd4fa3c62077b1829fe1850c20ffb50c7044c Mon Sep 17 00:00:00 2001 From: Chris Scribner Date: Tue, 24 Jan 2017 10:41:26 -0800 Subject: [PATCH] Add promise-based interceptor definitions --- axios/axios-tests.ts | 20 +++++++++++++++++++- axios/index.d.ts | 20 ++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/axios/axios-tests.ts b/axios/axios-tests.ts index eb98e2710c..2587222b36 100644 --- a/axios/axios-tests.ts +++ b/axios/axios-tests.ts @@ -13,6 +13,13 @@ interface Issue { title: string; } +function makePromise(val: any) { + return >{ + then: () => val, + catch: () => val + }; +} + axios.interceptors.request.use(config => { console.log("Method:" + config.method + " Url:" +config.url); return config; @@ -30,12 +37,23 @@ const requestId: number = axios.interceptors.request.use( axios.interceptors.request.eject(requestId); axios.interceptors.request.eject(7); +const requestId2: number = axios.interceptors.request.use( + (config) => { + console.log("Method:" + config.method + " Url:" +config.url); + return makePromise(config); + }, + (error: any) => error); axios.interceptors.response.use(config => { console.log("Status:" + config.status); return config; }); +axios.interceptors.response.use(config => { + console.log("Status:" + config.status); + return makePromise(config); +}); + const responseId: number = axios.interceptors.response.use( config => { console.log("Status:" + config.status); @@ -104,4 +122,4 @@ axios.defaults.baseURL = 'https://api.example.com'; axios.defaults.headers.common['Authorization'] = "AUTH_TOKEN"; axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; -axiosInstance.defaults.headers.common['Authorization'] = "AUTH_TOKEN"; \ No newline at end of file +axiosInstance.defaults.headers.common['Authorization'] = "AUTH_TOKEN"; diff --git a/axios/index.d.ts b/axios/index.d.ts index 681fefc806..b32a78ebe0 100644 --- a/axios/index.d.ts +++ b/axios/index.d.ts @@ -99,10 +99,10 @@ declare namespace Axios { * change the response data to be made before it is passed to then/catch */ transformResponse?: (data: T) => U; - - /** - * defines whether to resolve or reject the promise for a given HTTP response status code. - * If returns `true` (or is set to `null` or `undefined`), the promise will be resolved; + + /** + * defines whether to resolve or reject the promise for a given HTTP response status code. + * If returns `true` (or is set to `null` or `undefined`), the promise will be resolved; * otherwise, the promise will be rejected */ validateStatus?: (status: number) => boolean | undefined; @@ -199,6 +199,12 @@ declare namespace Axios { rejectedFn: (error: any) => any) : InterceptorId; + use(fulfilledFn: (config: AxiosXHRConfig) => IPromise>): InterceptorId; + + use(fulfilledFn: (config: AxiosXHRConfig) => IPromise>, + rejectedFn: (error: any) => any) + : InterceptorId; + eject(interceptorId: InterceptorId): void; } @@ -209,10 +215,16 @@ declare namespace Axios { use(fulfilledFn: (config: Axios.AxiosXHR) => Axios.AxiosXHR): InterceptorId; + use(fulfilledFn: (config: Axios.AxiosXHR) => IPromise>): InterceptorId; + use(fulfilledFn: (config: Axios.AxiosXHR) => Axios.AxiosXHR, rejectedFn: (error: any) => any) : InterceptorId; + use(fulfilledFn: (config: Axios.AxiosXHR) => IPromise>, + rejectedFn: (error: any) => any) + : InterceptorId; + eject(interceptorId: InterceptorId): void; }