This repository was archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathhttp-client.ts
66 lines (55 loc) · 1.54 KB
/
http-client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
const headers: Readonly<Record<string, string | boolean>> = {
Accept: "application/json",
"Content-Type": "application/json; charset=utf-8",
"Access-Control-Allow-Credentials": true,
};
class HttpClient {
private instance: AxiosInstance | null = null;
private get http(): AxiosInstance {
return this.instance != null ? this.instance : this.initHttp();
}
get<T = any, R = AxiosResponse<T>>(
url: string,
config?: AxiosRequestConfig,
): Promise<R> {
return this.http.get<T, R>(url, config);
}
post<T = any, R = AxiosResponse<T>>(
url: string,
data?: T,
config?: AxiosRequestConfig,
): Promise<R> {
return this.http.post<T, R>(url, data, config);
}
put<T = any, R = AxiosResponse<T>>(
url: string,
data?: T,
config?: AxiosRequestConfig,
): Promise<R> {
return this.http.put<T, R>(url, data, config);
}
patch<T = any, R = AxiosResponse<T>>(
url: string,
data?: T,
config?: AxiosRequestConfig,
): Promise<R> {
return this.http.patch<T, R>(url, data, config);
}
delete<T = any, R = AxiosResponse<T>>(
url: string,
config?: AxiosRequestConfig,
): Promise<R> {
return this.http.delete<T, R>(url, config);
}
private initHttp() {
const http = axios.create({
baseURL:
"https://soroban-dapps-challenge-wrangler.julian-martinez.workers.dev",
headers,
});
this.instance = http;
return http;
}
}
export const httpClient = new HttpClient();