2
0
mirror of https://github.com/tenrok/axios.git synced 2026-05-15 11:59:42 +03:00

Improved type-safety for AxiosRequestConfig (#2995)

* Improved type-safety for AxiosRequestConfig

- AxiosRequestConfig is now a generic type whose template corresponds to data

Signed-off-by: Carlos Chida <carlos.chida@starchitecture.eu>

* Fixed tests

- TS tests now match the behaviour described in the PR

Signed-off-by: Carlos Chida <carlos.chida@starchitecture.eu>

Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
Carlos Chida
2021-09-05 07:06:05 -05:00
committed by GitHub
parent cd7ff042b0
commit 4eeb3b17e2
2 changed files with 24 additions and 20 deletions
+13 -9
View File
@@ -111,6 +111,10 @@ axios.patch('/user', { foo: 'bar' })
.catch(handleError);
// Typed methods
interface UserCreationDef {
name: string;
}
interface User {
id: number;
name: string;
@@ -138,7 +142,7 @@ axios.get<User>('/user', { params: { id: 12345 } })
axios.head<User>('/user')
.then(handleUserResponse)
.catch(handleError);
axios.options<User>('/user')
.then(handleUserResponse)
.catch(handleError);
@@ -147,19 +151,19 @@ axios.delete<User>('/user')
.then(handleUserResponse)
.catch(handleError);
axios.post<User>('/user', { foo: 'bar' })
axios.post<User>('/user', { name: 'foo', id: 1 })
.then(handleUserResponse)
.catch(handleError);
axios.post<User>('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
axios.post<User>('/user', { name: 'foo', id: 1 }, { headers: { 'X-FOO': 'bar' } })
.then(handleUserResponse)
.catch(handleError);
axios.put<User>('/user', { foo: 'bar' })
axios.put<User>('/user', { name: 'foo', id: 1 })
.then(handleUserResponse)
.catch(handleError);
axios.patch<User>('/user', { foo: 'bar' })
axios.patch<User>('/user', { name: 'foo', id: 1 })
.then(handleUserResponse)
.catch(handleError);
@@ -189,19 +193,19 @@ axios.delete<User, string>('/user')
.then(handleStringResponse)
.catch(handleError);
axios.post<User, string>('/user', { foo: 'bar' })
axios.post<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
.then(handleStringResponse)
.catch(handleError);
axios.post<User, string>('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
axios.post<Partial<UserCreationDef>, string>('/user', { name: 'foo' }, { headers: { 'X-FOO': 'bar' } })
.then(handleStringResponse)
.catch(handleError);
axios.put<User, string>('/user', { foo: 'bar' })
axios.put<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
.then(handleStringResponse)
.catch(handleError);
axios.patch<User, string>('/user', { foo: 'bar' })
axios.patch<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
.then(handleStringResponse)
.catch(handleError);