| 
 | 1 | +import axios, { AxiosRequestConfig, AxiosResponse } from "axios";  | 
 | 2 | + | 
 | 3 | +type RequestOptions = {  | 
 | 4 | +  url: string;  | 
 | 5 | +  headers?: Record<string, string>;  | 
 | 6 | +  params?: Record<string, string | number>;  | 
 | 7 | +  body?: any;  | 
 | 8 | +  raise_error?: boolean; // default: true  | 
 | 9 | +};  | 
 | 10 | + | 
 | 11 | +class ApiResponse<T = any> {  | 
 | 12 | +  private _response: AxiosResponse<T>;  | 
 | 13 | + | 
 | 14 | +  constructor(response: AxiosResponse<T>) {  | 
 | 15 | +    this._response = response;  | 
 | 16 | +  }  | 
 | 17 | + | 
 | 18 | +  get data(): T {  | 
 | 19 | +    return this._response.data;  | 
 | 20 | +  }  | 
 | 21 | + | 
 | 22 | +  get status(): number {  | 
 | 23 | +    return this._response.status;  | 
 | 24 | +  }  | 
 | 25 | + | 
 | 26 | +  get statusText(): string {  | 
 | 27 | +    return this._response.statusText;  | 
 | 28 | +  }  | 
 | 29 | + | 
 | 30 | +  get headers(): Record<string, string> {  | 
 | 31 | +    const raw = this._response.headers;  | 
 | 32 | +    const sanitized: Record<string, string> = {};  | 
 | 33 | + | 
 | 34 | +    for (const key in raw) {  | 
 | 35 | +      const value = raw[key];  | 
 | 36 | +      if (typeof value === "string") {  | 
 | 37 | +        sanitized[key] = value;  | 
 | 38 | +      }  | 
 | 39 | +    }  | 
 | 40 | + | 
 | 41 | +    return sanitized;  | 
 | 42 | +  }  | 
 | 43 | + | 
 | 44 | +  get config(): AxiosRequestConfig {  | 
 | 45 | +    return this._response.config;  | 
 | 46 | +  }  | 
 | 47 | + | 
 | 48 | +  get url(): string | undefined {  | 
 | 49 | +    return this._response.config.url;  | 
 | 50 | +  }  | 
 | 51 | + | 
 | 52 | +  get ok(): boolean {  | 
 | 53 | +    return this._response.status >= 200 && this._response.status < 300;  | 
 | 54 | +  }  | 
 | 55 | +}  | 
 | 56 | + | 
 | 57 | +class ApiClient {  | 
 | 58 | +  private instance = axios.create();  | 
 | 59 | + | 
 | 60 | +  private async requestWrapper<T>(  | 
 | 61 | +    fn: () => Promise<AxiosResponse<T>>,  | 
 | 62 | +    raise_error: boolean = true,  | 
 | 63 | +  ): Promise<ApiResponse<T>> {  | 
 | 64 | +    try {  | 
 | 65 | +      const res = await fn();  | 
 | 66 | +      return new ApiResponse<T>(res);  | 
 | 67 | +    } catch (error: any) {  | 
 | 68 | +      if (error.response && !raise_error) {  | 
 | 69 | +        return new ApiResponse<T>(error.response);  | 
 | 70 | +      }  | 
 | 71 | +      throw error;  | 
 | 72 | +    }  | 
 | 73 | +  }  | 
 | 74 | + | 
 | 75 | +  async get<T = any>({  | 
 | 76 | +    url,  | 
 | 77 | +    headers,  | 
 | 78 | +    params,  | 
 | 79 | +    raise_error = true,  | 
 | 80 | +  }: RequestOptions): Promise<ApiResponse<T>> {  | 
 | 81 | +    return this.requestWrapper<T>(  | 
 | 82 | +      () => this.instance.get<T>(url, { headers, params }),  | 
 | 83 | +      raise_error,  | 
 | 84 | +    );  | 
 | 85 | +  }  | 
 | 86 | + | 
 | 87 | +  async post<T = any>({  | 
 | 88 | +    url,  | 
 | 89 | +    headers,  | 
 | 90 | +    body,  | 
 | 91 | +    raise_error = true,  | 
 | 92 | +  }: RequestOptions): Promise<ApiResponse<T>> {  | 
 | 93 | +    return this.requestWrapper<T>(  | 
 | 94 | +      () => this.instance.post<T>(url, body, { headers }),  | 
 | 95 | +      raise_error,  | 
 | 96 | +    );  | 
 | 97 | +  }  | 
 | 98 | + | 
 | 99 | +  async put<T = any>({  | 
 | 100 | +    url,  | 
 | 101 | +    headers,  | 
 | 102 | +    body,  | 
 | 103 | +    raise_error = true,  | 
 | 104 | +  }: RequestOptions): Promise<ApiResponse<T>> {  | 
 | 105 | +    return this.requestWrapper<T>(  | 
 | 106 | +      () => this.instance.put<T>(url, body, { headers }),  | 
 | 107 | +      raise_error,  | 
 | 108 | +    );  | 
 | 109 | +  }  | 
 | 110 | + | 
 | 111 | +  async patch<T = any>({  | 
 | 112 | +    url,  | 
 | 113 | +    headers,  | 
 | 114 | +    body,  | 
 | 115 | +    raise_error = true,  | 
 | 116 | +  }: RequestOptions): Promise<ApiResponse<T>> {  | 
 | 117 | +    return this.requestWrapper<T>(  | 
 | 118 | +      () => this.instance.patch<T>(url, body, { headers }),  | 
 | 119 | +      raise_error,  | 
 | 120 | +    );  | 
 | 121 | +  }  | 
 | 122 | + | 
 | 123 | +  async delete<T = any>({  | 
 | 124 | +    url,  | 
 | 125 | +    headers,  | 
 | 126 | +    params,  | 
 | 127 | +    raise_error = true,  | 
 | 128 | +  }: RequestOptions): Promise<ApiResponse<T>> {  | 
 | 129 | +    return this.requestWrapper<T>(  | 
 | 130 | +      () => this.instance.delete<T>(url, { headers, params }),  | 
 | 131 | +      raise_error,  | 
 | 132 | +    );  | 
 | 133 | +  }  | 
 | 134 | +}  | 
 | 135 | + | 
 | 136 | +export const apiClient = new ApiClient();  | 
 | 137 | +export type { ApiResponse, RequestOptions };  | 
0 commit comments