Skip to content

Commit

Permalink
wrapping-react-components: post added to IHttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
dsibinski committed Feb 19, 2023
1 parent e4b4711 commit 200f31b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion wrapping-react-components/src/wrappers/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios, { AxiosError, AxiosInstance, AxiosResponse } from "axios";

interface IHttpClient {
get<TResponse>(url: string): Promise<TResponse>;
post<TResponse>(url: string, data?: object): Promise<TResponse>;
}

class AxiosHttpClient implements IHttpClient {
Expand All @@ -11,7 +12,7 @@ class AxiosHttpClient implements IHttpClient {
return this.instance ?? this.initAxiosClient();
}

initAxiosClient() {
private initAxiosClient() {
return axios.create();
}

Expand All @@ -27,6 +28,19 @@ class AxiosHttpClient implements IHttpClient {
});
});
}

post<TResponse>(url: string, data?: object): Promise<TResponse> {
return new Promise<TResponse>((resolve, reject) => {
this.axiosClient
.post<TResponse, AxiosResponse<TResponse>>(url, data)
.then((result) => {
resolve(result.data);
})
.catch((error: Error | AxiosError) => {
reject(error);
});
});
}
}

export const httpClient: IHttpClient = new AxiosHttpClient();

0 comments on commit 200f31b

Please sign in to comment.