Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/cli/src/commands/forms/create.ts
T
2026-07-08 16:15:37 +02:00

48 lines
2.0 KiB
TypeScript

/**
* `forms create --tenant <id> --name <name> [--origin <url>]` — seed a
* minimal form definition (text + email + textarea + honeypot) and
* print the resulting public key for embed-snippet wiring.
*/
import { defineCommand } from "citty";
import consola from "consola";
import { FormDefinitionSchema, HONEYPOT_FIELD_KEY } from "@hivegrid/form-widget";
export const formsCreateCommand = defineCommand({
meta: { name: "create", description: "Seed a minimal form for a tenant." },
args: {
tenant: { type: "string", required: true, description: "Tenant ID." },
name: { type: "string", required: true, description: "Form display name." },
origin: { type: "string", required: false, description: "Allowed embedding origin (https://...)." },
},
async run({ args }) {
const tenantId = String(args.tenant);
const name = String(args.name);
const origin = args.origin ? String(args.origin) : undefined;
const definition = FormDefinitionSchema.parse({
title: name,
submitLabel: "Submit",
successMessage: "Thanks — we received your submission.",
fields: [
{ key: "full_name", type: "text", label: "Full name", required: true },
{ key: "email", type: "email", label: "Email", required: true },
{ key: "message", type: "textarea", label: "Message", required: false },
{ key: HONEYPOT_FIELD_KEY, type: "honeypot", label: "Leave empty" },
],
});
const { getDbClient } = await import("@hivegrid/db/client.server");
const prisma = getDbClient();
const created = await prisma.form.create({
data: {
tenantId,
name,
definition: definition as object,
allowedOrigins: origin ? [origin] : [],
config: { honeypotEnabled: true } as object,
},
select: { id: true, key: true },
});
consola.success(`Form created: id=${created.id} key=${created.key}`);
consola.log(`Embed: <script src="<host>/embed/widget.js" data-form-key="${created.key}" async></script>`);
},
});