Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
export default async function(req: Request): Promise<Response> {
const url =
"https://script.google.com/macros/s/AKfycbwzkp9Nr29pws2ShKq9ZlI1W-DaLmKBJbGBk8kYuw4bOkmyWvgdE7E6M5d6YwUITekf/exec";
try {
// Fetch the data from the Google Script URL
const response = await fetch(url);
const jsonData = await response.json();
// Extract the array inside the "data" field
const dataArray = jsonData.data;
// Extract the email parameter from the query string
const email = new URL(req.url).searchParams.get("email");
if (!email) {
// Create a string by concatenating each object JSON string
const result = dataArray
.map((item: { date: string; email: string }) => JSON.stringify(item))
.join("");
// Return the concatenated string
return new Response(result, {
headers: { "Content-Type": "application/json" },
});
}
// Find the object in the array that matches the provided email
const record = dataArray.find((item: { email: string }) => item.email === email);
if (!record) {
return new Response("No record found for the provided email", { status: 404 });
}
// Return the date associated with the email
return new Response(JSON.stringify({ date: record.date }), {
headers: { "Content-Type": "application/json" },
});
} catch (error) {
return new Response("Failed to fetch data", { status: 500 });
}
}
fahmi-accessiblescarlettoad.web.val.run
August 10, 2024