From fccd7d8e6fd518ac03b687d77b0e983fe52a8de3 Mon Sep 17 00:00:00 2001 From: Joe Schafer Date: Sat, 27 Feb 2016 17:08:41 -0500 Subject: [PATCH] axios: add functions to InterceptorManger Both the request and response interceptor are InterceptorManager's https://github.com/mzabriskie/axios/blob/master/lib/core/InterceptorManager.js So, they have `use` and `eject`. --- axios/axios-tests.ts | 22 ++++++++++++++++++++++ axios/axios.d.ts | 22 +++++++++++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/axios/axios-tests.ts b/axios/axios-tests.ts index 184292c92..040994531 100644 --- a/axios/axios-tests.ts +++ b/axios/axios-tests.ts @@ -18,11 +18,33 @@ axios.interceptors.request.use(config => { return config; }); + +const requestId: number = axios.interceptors.request.use( + (config) => { + console.log("Method:" + config.method + " Url:" +config.url); + return config; + }, + (error: any) => error); + + +axios.interceptors.request.eject(requestId); +axios.interceptors.request.eject(7); + + axios.interceptors.response.use(config => { console.log("Status:" + config.status); return config; }); +const responseId: number = axios.interceptors.response.use( + config => { + console.log("Status:" + config.status); + return config; + }, + (error: any) => error); + +axios.interceptors.response.eject(responseId); + axios.get("https://api.github.com/repos/mzabriskie/axios") .then(r => console.log(r.config.method)); diff --git a/axios/axios.d.ts b/axios/axios.d.ts index 7348ec651..5b610c1c1 100644 --- a/axios/axios.d.ts +++ b/axios/axios.d.ts @@ -1,4 +1,4 @@ -// Type definitions for axios 0.8.1 +// Type definitions for axios 0.9.1 // Project: https://github.com/mzabriskie/axios // Definitions by: Marcel Buesing // Definitions: https://github.com/borisyankov/DefinitelyTyped @@ -167,18 +167,34 @@ declare module Axios { response: ResponseInterceptor } + type InterceptorId = number; + interface RequestInterceptor { /** * - request body data type */ - use(fn: (config: AxiosXHRConfig) => AxiosXHRConfig): void; + + use(fulfilledFn: (config: AxiosXHRConfig) => AxiosXHRConfig): InterceptorId; + + use(fulfilledFn: (config: AxiosXHRConfig) => AxiosXHRConfig, + rejectedFn: (error: any) => any) + : InterceptorId; + + eject(interceptorId: InterceptorId): void; } interface ResponseInterceptor { /** * - expected response type */ - use(fn: (config: AxiosXHR) => AxiosXHR): void; + + use(fulfilledFn: (config: Axios.AxiosXHR) => Axios.AxiosXHR): InterceptorId; + + use(fulfilledFn: (config: Axios.AxiosXHR) => Axios.AxiosXHR, + rejectedFn: (error: any) => any) + : InterceptorId; + + eject(interceptorId: InterceptorId): void; } /**