diff --git a/index.d.ts b/index.d.ts index 2ea4643a..ffd0e12f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -109,13 +109,13 @@ export interface AxiosInstance { request: AxiosInterceptorManager; response: AxiosInterceptorManager; }; - request(config: AxiosRequestConfig): AxiosPromise; - get(url: string, config?: AxiosRequestConfig): AxiosPromise; - delete(url: string, config?: AxiosRequestConfig): AxiosPromise; - head(url: string, config?: AxiosRequestConfig): AxiosPromise; - post(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise; - put(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise; - patch(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise; + request> (config: AxiosRequestConfig): Promise; + get>(url: string, config?: AxiosRequestConfig): Promise; + delete>(url: string, config?: AxiosRequestConfig): Promise; + head>(url: string, config?: AxiosRequestConfig): Promise; + post>(url: string, data?: any, config?: AxiosRequestConfig): Promise; + put>(url: string, data?: any, config?: AxiosRequestConfig): Promise; + patch>(url: string, data?: any, config?: AxiosRequestConfig): Promise; } export interface AxiosStatic extends AxiosInstance { diff --git a/test/typescript/axios.ts b/test/typescript/axios.ts index eb6e3fe6..fd87dafb 100644 --- a/test/typescript/axios.ts +++ b/test/typescript/axios.ts @@ -103,6 +103,8 @@ interface User { name: string; } +// with default AxiosResponse result + const handleUserResponse = (response: AxiosResponse) => { console.log(response.data.id); console.log(response.data.name); @@ -121,11 +123,11 @@ axios.get('/user', { params: { id: 12345 } }) .catch(handleError); axios.head('/user') - .then(handleResponse) + .then(handleUserResponse) .catch(handleError); axios.delete('/user') - .then(handleResponse) + .then(handleUserResponse) .catch(handleError); axios.post('/user', { foo: 'bar' }) @@ -142,7 +144,52 @@ axios.put('/user', { foo: 'bar' }) axios.patch('/user', { foo: 'bar' }) .then(handleUserResponse) - .catch(handleError); + .catch(handleError); + +// (Typed methods) with custom response type + +const handleStringResponse = (response: string) => { + console.log(response) +} + +axios.get('/user?id=12345') + .then(handleStringResponse) + .catch(handleError); + +axios.get('/user', { params: { id: 12345 } }) + .then(handleStringResponse) + .catch(handleError); + +axios.head('/user') + .then(handleStringResponse) + .catch(handleError); + +axios.delete('/user') + .then(handleStringResponse) + .catch(handleError); + +axios.post('/user', { foo: 'bar' }) + .then(handleStringResponse) + .catch(handleError); + +axios.post('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } }) + .then(handleStringResponse) + .catch(handleError); + +axios.put('/user', { foo: 'bar' }) + .then(handleStringResponse) + .catch(handleError); + +axios.patch('/user', { foo: 'bar' }) + .then(handleStringResponse) + .catch(handleError); + +axios.request({ + method: 'get', + url: '/user?id=12345' +}) + .then(handleStringResponse) + .catch(handleError); // Instances