mirror of
https://github.com/juherr/kill-the-news.git
synced 2026-06-20 22:03:48 +00:00
feat: accept JSON on POST /admin/feeds/create and return {feedId, email, feedUrl}
When Content-Type is application/json, parse the request body as JSON and return a JSON response instead of redirecting. Useful for automation tools (e.g. Terraform/OpenTofu provisioning). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+38
-9
@@ -1563,17 +1563,35 @@ app.post("/feeds/create", async (c) => {
|
|||||||
// Type assertion for environment variables
|
// Type assertion for environment variables
|
||||||
const env = c.env as unknown as Env;
|
const env = c.env as unknown as Env;
|
||||||
const emailStorage = env.EMAIL_STORAGE;
|
const emailStorage = env.EMAIL_STORAGE;
|
||||||
|
const isJson =
|
||||||
|
c.req.header("Content-Type")?.includes("application/json") ?? false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const formData = await c.req.formData();
|
let title: string;
|
||||||
const title = formData.get("title")?.toString() || "";
|
let description: string | undefined;
|
||||||
const description = formData.get("description")?.toString();
|
let language: string;
|
||||||
const language = formData.get("language")?.toString() || "en";
|
let view: string;
|
||||||
const view =
|
let allowedSenders: string[];
|
||||||
formData.get("view")?.toString() === "table" ? "table" : "list";
|
|
||||||
const allowedSenders = parseAllowedSenders(
|
if (isJson) {
|
||||||
formData.get("allowed_senders")?.toString() || "",
|
const body = await c.req.json<Record<string, unknown>>();
|
||||||
);
|
title = String(body.title ?? "");
|
||||||
|
description = body.description != null ? String(body.description) : undefined;
|
||||||
|
language = String(body.language ?? "en");
|
||||||
|
view = "list";
|
||||||
|
allowedSenders = Array.isArray(body.allowedSenders)
|
||||||
|
? (body.allowedSenders as unknown[]).map(String).map((s) => s.trim().toLowerCase()).filter(Boolean)
|
||||||
|
: [];
|
||||||
|
} else {
|
||||||
|
const formData = await c.req.formData();
|
||||||
|
title = formData.get("title")?.toString() || "";
|
||||||
|
description = formData.get("description")?.toString();
|
||||||
|
language = formData.get("language")?.toString() || "en";
|
||||||
|
view = formData.get("view")?.toString() === "table" ? "table" : "list";
|
||||||
|
allowedSenders = parseAllowedSenders(
|
||||||
|
formData.get("allowed_senders")?.toString() || "",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Validate inputs
|
// Validate inputs
|
||||||
const parsedData = createFeedSchema.parse({
|
const parsedData = createFeedSchema.parse({
|
||||||
@@ -1618,10 +1636,21 @@ app.post("/feeds/create", async (c) => {
|
|||||||
parsedData.description,
|
parsedData.description,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (isJson) {
|
||||||
|
return c.json({
|
||||||
|
feedId,
|
||||||
|
email: `${feedId}@${env.DOMAIN}`,
|
||||||
|
feedUrl: feedConfig.feed_url,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Redirect back to admin page
|
// Redirect back to admin page
|
||||||
return c.redirect(`/admin?view=${view}`);
|
return c.redirect(`/admin?view=${view}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error creating feed:", error);
|
console.error("Error creating feed:", error);
|
||||||
|
if (isJson) {
|
||||||
|
return c.json({ error: "Error creating feed." }, 400);
|
||||||
|
}
|
||||||
return c.text("Error creating feed. Please try again.", 400);
|
return c.text("Error creating feed. Please try again.", 400);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user