@formmy.app/cli 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +5 -0
  2. package/index.mjs +35 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -9,3 +9,8 @@ npx @formmy.app/cli forms list
9
9
  ```
10
10
 
11
11
  `create` imprime el `<iframe>`; pégalo en tu HTML (p. ej. el `index.html` de GitHub Pages). `--json` para salida máquina.
12
+
13
+ ```bash
14
+ npx @formmy.app/cli forms update <formId> --message "¡Listo! Descarga la guía: https://…"
15
+ npx @formmy.app/cli forms answers <formId> # quién llenó el formulario
16
+ ```
package/index.mjs CHANGED
@@ -7,6 +7,9 @@ const HELP = `Uso:
7
7
  formmy forms create --name <nombre> [--type contact|subscription] [--inputs name,email,message]
8
8
  [--theme dark|light] [--color #9b9aea] [--message "Gracias"] [--json]
9
9
  formmy forms list [--json]
10
+ formmy forms update <formId> [--name <nombre>] [--inputs name,email] [--theme dark|light]
11
+ [--color #9b9aea] [--message "Gracias"] [--json]
12
+ formmy forms answers <formId> [--limit 50] [--json]
10
13
 
11
14
  Requiere FORMMY_SECRET_KEY (formmy_sk_live_…, en formmy.app/dashboard/api-keys).
12
15
  Inputs válidos: name, email, message, phone, company.`;
@@ -22,8 +25,9 @@ function parseFlags(args) {
22
25
  return flags;
23
26
  }
24
27
 
25
- async function call(intent, method, body) {
26
- const res = await fetch(`${API}?intent=${intent}`, {
28
+ async function call(intent, method, body, params = {}) {
29
+ const qs = new URLSearchParams({ intent, ...params });
30
+ const res = await fetch(`${API}?${qs}`, {
27
31
  method,
28
32
  headers: { Authorization: `Bearer ${KEY}`, "Content-Type": "application/json" },
29
33
  body: body ? JSON.stringify(body) : undefined,
@@ -38,8 +42,10 @@ async function call(intent, method, body) {
38
42
 
39
43
  const [resource, action, ...rest] = process.argv.slice(2);
40
44
  const flags = parseFlags(rest);
45
+ // update/answers llevan el id como primer argumento posicional
46
+ const formId = rest[0] && !rest[0].startsWith("--") ? rest[0] : null;
41
47
 
42
- if (resource !== "forms" || !["create", "list"].includes(action)) {
48
+ if (resource !== "forms" || !["create", "list", "update", "answers"].includes(action)) {
43
49
  console.log(HELP);
44
50
  process.exit(resource ? 1 : 0);
45
51
  }
@@ -63,6 +69,32 @@ if (action === "create") {
63
69
  });
64
70
  if (flags.json) console.log(JSON.stringify(form, null, 2));
65
71
  else console.log(`✓ ${form.name} (${form.id})\nEditar: ${form.editUrl}\n\n${form.embed.iframe}`);
72
+ } else if (action === "update" || action === "answers") {
73
+ if (!formId) {
74
+ console.error("Falta el <formId> (sale en `formmy forms list`).\n\n" + HELP);
75
+ process.exit(1);
76
+ }
77
+ if (action === "update") {
78
+ const body = {
79
+ ...(flags.name && { name: flags.name }),
80
+ ...(flags.inputs && { inputs: String(flags.inputs).split(",").map((s) => s.trim()) }),
81
+ ...(flags.theme && { theme: flags.theme }),
82
+ ...(flags.color && { ctaColor: flags.color }),
83
+ ...(flags.message && { message: flags.message }),
84
+ };
85
+ if (!Object.keys(body).length) {
86
+ console.error("Nada que cambiar: pasa al menos un flag.\n\n" + HELP);
87
+ process.exit(1);
88
+ }
89
+ const { form } = await call("forms.update", "POST", body, { formId });
90
+ if (flags.json) console.log(JSON.stringify(form, null, 2));
91
+ else console.log(`✓ ${form.name} (${form.id}) actualizado\nMensaje: ${form.config.message}`);
92
+ } else {
93
+ const { answers } = await call("forms.answers", "GET", null, { formId, ...(flags.limit && { limit: flags.limit }) });
94
+ if (flags.json) console.log(JSON.stringify(answers, null, 2));
95
+ else if (!answers.length) console.log("Sin respuestas todavía.");
96
+ else for (const a of answers) console.log(`${a.createdAt.slice(0, 16).replace("T", " ")} ${Object.entries(a.data || {}).map(([k, v]) => `${k}=${v}`).join(" ")}`);
97
+ }
66
98
  } else {
67
99
  const { forms } = await call("forms.list", "GET");
68
100
  if (flags.json) console.log(JSON.stringify(forms, null, 2));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formmy.app/cli",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Formmy desde la terminal: crea formularios y obtén su iframe para embeber",
5
5
  "type": "module",
6
6
  "bin": { "formmy": "index.mjs" },