Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export interface AxiosTransformer {
}

export interface AxiosAdapter {
(config: AxiosRequestConfig): AxiosPromise;
(config: AxiosRequestConfig): AxiosPromise<any>;
}

export interface AxiosBasicCredentials {
Expand Down Expand Up @@ -44,8 +44,8 @@ export interface AxiosRequestConfig {
cancelToken?: CancelToken;
}

export interface AxiosResponse {
data: any;
export interface AxiosResponse<T = any> {
data: T;
status: number;
statusText: string;
headers: any;
Expand All @@ -59,7 +59,7 @@ export interface AxiosError extends Error {
response?: AxiosResponse;
}

export interface AxiosPromise extends Promise<AxiosResponse> {
export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
}

export interface CancelStatic {
Expand Down Expand Up @@ -101,13 +101,13 @@ export interface AxiosInstance {
request: AxiosInterceptorManager<AxiosRequestConfig>;
response: AxiosInterceptorManager<AxiosResponse>;
};
request(config: AxiosRequestConfig): AxiosPromise;
get(url: string, config?: AxiosRequestConfig): AxiosPromise;
request<T = any>(config: AxiosRequestConfig): AxiosPromise<T>;
get<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
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<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
Comment on lines +104 to +110
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

delete and head methods are missing generic type parameters.

The delete and head methods on lines 106-107 have not been updated with generic type parameters, unlike get, post, put, and patch. This creates an inconsistency where users cannot specify response types for these methods.

🔧 Proposed fix to add generics to delete and head
   request<T = any>(config: AxiosRequestConfig): AxiosPromise<T>;
   get<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
-  delete(url: string, config?: AxiosRequestConfig): AxiosPromise;
-  head(url: string, config?: AxiosRequestConfig): AxiosPromise;
+  delete<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
+  head<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
   post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
   put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
   patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
request<T = any>(config: AxiosRequestConfig): AxiosPromise<T>;
get<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
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<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
request<T = any>(config: AxiosRequestConfig): AxiosPromise<T>;
get<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
delete<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
head<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
🤖 Prompt for AI Agents
In `@index.d.ts` around lines 104 - 110, The delete and head method signatures in
index.d.ts lack generic type parameters; update the methods named delete and
head to accept a generic type parameter (e.g., <T = any>) and make their return
types AxiosPromise<T>, matching the pattern used by get/post/put/patch so
callers can specify the response type; modify the signatures for the delete and
head functions (the ones currently declared as delete(url: string, config?:
AxiosRequestConfig): AxiosPromise and head(url: string, config?:
AxiosRequestConfig): AxiosPromise) to include the generic and return
AxiosPromise<T>.

}

export interface AxiosStatic extends AxiosInstance {
Expand Down
39 changes: 39 additions & 0 deletions test/typescript/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<User>) => {
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>('/user?id=12345')
.then(handleUserResponse)
.catch(handleError);

axios.get<User>('/user', { params: { id: 12345 } })
.then(handleUserResponse)
.catch(handleError);

axios.post<User>('/user', { foo: 'bar' })
.then(handleUserResponse)
.catch(handleError);

axios.post<User>('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
.then(handleUserResponse)
.catch(handleError);

axios.put<User>('/user', { foo: 'bar' })
.then(handleUserResponse)
.catch(handleError);

axios.patch<User>('/user', { foo: 'bar' })
.then(handleUserResponse)
.catch(handleError);

// Instances

const instance1: AxiosInstance = axios.create();
Expand Down