import axios, { AxiosInstance } from "axios";
import type { requestConfig, requestInterceptors } from "@/api/type";
class Request {
  instance: AxiosInstance;
  interceptors?: requestInterceptors;

  constructor(config: requestConfig) {
    this.instance = axios.create(config);
    this.interceptors = config.interceptors;

    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptorCatch,
      this.interceptors?.requestInterceptor
    );
    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    );
  }

  request<T>(config: requestConfig): Promise<T> {
    return new Promise((resolve, reject) => {
      // if (config.interceptors?.requestInterceptor) {
      //   config = config.interceptors.requestInterceptor(config);
      // }
      this.instance.request(config).then((res) => {
        if (config.interceptors?.responseInterceptor) {
          res = config.interceptors.responseInterceptor(res);
        }
        console.log(res);
      });
    });
  }
}

export default Request;
