Untitled
unknown
plain_text
a year ago
3.4 kB
9
Indexable
import { cookies } from "next/headers";
const myCookie = cookies()
.getAll()
.map(c => c.name + "=" + c.value)
.join("; ");
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",
Cookie: myCookie
},
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;
Editor is loading...
Leave a Comment