Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to add types for data object in HttpService #1028

Closed
1 task done
kpervin opened this issue Feb 22, 2024 · 1 comment
Closed
1 task done

Add ability to add types for data object in HttpService #1028

kpervin opened this issue Feb 22, 2024 · 1 comment

Comments

@kpervin
Copy link

kpervin commented Feb 22, 2024

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

Currently all methods of the HttpService take a <T = any> generic parameter, but type data?; any. It would be nice to enforce types on the data object that gets passed to either AxiosRequestConfig or the data parameter, respectively. This also allow it to be compliant with the types that Axios themselves have in their library.

Describe the solution you'd like

Retype the HttpService as follows:

import { AxiosInstance, AxiosPromise, AxiosRequestConfig, AxiosResponse } from "axios";
import { Observable } from "rxjs";

export declare class HttpService {
  protected readonly instance: AxiosInstance;
  constructor(instance?: AxiosInstance);
  request<T = any, D = any>(config: AxiosRequestConfig<D>): Observable<AxiosResponse<T>>;

  get<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Observable<AxiosResponse<T>>;

  delete<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Observable<AxiosResponse<T>>;

  head<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Observable<AxiosResponse<T>>;

  post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Observable<AxiosResponse<T>>;
  post<T extends any, D extends any>(url: string, data: D, config?: AxiosRequestConfig<D>): Observable<AxiosResponse<T>>;

  put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Observable<AxiosResponse<T>>;
  put<T extends any, D extends any>(url: string, data: D, config?: AxiosRequestConfig<D>): Observable<AxiosResponse<T>>;

  patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Observable<AxiosResponse<T>>;
  patch<T extends any, D extends any>(url: string, data: D, config?: AxiosRequestConfig<D>): Observable<AxiosResponse<T>>;

  get axiosRef(): AxiosInstance;
  protected makeObservable<T>(axios: (...args: any[]) => AxiosPromise<T>, ...args: any[]): Observable<AxiosResponse<T, any>>;
}

This way with overloading, if we provide the type then data becomes required.

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

Just allowing the option to enforce stricter typing.

@kamilmysliwiec
Copy link
Member

Let's track this here #1029

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants