import { baseAPI } from "@/shared/api/baseAPI";
import { IList } from "@/shared/types/list-type";
import { INews } from "@/shared/types/news-type";
import axios, { AxiosError } from "axios";

interface IFetchNews extends IList {
  results: INews[];
}

export const getNews = async () => {
  try {
    const data = (await axios.get<IFetchNews>(`${baseAPI}/news/`))
      .data;

    return data.results.slice(0, 4);
  } catch (error: unknown) {
    if (error instanceof AxiosError) {
      alert(error.message);
    } else {
      alert("An error ocured");
    }
  }
};
