Skip to content

Commit 4eeb3b1

Browse files
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>
1 parent cd7ff04 commit 4eeb3b1

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

index.d.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface TransitionalOptions{
4747
clarifyTimeoutError: boolean;
4848
}
4949

50-
export interface AxiosRequestConfig {
50+
export interface AxiosRequestConfig<T = any> {
5151
url?: string;
5252
method?: Method;
5353
baseURL?: string;
@@ -56,7 +56,7 @@ export interface AxiosRequestConfig {
5656
headers?: any;
5757
params?: any;
5858
paramsSerializer?: (params: any) => string;
59-
data?: any;
59+
data?: T;
6060
timeout?: number;
6161
timeoutErrorMessage?: string;
6262
withCredentials?: boolean;
@@ -85,7 +85,7 @@ export interface AxiosResponse<T = any> {
8585
status: number;
8686
statusText: string;
8787
headers: any;
88-
config: AxiosRequestConfig;
88+
config: AxiosRequestConfig<T>;
8989
request?: any;
9090
}
9191

@@ -142,14 +142,14 @@ export class Axios {
142142
response: AxiosInterceptorManager<AxiosResponse>;
143143
};
144144
getUri(config?: AxiosRequestConfig): string;
145-
request<T = any, R = AxiosResponse<T>> (config: AxiosRequestConfig): Promise<R>;
146-
get<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
147-
delete<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
148-
head<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
149-
options<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
150-
post<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
151-
put<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
152-
patch<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
145+
request<T = any, R = AxiosResponse<T>> (config: AxiosRequestConfig<T>): Promise<R>;
146+
get<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
147+
delete<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
148+
head<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
149+
options<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
150+
post<T = any, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
151+
put<T = any, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
152+
patch<T = any, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
153153
}
154154

155155
export interface AxiosInstance extends Axios {

test/typescript/axios.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ axios.patch('/user', { foo: 'bar' })
111111
.catch(handleError);
112112

113113
// Typed methods
114+
interface UserCreationDef {
115+
name: string;
116+
}
117+
114118
interface User {
115119
id: number;
116120
name: string;
@@ -138,7 +142,7 @@ axios.get<User>('/user', { params: { id: 12345 } })
138142
axios.head<User>('/user')
139143
.then(handleUserResponse)
140144
.catch(handleError);
141-
145+
142146
axios.options<User>('/user')
143147
.then(handleUserResponse)
144148
.catch(handleError);
@@ -147,19 +151,19 @@ axios.delete<User>('/user')
147151
.then(handleUserResponse)
148152
.catch(handleError);
149153

150-
axios.post<User>('/user', { foo: 'bar' })
154+
axios.post<User>('/user', { name: 'foo', id: 1 })
151155
.then(handleUserResponse)
152156
.catch(handleError);
153157

154-
axios.post<User>('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
158+
axios.post<User>('/user', { name: 'foo', id: 1 }, { headers: { 'X-FOO': 'bar' } })
155159
.then(handleUserResponse)
156160
.catch(handleError);
157161

158-
axios.put<User>('/user', { foo: 'bar' })
162+
axios.put<User>('/user', { name: 'foo', id: 1 })
159163
.then(handleUserResponse)
160164
.catch(handleError);
161165

162-
axios.patch<User>('/user', { foo: 'bar' })
166+
axios.patch<User>('/user', { name: 'foo', id: 1 })
163167
.then(handleUserResponse)
164168
.catch(handleError);
165169

@@ -189,19 +193,19 @@ axios.delete<User, string>('/user')
189193
.then(handleStringResponse)
190194
.catch(handleError);
191195

192-
axios.post<User, string>('/user', { foo: 'bar' })
196+
axios.post<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
193197
.then(handleStringResponse)
194198
.catch(handleError);
195199

196-
axios.post<User, string>('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
200+
axios.post<Partial<UserCreationDef>, string>('/user', { name: 'foo' }, { headers: { 'X-FOO': 'bar' } })
197201
.then(handleStringResponse)
198202
.catch(handleError);
199203

200-
axios.put<User, string>('/user', { foo: 'bar' })
204+
axios.put<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
201205
.then(handleStringResponse)
202206
.catch(handleError);
203207

204-
axios.patch<User, string>('/user', { foo: 'bar' })
208+
axios.patch<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
205209
.then(handleStringResponse)
206210
.catch(handleError);
207211

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy