|
| 1 | +import axios, {AxiosResponse } from 'axios'; |
| 2 | +import RequestMethodEnum from '../constants/RequestMethodEnum'; |
| 3 | + |
| 4 | +// http://httpstat.us |
| 5 | +export default class HttpUtility { |
| 6 | + |
| 7 | + public async get(endpoint: string): Promise<AxiosResponse<any>> { |
| 8 | + const request = new Request(endpoint, { |
| 9 | + method: RequestMethodEnum.Get, |
| 10 | + }); |
| 11 | + |
| 12 | + return this._fetch(request); |
| 13 | + } |
| 14 | + |
| 15 | + // TODO: finish setting up |
| 16 | + public async post(endpoint: string): Promise<AxiosResponse<any>> { |
| 17 | + const request = new Request(endpoint, { |
| 18 | + method: RequestMethodEnum.Post, |
| 19 | + }); |
| 20 | + |
| 21 | + return this._fetch(request); |
| 22 | + } |
| 23 | + |
| 24 | + // TODO: finish setting up |
| 25 | + public async put(endpoint: string): Promise<AxiosResponse<any>> { |
| 26 | + const request = new Request(endpoint, { |
| 27 | + method: RequestMethodEnum.Put, |
| 28 | + }); |
| 29 | + |
| 30 | + return this._fetch(request); |
| 31 | + } |
| 32 | + |
| 33 | + // TODO: finish setting up |
| 34 | + public async delete(endpoint: string): Promise<AxiosResponse<any>> { |
| 35 | + const request = new Request(endpoint, { |
| 36 | + method: RequestMethodEnum.Delete, |
| 37 | + }); |
| 38 | + |
| 39 | + return this._fetch(request); |
| 40 | + } |
| 41 | + |
| 42 | + private async _fetch(request: Request, init?: any): Promise<AxiosResponse<any>> { |
| 43 | + try { |
| 44 | + return await axios({ |
| 45 | + data: init, |
| 46 | + method: request.method, |
| 47 | + url: request.url, |
| 48 | + }); |
| 49 | + } catch (error) { |
| 50 | + console.error(`error`, error); |
| 51 | + |
| 52 | + return error; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments