Skip to content

Commit 73cab97

Browse files
zceiKhaledgarbaya
authored andcommitted
typings: allow custom return types
response interceptor might change the type from AxiosResponse to anything they like
1 parent 0b3db5d commit 73cab97

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

index.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ export interface AxiosInstance {
109109
request: AxiosInterceptorManager<AxiosRequestConfig>;
110110
response: AxiosInterceptorManager<AxiosResponse>;
111111
};
112-
request<T = any>(config: AxiosRequestConfig): AxiosPromise<T>;
113-
get<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
114-
delete<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
115-
head<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
116-
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
117-
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
118-
patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
112+
request<T = any, R = AxiosResponse<T>> (config: AxiosRequestConfig): Promise<R>;
113+
get<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
114+
delete<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
115+
head<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
116+
post<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
117+
put<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
118+
patch<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
119119
}
120120

121121
export interface AxiosStatic extends AxiosInstance {

test/typescript/axios.ts

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ interface User {
103103
name: string;
104104
}
105105

106+
// with default AxiosResponse<T> result
107+
106108
const handleUserResponse = (response: AxiosResponse<User>) => {
107109
console.log(response.data.id);
108110
console.log(response.data.name);
@@ -121,11 +123,11 @@ axios.get<User>('/user', { params: { id: 12345 } })
121123
.catch(handleError);
122124

123125
axios.head<User>('/user')
124-
.then(handleResponse)
126+
.then(handleUserResponse)
125127
.catch(handleError);
126128

127129
axios.delete<User>('/user')
128-
.then(handleResponse)
130+
.then(handleUserResponse)
129131
.catch(handleError);
130132

131133
axios.post<User>('/user', { foo: 'bar' })
@@ -142,7 +144,52 @@ axios.put<User>('/user', { foo: 'bar' })
142144

143145
axios.patch<User>('/user', { foo: 'bar' })
144146
.then(handleUserResponse)
145-
.catch(handleError);
147+
.catch(handleError);
148+
149+
// (Typed methods) with custom response type
150+
151+
const handleStringResponse = (response: string) => {
152+
console.log(response)
153+
}
154+
155+
axios.get<User, string>('/user?id=12345')
156+
.then(handleStringResponse)
157+
.catch(handleError);
158+
159+
axios.get<User, string>('/user', { params: { id: 12345 } })
160+
.then(handleStringResponse)
161+
.catch(handleError);
162+
163+
axios.head<User, string>('/user')
164+
.then(handleStringResponse)
165+
.catch(handleError);
166+
167+
axios.delete<User, string>('/user')
168+
.then(handleStringResponse)
169+
.catch(handleError);
170+
171+
axios.post<User, string>('/user', { foo: 'bar' })
172+
.then(handleStringResponse)
173+
.catch(handleError);
174+
175+
axios.post<User, string>('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
176+
.then(handleStringResponse)
177+
.catch(handleError);
178+
179+
axios.put<User, string>('/user', { foo: 'bar' })
180+
.then(handleStringResponse)
181+
.catch(handleError);
182+
183+
axios.patch<User, string>('/user', { foo: 'bar' })
184+
.then(handleStringResponse)
185+
.catch(handleError);
186+
187+
axios.request<User, string>({
188+
method: 'get',
189+
url: '/user?id=12345'
190+
})
191+
.then(handleStringResponse)
192+
.catch(handleError);
146193

147194
// Instances
148195

0 commit comments

Comments
 (0)