mirror of
https://github.com/tenrok/axios.git
synced 2026-06-11 18:02:32 +03:00
Updated TypeScript typings with generic type parameters for request methods & AxiosResponse
This commit is contained in:
@@ -97,6 +97,45 @@ axios.patch('/user', { foo: 'bar' })
|
||||
.then(handleResponse)
|
||||
.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
|
||||
|
||||
const instance1: AxiosInstance = axios.create();
|
||||
|
||||
Reference in New Issue
Block a user