Skip to content

Commit

Permalink
Adding TypeScript definitions for interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
nickuraltsev committed Aug 16, 2016
1 parent 8f0973c commit 0664d98
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.tscache
.DS_Store
node_modules/
typings/
coverage/
test/typescript/axios.js*
sauce_connect.log
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
coverage/
examples/
node_modules/
typings/
sandbox/
test/
bower.json
Expand Down
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
52 changes: 35 additions & 17 deletions axios.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 32 additions & 1 deletion test/typescript/axios.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 changes: 5 additions & 0 deletions typings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"es6-promise": "registry:npm/es6-promise#3.0.0+20160723033700"
}
}

0 comments on commit 0664d98

Please sign in to comment.