Skip to content

Commit f8dae06

Browse files
committed
Return form submissions
1 parent ae77e36 commit f8dae06

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

contact-urgent.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: Urgent Contact
55

66
If you have an access token then you can send me an urgent message using this form:
77

8-
<form id="contact-form" method="post" action="/api/contact-urgent">
8+
<form id="contact-form" method="post" action="/api/form/contact-urgent">
99
<fieldset style="display:none">
1010
<label for="name">Leave blank if you're human:</label>
1111
<input type="text" name="name" id="name" placeholder="Leave blank if you're human">

contact.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: Contact
55

66
If you'd like to get in touch then you can contact me using the below form.
77

8-
<form id="contact-form" method="post" action="/api/contact">
8+
<form id="contact-form" method="post" action="/api/form/contact">
99
<fieldset style="display:none">
1010
<label for="name">Leave blank if you're human:</label>
1111
<input type="text" name="name" id="name" placeholder="Leave blank if you're human">

functions/api/contact-urgent.js renamed to functions/api/form/contact-urgent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { handleForm } from "../../functions-src/forms.js"
1+
import { handleForm } from "../../../functions-src/forms.js"
22

33
export async function onRequest(context) {
44
if (context.request.method !== "POST") {

functions/api/contact.js renamed to functions/api/form/contact.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { handleForm } from "../../functions-src/forms.js"
1+
import { handleForm } from "../../../functions-src/forms.js"
22

33
export async function onRequest(context) {
44
if (context.request.method !== "POST") {

functions/secure/api/form.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export async function onRequest(context) {
2+
if (context.request.method !== "GET") {
3+
return new Response("Invalid request method", { status: 405 });
4+
}
5+
6+
const submissionsQuery = await context.env.DB_FORMS.prepare("SELECT submission_id, form_id, submitted_ts, spam_reasons, json_extract(headers, '$.cf-connecting-ip') as ip, json_extract(cf, '$.asn') as asn, json_extract(cf, '$.country') as country FROM submissions WHERE submitted_ts > ?")
7+
.bind(
8+
new Date().subtractDays(30).toISOString(),
9+
)
10+
.all();
11+
const rows = submissionsQuery
12+
.results
13+
.map((row) => {
14+
row.submitted_ts = new Date(row.submitted_ts);
15+
row.spam_reasons = JSON.parse(row.spam_reasons);
16+
row.asn = row.asn.toString();
17+
return row;
18+
});
19+
20+
return Response.json(submissionsQuery.results);
21+
}
22+
23+
// TODO: this is bad practice, consider replacing (see https://www.reddit.com/r/learnjavascript/comments/qgtut6/comment/hi8jg6w/)
24+
Date.prototype.subtractDays = function(days) {
25+
var date = new Date(this.valueOf());
26+
date.setDate(date.getDate() - days);
27+
return date;
28+
}

0 commit comments

Comments
 (0)