mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Adding TypeScript definitions for adapters
This commit is contained in:
Vendored
+17
-8
@@ -1,21 +1,30 @@
|
|||||||
export interface AxiosDataTransformer {
|
export interface AxiosTransformer {
|
||||||
(data: any): any;
|
(data: any): any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface AxiosAdapter {
|
||||||
|
(config: AxiosRequestConfig): AxiosPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AxiosBasicCredentials {
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface AxiosRequestConfig {
|
export interface AxiosRequestConfig {
|
||||||
url?: string;
|
url?: string;
|
||||||
method?: string;
|
method?: string;
|
||||||
baseURL?: string;
|
baseURL?: string;
|
||||||
transformRequest?: AxiosDataTransformer | AxiosDataTransformer[];
|
transformRequest?: AxiosTransformer | AxiosTransformer[];
|
||||||
transformResponse?: AxiosDataTransformer | AxiosDataTransformer[];
|
transformResponse?: AxiosTransformer | AxiosTransformer[];
|
||||||
headers?: any;
|
headers?: any;
|
||||||
params?: any;
|
params?: any;
|
||||||
paramsSerializer?: (params: any) => string;
|
paramsSerializer?: (params: any) => string;
|
||||||
data?: any;
|
data?: any;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
withCredentials?: boolean;
|
withCredentials?: boolean;
|
||||||
adapter?: any;
|
adapter?: AxiosAdapter;
|
||||||
auth?: any;
|
auth?: AxiosBasicCredentials;
|
||||||
responseType?: string;
|
responseType?: string;
|
||||||
xsrfCookieName?: string;
|
xsrfCookieName?: string;
|
||||||
xsrfHeaderName?: string;
|
xsrfHeaderName?: string;
|
||||||
@@ -50,7 +59,7 @@ export interface Promise<V> {
|
|||||||
export interface AxiosPromise extends Promise<AxiosResponse> {
|
export interface AxiosPromise extends Promise<AxiosResponse> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface InterceptorManager<V> {
|
export interface AxiosInterceptorManager<V> {
|
||||||
use(onFulfilled: (value: V) => V | Promise<V>, onRejected?: (error: any) => any): number;
|
use(onFulfilled: (value: V) => V | Promise<V>, onRejected?: (error: any) => any): number;
|
||||||
eject(id: number): void;
|
eject(id: number): void;
|
||||||
}
|
}
|
||||||
@@ -58,8 +67,8 @@ export interface InterceptorManager<V> {
|
|||||||
export interface AxiosInstance {
|
export interface AxiosInstance {
|
||||||
defaults: AxiosRequestConfig;
|
defaults: AxiosRequestConfig;
|
||||||
interceptors: {
|
interceptors: {
|
||||||
request: InterceptorManager<AxiosRequestConfig>;
|
request: AxiosInterceptorManager<AxiosRequestConfig>;
|
||||||
response: InterceptorManager<AxiosResponse>;
|
response: AxiosInterceptorManager<AxiosResponse>;
|
||||||
};
|
};
|
||||||
request(config: AxiosRequestConfig): AxiosPromise;
|
request(config: AxiosRequestConfig): AxiosPromise;
|
||||||
get(url: string, config?: AxiosRequestConfig): AxiosPromise;
|
get(url: string, config?: AxiosRequestConfig): AxiosPromise;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import axios, { AxiosRequestConfig, AxiosResponse, AxiosError, AxiosInstance } from '../../';
|
import axios, { AxiosRequestConfig, AxiosResponse, AxiosInstance, AxiosAdapter } from '../../';
|
||||||
import { Promise } from 'es6-promise';
|
import { Promise } from 'es6-promise';
|
||||||
|
|
||||||
axios.get('/user?ID=12345')
|
axios.get('/user?ID=12345')
|
||||||
@@ -157,6 +157,8 @@ instance.get('/user', {
|
|||||||
console.log(response);
|
console.log(response);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Interceptors
|
||||||
|
|
||||||
const requestInterceptorId: number = axios.interceptors.request.use(
|
const requestInterceptorId: number = axios.interceptors.request.use(
|
||||||
(config: AxiosRequestConfig) => config,
|
(config: AxiosRequestConfig) => config,
|
||||||
(error: any) => Promise.reject(error)
|
(error: any) => Promise.reject(error)
|
||||||
@@ -186,3 +188,18 @@ axios.interceptors.response.use(
|
|||||||
|
|
||||||
axios.interceptors.response.use((response: AxiosResponse) => response);
|
axios.interceptors.response.use((response: AxiosResponse) => response);
|
||||||
axios.interceptors.response.use((response: AxiosResponse) => Promise.resolve(response));
|
axios.interceptors.response.use((response: AxiosResponse) => Promise.resolve(response));
|
||||||
|
|
||||||
|
// Adapters
|
||||||
|
|
||||||
|
const adapter: AxiosAdapter = (config: AxiosRequestConfig) => {
|
||||||
|
const response: AxiosResponse = {
|
||||||
|
data: 'foo',
|
||||||
|
status: 200,
|
||||||
|
statusText: 'OK',
|
||||||
|
headers: { 'X-FOO': 'bar' },
|
||||||
|
config: config,
|
||||||
|
};
|
||||||
|
return Promise.resolve(response);
|
||||||
|
};
|
||||||
|
|
||||||
|
axios.defaults.adapter = adapter;
|
||||||
|
|||||||
Reference in New Issue
Block a user