import { AxiosPromise } from 'axios';
import { NiceAxiosError } from '@/shared/@types/error';

export const resultify = async <T = any>(promiseObj: AxiosPromise<T>): Promise<T> => {
  try {
    const result: any = await promiseObj;
    return result?.data !== undefined && result?.data !== null ? result.data : result;
  } catch (error: any) {
    const axiosError = error as NiceAxiosError;
    console.error(
      `${
        axiosError.response?.status ? `[${axiosError.response.status}], ` : ''
      }${JSON.stringify(axiosError.response?.data)}`,
    );
    throw error;
  }
};
