2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-20 20:00:40 +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 .tscache
.DS_Store .DS_Store
node_modules/ node_modules/
typings/
coverage/ coverage/
test/typescript/axios.js* test/typescript/axios.js*
sauce_connect.log sauce_connect.log
+1
View File
@@ -3,6 +3,7 @@
coverage/ coverage/
examples/ examples/
node_modules/ node_modules/
typings/
sandbox/ sandbox/
test/ test/
bower.json bower.json
+1 -1
View File
@@ -13,7 +13,7 @@ module.exports = function(grunt) {
ts: { ts: {
test: { test: {
src: ['test/typescript/*.ts'], src: ['typings/index.d.ts', 'test/typescript/*.ts'],
out: 'test/typescript/out.js', out: 'test/typescript/out.js',
options: { options: {
module: 'commonjs', 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 { export interface AxiosDataTransformer {
(data: any): any; (data: any): any;
} }
@@ -41,23 +35,47 @@ export interface AxiosResponse {
config: AxiosRequestConfig; 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 { export interface AxiosInstance {
defaults: AxiosRequestConfig; defaults: AxiosRequestConfig;
request(config: AxiosRequestConfig): Thenable<AxiosResponse>; interceptors: {
get(url: string, config?: AxiosRequestConfig): Thenable<AxiosResponse>; request: InterceptorManager<AxiosRequestConfig>;
delete(url: string, config?: AxiosRequestConfig): Thenable<AxiosResponse>; response: InterceptorManager<AxiosResponse>;
head(url: string, config?: AxiosRequestConfig): Thenable<AxiosResponse>; };
post(url: string, data?: any, config?: AxiosRequestConfig): Thenable<AxiosResponse>; request(config: AxiosRequestConfig): AxiosPromise;
put(url: string, data?: any, config?: AxiosRequestConfig): Thenable<AxiosResponse>; get(url: string, config?: AxiosRequestConfig): AxiosPromise;
patch(url: string, data?: any, config?: AxiosRequestConfig): Thenable<AxiosResponse>; 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 { export interface AxiosStatic extends AxiosInstance {
(config: AxiosRequestConfig): Thenable<AxiosResponse>; (config: AxiosRequestConfig): AxiosPromise;
(url: string, config?: AxiosRequestConfig): Thenable<AxiosResponse>; (url: string, config?: AxiosRequestConfig): AxiosPromise;
create(config?: AxiosRequestConfig): AxiosInstance; create(config?: AxiosRequestConfig): AxiosInstance;
all(iterable: any): Thenable<any>; all(iterable: any): any;
spread(callback: any): Thenable<any>; spread(callback: any): any;
} }
declare const Axios: AxiosStatic; 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') axios.get('/user?ID=12345')
.then(function (response) { .then(function (response) {
@@ -155,3 +156,33 @@ instance.get('/user', {
.catch(function (response) { .catch(function (response) {
console.log(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"
}
}