Skip to content

Commit

Permalink
Use json post body
Browse files Browse the repository at this point in the history
  • Loading branch information
cultpodcasts committed Feb 17, 2024
1 parent 39c905f commit 3b394c9
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,30 @@ export default {
}

if (pathname.startsWith(submitRoute) && request.method === "POST") {
let url: URL | undefined;
let urlParam = searchParams.get("url");
if (urlParam == null) {
return new Error("Missing url param.");
}
try {
url = new URL(urlParam);
} catch {
return new Error(`Invalid url '${url}'.`);
}
let insert = env.DB
.prepare("INSERT INTO submissions (url, timestamp, timestamp_date, ip_address, country, user_agent) VALUES (?, ?, ?, ?, ?)")
.bind(url.toString(), Date.now(), new Date().toLocaleString(), request.headers.get("CF-Connecting-IP"), request.headers.get("CF-Connecting-IP"), request.headers.get("User-Agent"));
let result = await insert.run();

if (result.success) {
return new Response();
} else {
return new Error();
}
return request
.json()
.then(async (data: any) => {
let url: URL | undefined;
let urlParam = data.url;
if (urlParam == null) {
return new Error("Missing url param.");
}
try {
url = new URL(urlParam);
} catch {
return new Error(`Invalid url '${url}'.`);
}
let insert = env.DB
.prepare("INSERT INTO submissions (url, timestamp, timestamp_date, ip_address, country, user_agent) VALUES (?, ?, ?, ?, ?)")
.bind(url.toString(), Date.now(), new Date().toLocaleString(), request.headers.get("CF-Connecting-IP"), request.headers.get("CF-Connecting-IP"), request.headers.get("User-Agent"));
let result = await insert.run();

if (result.success) {
return new Response();
} else {
return new Error();
}
});
}

return new Response(
Expand Down

0 comments on commit 3b394c9

Please sign in to comment.