diff --git a/.gitignore b/.gitignore index 28775f1..ca56b63 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .tscache .DS_Store node_modules/ +typings/ coverage/ test/typescript/axios.js* sauce_connect.log diff --git a/.npmignore b/.npmignore index 1887984..09739a3 100644 --- a/.npmignore +++ b/.npmignore @@ -3,6 +3,7 @@ coverage/ examples/ node_modules/ +typings/ sandbox/ test/ bower.json diff --git a/Gruntfile.js b/Gruntfile.js index 19615ff..5a737ec 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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', diff --git a/axios.d.ts b/axios.d.ts index 920657a..3ae48db 100644 --- a/axios.d.ts +++ b/axios.d.ts @@ -1,9 +1,3 @@ -export interface Thenable { - then(onFulfilled?: (value: V) => R | Thenable, onRejected?: (reason: any) => R | Thenable): Thenable; - then(onFulfilled?: (value: V) => R | Thenable, onRejected?: (reason: any) => void): Thenable; - catch(onRejected?: (reason: any) => R | Thenable): Thenable; -} - 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 { + then(onFulfilled: (value: V) => R1 | Promise, onRejected: (error: any) => R2 | Promise): Promise; + then(onFulfilled: (value: V) => R | Promise): Promise; + catch(onRejected: (error: any) => R | Promise): Promise; +} + +export interface AxiosPromise extends Promise { +} + +export interface InterceptorManager { + use(onFulfilled: (value: V) => V | Promise, onRejected?: (error: any) => any): number; + eject(id: number): void; +} + export interface AxiosInstance { defaults: AxiosRequestConfig; - request(config: AxiosRequestConfig): Thenable; - get(url: string, config?: AxiosRequestConfig): Thenable; - delete(url: string, config?: AxiosRequestConfig): Thenable; - head(url: string, config?: AxiosRequestConfig): Thenable; - post(url: string, data?: any, config?: AxiosRequestConfig): Thenable; - put(url: string, data?: any, config?: AxiosRequestConfig): Thenable; - patch(url: string, data?: any, config?: AxiosRequestConfig): Thenable; + interceptors: { + request: InterceptorManager; + response: InterceptorManager; + }; + 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; - (url: string, config?: AxiosRequestConfig): Thenable; + (config: AxiosRequestConfig): AxiosPromise; + (url: string, config?: AxiosRequestConfig): AxiosPromise; create(config?: AxiosRequestConfig): AxiosInstance; - all(iterable: any): Thenable; - spread(callback: any): Thenable; + all(iterable: any): any; + spread(callback: any): any; } declare const Axios: AxiosStatic; diff --git a/test/typescript/axios.ts b/test/typescript/axios.ts index 61a5ddd..03fc3ee 100644 --- a/test/typescript/axios.ts +++ b/test/typescript/axios.ts @@ -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)); diff --git a/typings.json b/typings.json new file mode 100644 index 0000000..e168043 --- /dev/null +++ b/typings.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "es6-promise": "registry:npm/es6-promise#3.0.0+20160723033700" + } +}