|
1 | | -import axios, { AxiosRequestConfig } from "axios"; |
| 1 | +export class ApiError extends Error { |
| 2 | + response: Response; |
| 3 | + constructor( |
| 4 | + response: Response, |
| 5 | + message?: string | undefined, |
| 6 | + options?: ErrorOptions | undefined, |
| 7 | + ...other: any[] |
| 8 | + ) { |
| 9 | + super(message, options, ...other as []); |
| 10 | + this.response = response; |
| 11 | + } |
| 12 | +} |
2 | 13 |
|
3 | 14 | export class Api { |
4 | 15 | baseUrl: string; |
5 | 16 | constructor(baseUrl: string) { |
6 | 17 | this.baseUrl = baseUrl; |
7 | 18 | } |
8 | | - getAxiosConfig = (config: AxiosRequestConfig) => { |
| 19 | + getFetchConfig = (config: RequestInit) => { |
9 | 20 | const headers = { |
10 | 21 | 'Content-Type': 'application/json', |
11 | 22 | ...(config?.headers || {}) |
12 | 23 | } |
13 | | - const axiosConfig: AxiosRequestConfig = { |
| 24 | + const requestInit: RequestInit = { |
14 | 25 | ...config, |
15 | 26 | headers, |
16 | | - baseURL: this.baseUrl |
17 | 27 | } |
18 | | - return axiosConfig; |
| 28 | + return requestInit; |
19 | 29 | } |
20 | | - async post(endpoint: string, data: any, config: AxiosRequestConfig = {}) { |
21 | | - return axios.post(endpoint, data, this.getAxiosConfig(config)); |
| 30 | + async request(method: string = 'GET', endpoint: string, data: any, config: RequestInit) { |
| 31 | + const url = new URL(endpoint, this.baseUrl).toString(); |
| 32 | + const response = await fetch(url, { |
| 33 | + body: data ? JSON.stringify(data) : undefined, |
| 34 | + method, |
| 35 | + headers: { |
| 36 | + 'Content-Type': 'application/json' |
| 37 | + } |
| 38 | + }); |
| 39 | + if (!response.ok) { |
| 40 | + throw new ApiError(response, "Failed Api Request for "+endpoint); |
| 41 | + } |
| 42 | + const jsonData = await response.json(); |
| 43 | + return { data: jsonData }; |
| 44 | + } |
| 45 | + |
| 46 | + async post(endpoint: string, data: any, config: RequestInit = {}) { |
| 47 | + return this.request('POST', endpoint, data, this.getFetchConfig(config)); |
22 | 48 | } |
23 | | - async get(endpoint: string, config: AxiosRequestConfig = {}) { |
24 | | - return axios.get(endpoint, this.getAxiosConfig(config)); |
| 49 | + async get(endpoint: string, config: RequestInit = {}) { |
| 50 | + return this.request('GET', endpoint, null, this.getFetchConfig(config)); |
25 | 51 | } |
26 | | - async put(endpoint: string, data: any, config: AxiosRequestConfig = {}) { |
27 | | - return axios.put(endpoint, data, this.getAxiosConfig(config)); |
| 52 | + async put(endpoint: string, data: any, config: RequestInit = {}) { |
| 53 | + return this.request('PUT', endpoint, data, this.getFetchConfig(config)); |
28 | 54 | } |
29 | | - async delete(endpoint: string, config: AxiosRequestConfig = {}) { |
30 | | - return axios.delete(endpoint, this.getAxiosConfig(config)); |
| 55 | + async delete(endpoint: string, config: RequestInit = {}) { |
| 56 | + return this.request('DELETE', endpoint, null, this.getFetchConfig(config)); |
31 | 57 | } |
32 | 58 | } |
0 commit comments