mirror of
https://github.com/tenrok/axios.git
synced 2026-05-30 15:24:11 +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;
|
||||
}
|
||||
|
||||
export interface AxiosAdapter {
|
||||
(config: AxiosRequestConfig): AxiosPromise;
|
||||
}
|
||||
|
||||
export interface AxiosBasicCredentials {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface AxiosRequestConfig {
|
||||
url?: string;
|
||||
method?: string;
|
||||
baseURL?: string;
|
||||
transformRequest?: AxiosDataTransformer | AxiosDataTransformer[];
|
||||
transformResponse?: AxiosDataTransformer | AxiosDataTransformer[];
|
||||
transformRequest?: AxiosTransformer | AxiosTransformer[];
|
||||
transformResponse?: AxiosTransformer | AxiosTransformer[];
|
||||
headers?: any;
|
||||
params?: any;
|
||||
paramsSerializer?: (params: any) => string;
|
||||
data?: any;
|
||||
timeout?: number;
|
||||
withCredentials?: boolean;
|
||||
adapter?: any;
|
||||
auth?: any;
|
||||
adapter?: AxiosAdapter;
|
||||
auth?: AxiosBasicCredentials;
|
||||
responseType?: string;
|
||||
xsrfCookieName?: string;
|
||||
xsrfHeaderName?: string;
|
||||
@@ -50,7 +59,7 @@ export interface Promise<V> {
|
||||
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;
|
||||
eject(id: number): void;
|
||||
}
|
||||
@@ -58,8 +67,8 @@ export interface InterceptorManager<V> {
|
||||
export interface AxiosInstance {
|
||||
defaults: AxiosRequestConfig;
|
||||
interceptors: {
|
||||
request: InterceptorManager<AxiosRequestConfig>;
|
||||
response: InterceptorManager<AxiosResponse>;
|
||||
request: AxiosInterceptorManager<AxiosRequestConfig>;
|
||||
response: AxiosInterceptorManager<AxiosResponse>;
|
||||
};
|
||||
request(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';
|
||||
|
||||
axios.get('/user?ID=12345')
|
||||
@@ -157,6 +157,8 @@ instance.get('/user', {
|
||||
console.log(response);
|
||||
});
|
||||
|
||||
// Interceptors
|
||||
|
||||
const requestInterceptorId: number = axios.interceptors.request.use(
|
||||
(config: AxiosRequestConfig) => config,
|
||||
(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) => 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