2
0
mirror of https://github.com/tenrok/axios.git synced 2026-05-15 11:59:42 +03:00

Adding TypeScript definitions for interceptors

This commit is contained in:
Nick Uraltsev
2016-08-16 09:34:23 -07:00
parent 8f0973c6b9
commit 0664d9895a
6 changed files with 75 additions and 19 deletions
+1
View File
@@ -3,6 +3,7 @@
.tscache
.DS_Store
node_modules/
typings/
coverage/
test/typescript/axios.js*
sauce_connect.log
+1
View File
@@ -3,6 +3,7 @@
coverage/
examples/
node_modules/
typings/
sandbox/
test/
bower.json
+1 -1
View File
@@ -13,7 +13,7 @@ module.exports = function(grunt) {
ts: {
test: {
src: ['test/typescript/*.ts'],
src: ['typings/index.d.ts', 'test/typescript/*.ts'],
out: 'test/typescript/out.js',
options: {
module: 'commonjs',
Vendored
+35 -17
View File
@@ -1,9 +1,3 @@
export interface Thenable<V> {
then<R>(onFulfilled?: (value: V) => R | Thenable<R>, onRejected?: (reason: any) => R | Thenable<R>): Thenable<R>;
then<R>(onFulfilled?: (value: V) => R | Thenable<R>, onRejected?: (reason: any) => void): Thenable<R>;
catch<R>(onRejected?: (reason: any) => R | Thenable<R>): Thenable<R>;
}
export interface AxiosDataTransformer {
(data: any): any;
}
@@ -41,23 +35,47 @@ export interface AxiosResponse {
config: AxiosRequestConfig;
}
export interface AxiosError extends Error {
config: AxiosRequestConfig;
code?: string;
response?: AxiosResponse;
}
export interface Promise<V> {
then<R1, R2>(onFulfilled: (value: V) => R1 | Promise<R1>, onRejected: (error: any) => R2 | Promise<R2>): Promise<R1 | R2>;
then<R>(onFulfilled: (value: V) => R | Promise<R>): Promise<R>;
catch<R>(onRejected: (error: any) => R | Promise<R>): Promise<R>;
}
export interface AxiosPromise extends Promise<AxiosResponse> {
}
export interface InterceptorManager<V> {
use(onFulfilled: (value: V) => V | Promise<V>, onRejected?: (error: any) => any): number;
eject(id: number): void;
}
export interface AxiosInstance {
defaults: AxiosRequestConfig;
request(config: AxiosRequestConfig): Thenable<AxiosResponse>;
get(url: string, config?: AxiosRequestConfig): Thenable<AxiosResponse>;
delete(url: string, config?: AxiosRequestConfig): Thenable<AxiosResponse>;
head(url: string, config?: AxiosRequestConfig): Thenable<AxiosResponse>;
post(url: string, data?: any, config?: AxiosRequestConfig): Thenable<AxiosResponse>;
put(url: string, data?: any, config?: AxiosRequestConfig): Thenable<AxiosResponse>;
patch(url: string, data?: any, config?: AxiosRequestConfig): Thenable<AxiosResponse>;
interceptors: {
request: InterceptorManager<AxiosRequestConfig>;
response: InterceptorManager<AxiosResponse>;
};
request(config: AxiosRequestConfig): AxiosPromise;
get(url: string, config?: AxiosRequestConfig): AxiosPromise;
delete(url: string, config?: AxiosRequestConfig): AxiosPromise;
head(url: string, config?: AxiosRequestConfig): AxiosPromise;
post(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
put(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
patch(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
}
export interface AxiosStatic extends AxiosInstance {
(config: AxiosRequestConfig): Thenable<AxiosResponse>;
(url: string, config?: AxiosRequestConfig): Thenable<AxiosResponse>;
(config: AxiosRequestConfig): AxiosPromise;
(url: string, config?: AxiosRequestConfig): AxiosPromise;
create(config?: AxiosRequestConfig): AxiosInstance;
all(iterable: any): Thenable<any>;
spread(callback: any): Thenable<any>;
all(iterable: any): any;
spread(callback: any): any;
}
declare const Axios: AxiosStatic;
+32 -1
View File
@@ -1,4 +1,5 @@
import axios from '../../';
import axios, { AxiosRequestConfig, AxiosResponse, AxiosError, AxiosInstance } from '../../';
import { Promise } from 'es6-promise';
axios.get('/user?ID=12345')
.then(function (response) {
@@ -155,3 +156,33 @@ instance.get('/user', {
.catch(function (response) {
console.log(response);
});
const requestInterceptorId: number = axios.interceptors.request.use(
(config: AxiosRequestConfig) => config,
(error: any) => Promise.reject(error)
);
axios.interceptors.request.eject(requestInterceptorId);
axios.interceptors.request.use(
(config: AxiosRequestConfig) => Promise.resolve(config),
(error: any) => Promise.reject(error)
);
axios.interceptors.request.use((config: AxiosRequestConfig) => config);
axios.interceptors.request.use((config: AxiosRequestConfig) => Promise.resolve(config));
const responseInterceptorId: number = axios.interceptors.response.use(
(response: AxiosResponse) => response,
(error: any) => Promise.reject(error)
);
axios.interceptors.response.eject(responseInterceptorId);
axios.interceptors.response.use(
(response: AxiosResponse) => Promise.resolve(response),
(error: any) => Promise.reject(error)
);
axios.interceptors.response.use((response: AxiosResponse) => response);
axios.interceptors.response.use((response: AxiosResponse) => Promise.resolve(response));
+5
View File
@@ -0,0 +1,5 @@
{
"dependencies": {
"es6-promise": "registry:npm/es6-promise#3.0.0+20160723033700"
}
}