From b0dccc6f1696446c79af54b306151c384afc5d51 Mon Sep 17 00:00:00 2001 From: Daniel Fischer Date: Tue, 29 Aug 2017 20:53:49 +0200 Subject: [PATCH] Updated TypeScript typings with generic type parameters for request methods & AxiosResponse --- index.d.ts | 18 +++++++++--------- test/typescript/axios.ts | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/index.d.ts b/index.d.ts index 188895d2cd..e519d10e7c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,7 +3,7 @@ export interface AxiosTransformer { } export interface AxiosAdapter { - (config: AxiosRequestConfig): AxiosPromise; + (config: AxiosRequestConfig): AxiosPromise; } export interface AxiosBasicCredentials { @@ -44,8 +44,8 @@ export interface AxiosRequestConfig { cancelToken?: CancelToken; } -export interface AxiosResponse { - data: any; +export interface AxiosResponse { + data: T; status: number; statusText: string; headers: any; @@ -59,7 +59,7 @@ export interface AxiosError extends Error { response?: AxiosResponse; } -export interface AxiosPromise extends Promise { +export interface AxiosPromise extends Promise> { } export interface CancelStatic { @@ -101,13 +101,13 @@ export interface AxiosInstance { request: AxiosInterceptorManager; response: AxiosInterceptorManager; }; - request(config: AxiosRequestConfig): AxiosPromise; - get(url: string, config?: AxiosRequestConfig): AxiosPromise; + 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; + post(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise; + put(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise; + patch(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise; } export interface AxiosStatic extends AxiosInstance { diff --git a/test/typescript/axios.ts b/test/typescript/axios.ts index c5492d8470..631334eff6 100644 --- a/test/typescript/axios.ts +++ b/test/typescript/axios.ts @@ -97,6 +97,45 @@ axios.patch('/user', { foo: 'bar' }) .then(handleResponse) .catch(handleError); +// Typed methods +interface User { + id: number; + name: string; +} + +const handleUserResponse = (response: AxiosResponse) => { + console.log(response.data.id); + console.log(response.data.name); + console.log(response.status); + console.log(response.statusText); + console.log(response.headers); + console.log(response.config); +}; + +axios.get('/user?id=12345') + .then(handleUserResponse) + .catch(handleError); + +axios.get('/user', { params: { id: 12345 } }) + .then(handleUserResponse) + .catch(handleError); + +axios.post('/user', { foo: 'bar' }) + .then(handleUserResponse) + .catch(handleError); + +axios.post('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } }) + .then(handleUserResponse) + .catch(handleError); + +axios.put('/user', { foo: 'bar' }) + .then(handleUserResponse) + .catch(handleError); + +axios.patch('/user', { foo: 'bar' }) + .then(handleUserResponse) + .catch(handleError); + // Instances const instance1: AxiosInstance = axios.create();