Skip to content

Commit

Permalink
Changed response to json
Browse files Browse the repository at this point in the history
  • Loading branch information
cultpodcasts committed Feb 20, 2024
1 parent b1e270a commit a697ddc
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,22 @@ export default {
let url: URL | undefined;
let urlParam = data.url;
if (urlParam == null) {
return new Response("Missing url param.", { headers, status: 400 });
return new Response(JSON.stringify({ error: "Missing url param."}), { headers, status: 400 });
}
try {
url = new URL(urlParam);
} catch {
return new Response(`Invalid url '${data.url}'.`, { headers, status: 400 });
return new Response(JSON.stringify({error: `Invalid url '${data.url}'.`}), { headers, status: 400 });
}
let insert = env.DB
.prepare("INSERT INTO urls (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-IPCountry"), request.headers.get("User-Agent"));
let result = await insert.run();

if (result.success) {
return new Response("Submitted", { headers });
return new Response(JSON.stringify({ success: "Submitted" }), { headers });
} else {
return new Response("Unable to accept", { headers, status: 400 });
return new Response(JSON.stringify({ error: "Unable to accept" }), { headers, status: 400 });
}
});
} else if (request.method === "GET") {
Expand Down

0 comments on commit a697ddc

Please sign in to comment.