mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
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 <carlos.chida@starchitecture.eu> * Fixed tests - TS tests now match the behaviour described in the PR Signed-off-by: Carlos Chida <carlos.chida@starchitecture.eu> Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
Vendored
+11
-11
@@ -47,7 +47,7 @@ export interface TransitionalOptions{
|
|||||||
clarifyTimeoutError: boolean;
|
clarifyTimeoutError: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosRequestConfig {
|
export interface AxiosRequestConfig<T = any> {
|
||||||
url?: string;
|
url?: string;
|
||||||
method?: Method;
|
method?: Method;
|
||||||
baseURL?: string;
|
baseURL?: string;
|
||||||
@@ -56,7 +56,7 @@ export interface AxiosRequestConfig {
|
|||||||
headers?: any;
|
headers?: any;
|
||||||
params?: any;
|
params?: any;
|
||||||
paramsSerializer?: (params: any) => string;
|
paramsSerializer?: (params: any) => string;
|
||||||
data?: any;
|
data?: T;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
timeoutErrorMessage?: string;
|
timeoutErrorMessage?: string;
|
||||||
withCredentials?: boolean;
|
withCredentials?: boolean;
|
||||||
@@ -85,7 +85,7 @@ export interface AxiosResponse<T = any> {
|
|||||||
status: number;
|
status: number;
|
||||||
statusText: string;
|
statusText: string;
|
||||||
headers: any;
|
headers: any;
|
||||||
config: AxiosRequestConfig;
|
config: AxiosRequestConfig<T>;
|
||||||
request?: any;
|
request?: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,14 +142,14 @@ export class Axios {
|
|||||||
response: AxiosInterceptorManager<AxiosResponse>;
|
response: AxiosInterceptorManager<AxiosResponse>;
|
||||||
};
|
};
|
||||||
getUri(config?: AxiosRequestConfig): string;
|
getUri(config?: AxiosRequestConfig): string;
|
||||||
request<T = any, R = AxiosResponse<T>> (config: AxiosRequestConfig): Promise<R>;
|
request<T = any, R = AxiosResponse<T>> (config: AxiosRequestConfig<T>): Promise<R>;
|
||||||
get<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
|
get<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
|
||||||
delete<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
|
delete<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
|
||||||
head<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
|
head<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
|
||||||
options<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
|
options<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
|
||||||
post<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
|
post<T = any, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
|
||||||
put<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
|
put<T = any, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
|
||||||
patch<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
|
patch<T = any, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosInstance extends Axios {
|
export interface AxiosInstance extends Axios {
|
||||||
|
|||||||
@@ -111,6 +111,10 @@ axios.patch('/user', { foo: 'bar' })
|
|||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
// Typed methods
|
// Typed methods
|
||||||
|
interface UserCreationDef {
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface User {
|
interface User {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
@@ -138,7 +142,7 @@ axios.get<User>('/user', { params: { id: 12345 } })
|
|||||||
axios.head<User>('/user')
|
axios.head<User>('/user')
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.options<User>('/user')
|
axios.options<User>('/user')
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
@@ -147,19 +151,19 @@ axios.delete<User>('/user')
|
|||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.post<User>('/user', { foo: 'bar' })
|
axios.post<User>('/user', { name: 'foo', id: 1 })
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.post<User>('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
|
axios.post<User>('/user', { name: 'foo', id: 1 }, { headers: { 'X-FOO': 'bar' } })
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.put<User>('/user', { foo: 'bar' })
|
axios.put<User>('/user', { name: 'foo', id: 1 })
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.patch<User>('/user', { foo: 'bar' })
|
axios.patch<User>('/user', { name: 'foo', id: 1 })
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
@@ -189,19 +193,19 @@ axios.delete<User, string>('/user')
|
|||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.post<User, string>('/user', { foo: 'bar' })
|
axios.post<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.post<User, string>('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
|
axios.post<Partial<UserCreationDef>, string>('/user', { name: 'foo' }, { headers: { 'X-FOO': 'bar' } })
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.put<User, string>('/user', { foo: 'bar' })
|
axios.put<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.patch<User, string>('/user', { foo: 'bar' })
|
axios.patch<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user