From 4eeb3b17e28581e6931ad7b78dcc025cf3f99bc8 Mon Sep 17 00:00:00 2001 From: Carlos Chida Date: Sun, 5 Sep 2021 07:06:05 -0500 Subject: [PATCH] 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 * Fixed tests - TS tests now match the behaviour described in the PR Signed-off-by: Carlos Chida Co-authored-by: Jay --- index.d.ts | 22 +++++++++++----------- test/typescript/axios.ts | 22 +++++++++++++--------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/index.d.ts b/index.d.ts index e5d6b66..3c0cb3d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -47,7 +47,7 @@ export interface TransitionalOptions{ clarifyTimeoutError: boolean; } -export interface AxiosRequestConfig { +export interface AxiosRequestConfig { url?: string; method?: Method; baseURL?: string; @@ -56,7 +56,7 @@ export interface AxiosRequestConfig { headers?: any; params?: any; paramsSerializer?: (params: any) => string; - data?: any; + data?: T; timeout?: number; timeoutErrorMessage?: string; withCredentials?: boolean; @@ -85,7 +85,7 @@ export interface AxiosResponse { status: number; statusText: string; headers: any; - config: AxiosRequestConfig; + config: AxiosRequestConfig; request?: any; } @@ -142,14 +142,14 @@ export class Axios { response: AxiosInterceptorManager; }; getUri(config?: AxiosRequestConfig): string; - request> (config: AxiosRequestConfig): Promise; - get>(url: string, config?: AxiosRequestConfig): Promise; - delete>(url: string, config?: AxiosRequestConfig): Promise; - head>(url: string, config?: AxiosRequestConfig): Promise; - options>(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; + request> (config: AxiosRequestConfig): Promise; + get>(url: string, config?: AxiosRequestConfig): Promise; + delete>(url: string, config?: AxiosRequestConfig): Promise; + head>(url: string, config?: AxiosRequestConfig): Promise; + options>(url: string, config?: AxiosRequestConfig): Promise; + post>(url: string, data?: T, config?: AxiosRequestConfig): Promise; + put>(url: string, data?: T, config?: AxiosRequestConfig): Promise; + patch>(url: string, data?: T, config?: AxiosRequestConfig): Promise; } export interface AxiosInstance extends Axios { diff --git a/test/typescript/axios.ts b/test/typescript/axios.ts index 1a1b6a7..97a8ad3 100644 --- a/test/typescript/axios.ts +++ b/test/typescript/axios.ts @@ -111,6 +111,10 @@ axios.patch('/user', { foo: 'bar' }) .catch(handleError); // Typed methods +interface UserCreationDef { + name: string; +} + interface User { id: number; name: string; @@ -138,7 +142,7 @@ axios.get('/user', { params: { id: 12345 } }) axios.head('/user') .then(handleUserResponse) .catch(handleError); - + axios.options('/user') .then(handleUserResponse) .catch(handleError); @@ -147,19 +151,19 @@ axios.delete('/user') .then(handleUserResponse) .catch(handleError); -axios.post('/user', { foo: 'bar' }) +axios.post('/user', { name: 'foo', id: 1 }) .then(handleUserResponse) .catch(handleError); -axios.post('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } }) +axios.post('/user', { name: 'foo', id: 1 }, { headers: { 'X-FOO': 'bar' } }) .then(handleUserResponse) .catch(handleError); -axios.put('/user', { foo: 'bar' }) +axios.put('/user', { name: 'foo', id: 1 }) .then(handleUserResponse) .catch(handleError); -axios.patch('/user', { foo: 'bar' }) +axios.patch('/user', { name: 'foo', id: 1 }) .then(handleUserResponse) .catch(handleError); @@ -189,19 +193,19 @@ axios.delete('/user') .then(handleStringResponse) .catch(handleError); -axios.post('/user', { foo: 'bar' }) +axios.post, string>('/user', { name: 'foo' }) .then(handleStringResponse) .catch(handleError); -axios.post('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } }) +axios.post, string>('/user', { name: 'foo' }, { headers: { 'X-FOO': 'bar' } }) .then(handleStringResponse) .catch(handleError); -axios.put('/user', { foo: 'bar' }) +axios.put, string>('/user', { name: 'foo' }) .then(handleStringResponse) .catch(handleError); -axios.patch('/user', { foo: 'bar' }) +axios.patch, string>('/user', { name: 'foo' }) .then(handleStringResponse) .catch(handleError);