Add promise-based interceptor definitions

This commit is contained in:
Chris Scribner
2017-01-24 10:41:26 -08:00
parent 99d41f818c
commit fbbdd4fa3c
2 changed files with 35 additions and 5 deletions

View File

@@ -13,6 +13,13 @@ interface Issue {
title: string;
}
function makePromise(val: any) {
return <Axios.IPromise<any>>{
then: () => val,
catch: () => val
};
}
axios.interceptors.request.use<any>(config => {
console.log("Method:" + config.method + " Url:" +config.url);
return config;
@@ -30,12 +37,23 @@ const requestId: number = axios.interceptors.request.use<any>(
axios.interceptors.request.eject(requestId);
axios.interceptors.request.eject(7);
const requestId2: number = axios.interceptors.request.use<any>(
(config) => {
console.log("Method:" + config.method + " Url:" +config.url);
return makePromise(config);
},
(error: any) => error);
axios.interceptors.response.use<any>(config => {
console.log("Status:" + config.status);
return config;
});
axios.interceptors.response.use<any>(config => {
console.log("Status:" + config.status);
return makePromise(config);
});
const responseId: number = axios.interceptors.response.use<any>(
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";
axiosInstance.defaults.headers.common['Authorization'] = "AUTH_TOKEN";

20
axios/index.d.ts vendored
View File

@@ -99,10 +99,10 @@ declare namespace Axios {
* change the response data to be made before it is passed to then/catch
*/
transformResponse?: <U>(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<U>(fulfilledFn: (config: AxiosXHRConfig<U>) => IPromise<AxiosXHRConfig<U>>): InterceptorId;
use<U>(fulfilledFn: (config: AxiosXHRConfig<U>) => IPromise<AxiosXHRConfig<U>>,
rejectedFn: (error: any) => any)
: InterceptorId;
eject(interceptorId: InterceptorId): void;
}
@@ -209,10 +215,16 @@ declare namespace Axios {
use<T>(fulfilledFn: (config: Axios.AxiosXHR<T>) => Axios.AxiosXHR<T>): InterceptorId;
use<T>(fulfilledFn: (config: Axios.AxiosXHR<T>) => IPromise<Axios.AxiosXHR<T>>): InterceptorId;
use<T>(fulfilledFn: (config: Axios.AxiosXHR<T>) => Axios.AxiosXHR<T>,
rejectedFn: (error: any) => any)
: InterceptorId;
use<T>(fulfilledFn: (config: Axios.AxiosXHR<T>) => IPromise<Axios.AxiosXHR<T>>,
rejectedFn: (error: any) => any)
: InterceptorId;
eject(interceptorId: InterceptorId): void;
}