export const quoteResponder = (request) => {
const quotes = [
{
date: "2023-10-19",
quote:
"Happiness does not come from doing easy work but from the afterglow of satisfaction that comes after the achievement of a difficult task that demanded our best.",
author: "Theodore Isaac Rubin",
},
{
date: "2023-10-20",
quote:
"Just because something doesn't do what you planned it to do doesn't mean it's useless.",
author: "Thomas A. Edison",
},
{
date: "2023-10-21",
quote:
"Success is not final; failure is not fatal: it is the courage to continue that counts.",
author: "Winston Churchill",
},
{
date: "2023-10-22",
quote: "Life is what happens when you’re busy making other plans.",
author: "John Lennon",
},
{
date: "2023-10-23",
quote:
"Excellence is an art won by training and habituation. We do not act rightly because we have virtue or excellence, but we rather have those because we have acted rightly. We are what we repeatedly do. Excellence, then, is not an act but a habit",
author: "Aristotle",
},
];
const queryParams = (req) => {
const searchParams = new URL(req.url).searchParams;
return Object.fromEntries(searchParams.entries());
};
const { date: requestedDate } = queryParams(request) ||
new Date().toISOString().split("T")[0];
const currentDate = new Date().toISOString().split("T")[0];
if (requestedDate > currentDate) {
return new Response(
JSON.stringify({
error: "Cannot retrieve quotes from the future.",
}),
{
headers: {
"Content-Type": "application/json",
},
},
);
}
const selectedQuote = quotes.find((q) => q.date === requestedDate);
if (selectedQuote) {
return new Response(JSON.stringify(selectedQuote), {
headers: {
"Content-Type": "application/json",
},
});
}
else {
return new Response(
JSON.stringify({
error: `No quote available for the date ${requestedDate}.`,
}),
{
headers: {
"Content-Type": "application/json",
},
},
);
}
};