Untitled

mail@pastecode.io avatar
unknown
plain_text
13 days ago
3.3 kB
1
Indexable
Never
import { revalidateTag } from "next/cache";

type Endpoints = "story" | "gallery" | "banner" | "days" | "review" | "faq" | "resource" | "about";
const tags = ["story", "gallery", "banner", "days", "review", "faq", "resource", "about"];
interface FetchDataResponse {
  story?: any;
  gallery?: any;
  banner?: any;
  days?: any;
  review?: any;
  faq?: any;
  resource?: any;
  about?: any;
}

const endpoints: Record<Endpoints, { url: string; revalidate: string }> = {
  banner: {
    url: `${process.env.NEXT_PUBLIC_SERVER_URL}/banner/all?paginate=false`,
    revalidate: "banner"
  },
  days: {
    url: `${process.env.NEXT_PUBLIC_SERVER_URL}/days?sortBy=day`,
    revalidate: "days"
  },
  story: {
    url: `${process.env.NEXT_PUBLIC_SERVER_URL}/story`,
    revalidate: "story"
  },
  gallery: {
    url: `${process.env.NEXT_PUBLIC_SERVER_URL}/gallery`,
    revalidate: "gallery"
  },
  review: {
    url: `${process.env.NEXT_PUBLIC_SERVER_URL}/review/all?paginate=false`,
    revalidate: "review"
  },
  faq: {
    url: `${process.env.NEXT_PUBLIC_SERVER_URL}/faq/all?paginate=false`,
    revalidate: "faq"
  },
  resource: {
    url: `${process.env.NEXT_PUBLIC_SERVER_URL}/resource`,
    revalidate: "resource"
  },
  about: {
    url: `${process.env.NEXT_PUBLIC_SERVER_URL}/about`,
    revalidate: "about"
  }
};

const fetchData = async (fetchSpecific: Endpoints[] = []): Promise<FetchDataResponse> => {
  try {
    // const endpoints: Record<Endpoints, string> = {
    //   banner: `${process.env.NEXT_PUBLIC_SERVER_URL}/banner/all?paginate=false`,
    //   days: `${process.env.NEXT_PUBLIC_SERVER_URL}/days?sortBy=day`,
    //   story: `${process.env.NEXT_PUBLIC_SERVER_URL}/story`,
    //   gallery: `${process.env.NEXT_PUBLIC_SERVER_URL}/gallery`,
    //   review: `${process.env.NEXT_PUBLIC_SERVER_URL}/review/all?paginate=false`,
    //   faq: `${process.env.NEXT_PUBLIC_SERVER_URL}/faq/all?paginate=false`,
    //   resource: `${process.env.NEXT_PUBLIC_SERVER_URL}/resource`,
    //   about: `${process.env.NEXT_PUBLIC_SERVER_URL}/about`
    // };

    const keysToFetch = fetchSpecific.length > 0 ? fetchSpecific : (Object.keys(endpoints) as Endpoints[]);
    const fetchPromises = keysToFetch.map(key =>
      fetch(endpoints[key].url, {
        method: "GET",
        credentials: "include",
        headers: {
          "Content-Type": "application/json"
        },
        next: { tags: [endpoints[key].revalidate] }
      })
    );
    const responses = await Promise.all(fetchPromises);
    responses.forEach(response => {
      if (!response.ok) {
        throw new Error(`Failed to fetch data from ${response.url}: ${response.statusText}`);
      }
    });
    const jsonData = await Promise.all(responses.map(response => response.json()));
    const fetchedData: FetchDataResponse = keysToFetch.reduce((acc, key, index) => {
      acc[key] = jsonData[index];
      return acc;
    }, {} as FetchDataResponse);
    return fetchedData;
  } catch (error) {
    console.error("Error fetching data:", error);
    return {
      story: [],
      gallery: [],
      banner: [],
      days: [],
      review: [],
      faq: [],
      resource: [],
      about: []
    };
  }
};

export default fetchData;
Leave a Comment