|
| 1 | +import axios, { AxiosInstance, AxiosRequestConfig } from "axios"; |
| 2 | +import log from "loglevel"; |
| 3 | +import { ApiResponse } from "./apiResponse"; |
| 4 | + |
| 5 | +const SERVER_HOST = process.env.API_HOST ?? ""; |
| 6 | +const REQUEST_TIMEOUT_MS = 20000; |
| 7 | +const API_REQUEST_HEADERS = { |
| 8 | + "Content-Type": "application/json", |
| 9 | +}; |
| 10 | + |
| 11 | +function apiFailureResponseInterceptor(error: any) { |
| 12 | + if (axios.isAxiosError(error)) { |
| 13 | + let message = `AxiosError:${error.code}`; |
| 14 | + const response = error.response; |
| 15 | + if (response) { |
| 16 | + message = message + ` status:${response.status}`; |
| 17 | + if (response.data) { |
| 18 | + const data = response.data as ApiResponse; |
| 19 | + message = message + ` code:${data.code} message:${data.message}`; |
| 20 | + } |
| 21 | + } else if (error.request) { |
| 22 | + log.error(error.request); |
| 23 | + } else { |
| 24 | + log.error("Error", error.message); |
| 25 | + } |
| 26 | + return Promise.reject(message); |
| 27 | + } |
| 28 | + return Promise.reject(error); |
| 29 | +} |
| 30 | + |
| 31 | +const axiosInstance: AxiosInstance = axios.create({ |
| 32 | + baseURL: `${SERVER_HOST}/api/`, |
| 33 | + timeout: REQUEST_TIMEOUT_MS, |
| 34 | + headers: API_REQUEST_HEADERS, |
| 35 | + withCredentials: true, |
| 36 | +}); |
| 37 | +axiosInstance.interceptors.request.use((c) => c); |
| 38 | +axiosInstance.interceptors.response.use((r) => r, apiFailureResponseInterceptor); |
| 39 | + |
| 40 | +class Api { |
| 41 | + static get(url: string, queryParams?: any, config: Partial<AxiosRequestConfig> = {}) { |
| 42 | + return axiosInstance.request({ |
| 43 | + url, |
| 44 | + method: "GET", |
| 45 | + params: queryParams, |
| 46 | + ...config, |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + static post( |
| 51 | + url: string, |
| 52 | + body?: any, |
| 53 | + queryParams?: any, |
| 54 | + config: Partial<AxiosRequestConfig> = {} |
| 55 | + ) { |
| 56 | + return axiosInstance.request({ |
| 57 | + method: "POST", |
| 58 | + url, |
| 59 | + data: body, |
| 60 | + params: queryParams, |
| 61 | + ...config, |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + static put(url: string, body?: any, queryParams?: any, config: Partial<AxiosRequestConfig> = {}) { |
| 66 | + return axiosInstance.request({ |
| 67 | + method: "PUT", |
| 68 | + url, |
| 69 | + params: queryParams, |
| 70 | + data: body, |
| 71 | + ...config, |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + static patch( |
| 76 | + url: string, |
| 77 | + body?: any, |
| 78 | + queryParams?: any, |
| 79 | + config: Partial<AxiosRequestConfig> = {} |
| 80 | + ) { |
| 81 | + return axiosInstance.request({ |
| 82 | + method: "PATCH", |
| 83 | + url, |
| 84 | + data: body, |
| 85 | + params: queryParams, |
| 86 | + ...config, |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + static delete(url: string, queryParams?: any, config: Partial<AxiosRequestConfig> = {}) { |
| 91 | + return axiosInstance.request({ |
| 92 | + method: "DELETE", |
| 93 | + url, |
| 94 | + params: queryParams, |
| 95 | + ...config, |
| 96 | + }); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; |
| 101 | + |
| 102 | +export default Api; |
0 commit comments