Skip to content

Instantly share code, notes, and snippets.

@muhammadfaizan
Created February 24, 2022 08:19
Show Gist options
  • Save muhammadfaizan/e219ae4bc415b0f761f9e5bd718658cf to your computer and use it in GitHub Desktop.
Save muhammadfaizan/e219ae4bc415b0f761f9e5bd718658cf to your computer and use it in GitHub Desktop.
Base Class Client Respository
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
export class ResourcesRestClient {
private instance: AxiosInstance
private config?: AxiosRequestConfig
constructor(client: AxiosInstance, config?: AxiosRequestConfig) {
this.instance = client
this.config = config
}
public post<RequestType>(url: string) {
return (req: RequestType, config?: AxiosRequestConfig): Promise<AxiosResponse<ResponseType>> =>
this.instance.post<RequestType, AxiosResponse<ResponseType>>(url, req, config)
}
public get<RequestType>(url: string) {
return (req: RequestType, config?: AxiosRequestConfig): Promise<AxiosResponse<ResponseType>> =>
this.instance.get<RequestType, AxiosResponse<ResponseType>>(url, config || this.config)
}
public delete<RequestType, ResponseType>(url: string) {
return (req: RequestType, config?: AxiosRequestConfig): Promise<AxiosResponse<ResponseType>> =>
this.instance.post<RequestType, AxiosResponse<ResponseType>>(url, req, config)
}
public patch<RequestType, ResponseType>(url: string) {
return (req: RequestType, config?: AxiosRequestConfig): Promise<AxiosResponse<ResponseType>> =>
this.instance.post<RequestType, AxiosResponse<ResponseType>>(url, req, config)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment