import { message } from 'ant-design-vue';
import HttpService from './axios';
import { AxiosRequestConfig } from 'axios';

const baseURL = (import.meta as ImportMeta & { env: any}).env.VITE_APP_BASE_URL as string;

type ResponseResult<T> = {
  code: number;
  isAuthorized: boolean;
  isSuccess: boolean;
  result: T;
  error?: string;
}
const transformResponse = <T>(response: any & ResponseResult<T>, config: AxiosRequestConfig) => {
  if (config.responseType === 'blob') {
    return response;
  }
  if(response.code === 1) {
    return response.result;
  } else {
    message.warning(response.error as string);
    throw new Error(response.error)
  }
};

export const http = new HttpService(baseURL, {transformResponse});