@@ -103,6 +103,8 @@ interface User {
103
103
name : string ;
104
104
}
105
105
106
+ // with default AxiosResponse<T> result
107
+
106
108
const handleUserResponse = ( response : AxiosResponse < User > ) => {
107
109
console . log ( response . data . id ) ;
108
110
console . log ( response . data . name ) ;
@@ -121,11 +123,11 @@ axios.get<User>('/user', { params: { id: 12345 } })
121
123
. catch ( handleError ) ;
122
124
123
125
axios . head < User > ( '/user' )
124
- . then ( handleResponse )
126
+ . then ( handleUserResponse )
125
127
. catch ( handleError ) ;
126
128
127
129
axios . delete < User > ( '/user' )
128
- . then ( handleResponse )
130
+ . then ( handleUserResponse )
129
131
. catch ( handleError ) ;
130
132
131
133
axios . post < User > ( '/user' , { foo : 'bar' } )
@@ -142,7 +144,52 @@ axios.put<User>('/user', { foo: 'bar' })
142
144
143
145
axios . patch < User > ( '/user' , { foo : 'bar' } )
144
146
. 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 ) ;
146
193
147
194
// Instances
148
195
0 commit comments