mirror of
https://github.com/tenrok/axios.git
synced 2026-06-23 20:40:40 +03:00
Updated TypeScript typings with generic type parameters for request methods & AxiosResponse
This commit is contained in:
Vendored
+9
-9
@@ -3,7 +3,7 @@ export interface AxiosTransformer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosAdapter {
|
export interface AxiosAdapter {
|
||||||
(config: AxiosRequestConfig): AxiosPromise;
|
(config: AxiosRequestConfig): AxiosPromise<any>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosBasicCredentials {
|
export interface AxiosBasicCredentials {
|
||||||
@@ -44,8 +44,8 @@ export interface AxiosRequestConfig {
|
|||||||
cancelToken?: CancelToken;
|
cancelToken?: CancelToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosResponse {
|
export interface AxiosResponse<T = any> {
|
||||||
data: any;
|
data: T;
|
||||||
status: number;
|
status: number;
|
||||||
statusText: string;
|
statusText: string;
|
||||||
headers: any;
|
headers: any;
|
||||||
@@ -59,7 +59,7 @@ export interface AxiosError extends Error {
|
|||||||
response?: AxiosResponse;
|
response?: AxiosResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosPromise extends Promise<AxiosResponse> {
|
export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CancelStatic {
|
export interface CancelStatic {
|
||||||
@@ -101,13 +101,13 @@ export interface AxiosInstance {
|
|||||||
request: AxiosInterceptorManager<AxiosRequestConfig>;
|
request: AxiosInterceptorManager<AxiosRequestConfig>;
|
||||||
response: AxiosInterceptorManager<AxiosResponse>;
|
response: AxiosInterceptorManager<AxiosResponse>;
|
||||||
};
|
};
|
||||||
request(config: AxiosRequestConfig): AxiosPromise;
|
request<T = any>(config: AxiosRequestConfig): AxiosPromise<T>;
|
||||||
get(url: string, config?: AxiosRequestConfig): AxiosPromise;
|
get<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
|
||||||
delete(url: string, config?: AxiosRequestConfig): AxiosPromise;
|
delete(url: string, config?: AxiosRequestConfig): AxiosPromise;
|
||||||
head(url: string, config?: AxiosRequestConfig): AxiosPromise;
|
head(url: string, config?: AxiosRequestConfig): AxiosPromise;
|
||||||
post(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
|
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
|
||||||
put(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
|
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
|
||||||
patch(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
|
patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosStatic extends AxiosInstance {
|
export interface AxiosStatic extends AxiosInstance {
|
||||||
|
|||||||
@@ -97,6 +97,45 @@ axios.patch('/user', { foo: 'bar' })
|
|||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
|
// Typed methods
|
||||||
|
interface User {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleUserResponse = (response: AxiosResponse<User>) => {
|
||||||
|
console.log(response.data.id);
|
||||||
|
console.log(response.data.name);
|
||||||
|
console.log(response.status);
|
||||||
|
console.log(response.statusText);
|
||||||
|
console.log(response.headers);
|
||||||
|
console.log(response.config);
|
||||||
|
};
|
||||||
|
|
||||||
|
axios.get<User>('/user?id=12345')
|
||||||
|
.then(handleUserResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
|
||||||
|
axios.get<User>('/user', { params: { id: 12345 } })
|
||||||
|
.then(handleUserResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
|
||||||
|
axios.post<User>('/user', { foo: 'bar' })
|
||||||
|
.then(handleUserResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
|
||||||
|
axios.post<User>('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
|
||||||
|
.then(handleUserResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
|
||||||
|
axios.put<User>('/user', { foo: 'bar' })
|
||||||
|
.then(handleUserResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
|
||||||
|
axios.patch<User>('/user', { foo: 'bar' })
|
||||||
|
.then(handleUserResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
|
||||||
// Instances
|
// Instances
|
||||||
|
|
||||||
const instance1: AxiosInstance = axios.create();
|
const instance1: AxiosInstance = axios.create();
|
||||||
|
|||||||
Reference in New Issue
Block a user