@sdxc/kit 2026.10.17 → 2026.10.20

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 (282) hide show
  1. package/README.md +390 -20
  2. package/changelog.jsonl +1160 -1092
  3. package/config.schema.json +47 -1
  4. package/dist/cli.js +12 -0
  5. package/dist/commands/deploy.d.ts +23 -0
  6. package/dist/commands/deploy.js +167 -0
  7. package/dist/commands/destroy.d.ts +22 -0
  8. package/dist/commands/destroy.js +92 -0
  9. package/dist/commands/doctor.d.ts +26 -0
  10. package/dist/commands/doctor.js +65 -0
  11. package/dist/commands/initializers.d.ts +16 -0
  12. package/dist/commands/initializers.js +23 -0
  13. package/dist/commands/make.d.ts +72 -6
  14. package/dist/commands/make.js +127 -19
  15. package/dist/commands/middleware.d.ts +16 -0
  16. package/dist/commands/middleware.js +21 -0
  17. package/dist/commands/models.d.ts +18 -0
  18. package/dist/commands/models.js +29 -0
  19. package/dist/commands/notes.d.ts +17 -0
  20. package/dist/commands/notes.js +27 -0
  21. package/dist/commands/routes.d.ts +25 -0
  22. package/dist/commands/routes.js +50 -0
  23. package/dist/commands/run.d.ts +16 -0
  24. package/dist/commands/run.js +32 -11
  25. package/dist/commands/scaffold.d.ts +29 -0
  26. package/dist/commands/scaffold.js +33 -0
  27. package/dist/commands/schedule.d.ts +16 -0
  28. package/dist/commands/schedule.js +21 -0
  29. package/dist/commands/stats.d.ts +16 -0
  30. package/dist/commands/stats.js +21 -0
  31. package/dist/commands/ui/update.d.ts +6 -3
  32. package/dist/commands/ui/update.js +24 -7
  33. package/dist/commands/upgrade.d.ts +3 -1
  34. package/dist/commands/upgrade.js +88 -23
  35. package/dist/config.d.ts +18 -0
  36. package/dist/config.js +28 -1
  37. package/dist/contract.d.ts +97 -3
  38. package/dist/contract.js +360 -26
  39. package/dist/create/templates.js +3 -2
  40. package/dist/dependencies.d.ts +4 -1
  41. package/dist/dependencies.js +13 -1
  42. package/dist/deploy/plan.d.ts +49 -0
  43. package/dist/deploy/plan.js +68 -0
  44. package/dist/destroy.d.ts +40 -0
  45. package/dist/destroy.js +311 -0
  46. package/dist/doctor/check.d.ts +71 -0
  47. package/dist/doctor/check.js +53 -0
  48. package/dist/doctor/checks.d.ts +23 -0
  49. package/dist/doctor/checks.js +68 -0
  50. package/dist/doctor/define.d.ts +59 -0
  51. package/dist/doctor/define.js +16 -0
  52. package/dist/doctor/emails.d.ts +18 -0
  53. package/dist/doctor/emails.js +243 -0
  54. package/dist/doctor/env.d.ts +26 -0
  55. package/dist/doctor/env.js +88 -0
  56. package/dist/doctor/files.d.ts +14 -0
  57. package/dist/doctor/files.js +34 -0
  58. package/dist/doctor/fixture.d.ts +23 -0
  59. package/dist/doctor/fixture.js +46 -0
  60. package/dist/doctor/install.d.ts +17 -0
  61. package/dist/doctor/install.js +37 -0
  62. package/dist/doctor/installed.d.ts +11 -0
  63. package/dist/doctor/installed.js +79 -0
  64. package/dist/doctor/layout.d.ts +23 -0
  65. package/dist/doctor/layout.js +83 -0
  66. package/dist/doctor/migrations.d.ts +16 -0
  67. package/dist/doctor/migrations.js +96 -0
  68. package/dist/doctor/models.d.ts +13 -0
  69. package/dist/doctor/models.js +69 -0
  70. package/dist/doctor/pins.d.ts +15 -0
  71. package/dist/doctor/pins.js +79 -0
  72. package/dist/doctor/project.d.ts +58 -0
  73. package/dist/doctor/project.js +261 -0
  74. package/dist/doctor/remix.d.ts +14 -0
  75. package/dist/doctor/remix.js +44 -0
  76. package/dist/doctor/routes.d.ts +11 -0
  77. package/dist/doctor/routes.js +49 -0
  78. package/dist/doctor/typegen.d.ts +13 -0
  79. package/dist/doctor/typegen.js +57 -0
  80. package/dist/doctor/ui.d.ts +14 -0
  81. package/dist/doctor/ui.js +51 -0
  82. package/dist/initializers.d.ts +25 -0
  83. package/dist/initializers.js +89 -0
  84. package/dist/lock.d.ts +2 -2
  85. package/dist/make/actions.d.ts +23 -0
  86. package/dist/make/actions.js +82 -0
  87. package/dist/make/declare.d.ts +31 -3
  88. package/dist/make/declare.js +284 -9
  89. package/dist/make/email.d.ts +23 -0
  90. package/dist/make/email.js +111 -0
  91. package/dist/make/from-fields.d.ts +30 -0
  92. package/dist/make/from-fields.js +162 -0
  93. package/dist/make/from-model.d.ts +39 -0
  94. package/dist/make/from-model.js +161 -0
  95. package/dist/make/generate.d.ts +16 -5
  96. package/dist/make/generate.js +38 -15
  97. package/dist/make/guard.d.ts +35 -0
  98. package/dist/make/guard.js +73 -0
  99. package/dist/make/imports.d.ts +6 -0
  100. package/dist/make/imports.js +19 -0
  101. package/dist/make/index.d.ts +47 -4
  102. package/dist/make/index.js +287 -29
  103. package/dist/make/kinds.d.ts +29 -2
  104. package/dist/make/kinds.js +79 -7
  105. package/dist/make/mount.d.ts +25 -0
  106. package/dist/make/mount.js +135 -0
  107. package/dist/make/schema-diff.d.ts +58 -0
  108. package/dist/make/schema-diff.js +609 -0
  109. package/dist/make/schema.d.ts +76 -0
  110. package/dist/make/schema.js +206 -0
  111. package/dist/make/stubs.d.ts +7 -5
  112. package/dist/make/stubs.js +16 -5
  113. package/dist/make/test.d.ts +43 -0
  114. package/dist/make/test.js +149 -0
  115. package/dist/make/trigger.d.ts +31 -0
  116. package/dist/make/trigger.js +163 -0
  117. package/dist/models.d.ts +42 -0
  118. package/dist/models.js +149 -0
  119. package/dist/notes.d.ts +28 -0
  120. package/dist/notes.js +66 -0
  121. package/dist/reference.d.ts +27 -0
  122. package/dist/reference.js +103 -0
  123. package/dist/releases.d.ts +9 -0
  124. package/dist/releases.js +29 -8
  125. package/dist/routes/chains.d.ts +34 -0
  126. package/dist/routes/chains.js +95 -0
  127. package/dist/routes/filter.d.ts +53 -0
  128. package/dist/routes/filter.js +147 -0
  129. package/dist/routes/format.d.ts +23 -0
  130. package/dist/routes/format.js +123 -0
  131. package/dist/routes/helpers.d.ts +21 -0
  132. package/dist/routes/helpers.js +165 -0
  133. package/dist/routes/index.d.ts +56 -0
  134. package/dist/routes/index.js +167 -0
  135. package/dist/routes/modules.d.ts +116 -0
  136. package/dist/routes/modules.js +235 -0
  137. package/dist/routes/parse.d.ts +161 -0
  138. package/dist/routes/parse.js +621 -0
  139. package/dist/routes/routers.d.ts +69 -0
  140. package/dist/routes/routers.js +327 -0
  141. package/dist/scaffold/fields.d.ts +129 -0
  142. package/dist/scaffold/fields.js +421 -0
  143. package/dist/scaffold/index.d.ts +37 -0
  144. package/dist/scaffold/index.js +613 -0
  145. package/dist/scaffold/render.d.ts +51 -0
  146. package/dist/scaffold/render.js +332 -0
  147. package/dist/schedule.d.ts +45 -0
  148. package/dist/schedule.js +124 -0
  149. package/dist/stats.d.ts +37 -0
  150. package/dist/stats.js +126 -0
  151. package/dist/ui/commands.d.ts +10 -3
  152. package/dist/ui/commands.js +85 -15
  153. package/dist/ui/registry.d.ts +8 -8
  154. package/dist/upgrade/agents.d.ts +49 -0
  155. package/dist/upgrade/agents.js +110 -0
  156. package/dist/upgrade/changelog.d.ts +2 -2
  157. package/dist/upgrade/project.d.ts +4 -0
  158. package/dist/upgrade/project.js +11 -1
  159. package/dist/upgrade/prompt.d.ts +11 -0
  160. package/dist/upgrade/prompt.js +50 -7
  161. package/package.json +6 -4
  162. package/registry/ui/alert-dialog.json +2 -2
  163. package/registry/ui/attachment.json +2 -2
  164. package/registry/ui/calendar.json +2 -2
  165. package/registry/ui/color-picker.json +2 -2
  166. package/registry/ui/command.json +2 -2
  167. package/registry/ui/date-picker.json +2 -2
  168. package/registry/ui/date-range-picker.json +2 -2
  169. package/registry/ui/dialog.json +2 -2
  170. package/registry/ui/file-trigger.json +2 -2
  171. package/registry/ui/heading-scope.json +2 -2
  172. package/registry/ui/message-scroller.json +2 -2
  173. package/registry/ui/number-field.json +2 -2
  174. package/registry/ui/range-calendar.json +2 -2
  175. package/registry/ui/resizable.json +2 -2
  176. package/registry/ui/select.json +2 -2
  177. package/registry/ui/shared-element.json +2 -2
  178. package/stubs/controller.api.resource.stub +23 -0
  179. package/stubs/controller.form.stub +2 -2
  180. package/stubs/controller.form.test.stub +2 -2
  181. package/stubs/controller.resource.form.stub +67 -0
  182. package/stubs/controller.resource.stub +28 -0
  183. package/stubs/email.stub +9 -14
  184. package/stubs/email.test.stub +9 -9
  185. package/stubs/factory.stub +1 -1
  186. package/stubs/model.stub +3 -3
  187. package/stubs/policy.test.stub +34 -8
  188. package/stubs/scaffold.api.create.stub +34 -0
  189. package/stubs/scaffold.api.create.test.stub +37 -0
  190. package/stubs/scaffold.api.destroy.stub +34 -0
  191. package/stubs/scaffold.api.destroy.test.stub +44 -0
  192. package/stubs/scaffold.api.index.stub +23 -0
  193. package/stubs/scaffold.api.index.test.stub +31 -0
  194. package/stubs/scaffold.api.show.stub +27 -0
  195. package/stubs/scaffold.api.show.test.stub +41 -0
  196. package/stubs/scaffold.api.update.stub +44 -0
  197. package/stubs/scaffold.api.update.test.stub +46 -0
  198. package/stubs/scaffold.command.create.stub +22 -0
  199. package/stubs/scaffold.command.create.test.stub +30 -0
  200. package/stubs/scaffold.command.delete.stub +20 -0
  201. package/stubs/scaffold.command.delete.test.stub +39 -0
  202. package/stubs/scaffold.command.list.stub +18 -0
  203. package/stubs/scaffold.command.list.test.stub +31 -0
  204. package/stubs/scaffold.command.show.stub +19 -0
  205. package/stubs/scaffold.command.show.test.stub +37 -0
  206. package/stubs/scaffold.controller.destroy.stub +35 -0
  207. package/stubs/scaffold.controller.destroy.test.stub +65 -0
  208. package/stubs/scaffold.controller.edit.stub +97 -0
  209. package/stubs/scaffold.controller.edit.test.stub +91 -0
  210. package/stubs/scaffold.controller.index.stub +55 -0
  211. package/stubs/scaffold.controller.index.test.stub +32 -0
  212. package/stubs/scaffold.controller.new.stub +70 -0
  213. package/stubs/scaffold.controller.new.test.stub +81 -0
  214. package/stubs/scaffold.controller.show.stub +59 -0
  215. package/stubs/scaffold.controller.show.test.stub +43 -0
  216. package/stubs/scaffold.factory.stub +16 -0
  217. package/stubs/scaffold.form.stub +75 -0
  218. package/stubs/scaffold.mcp.create.stub +24 -0
  219. package/stubs/scaffold.mcp.create.test.stub +36 -0
  220. package/stubs/scaffold.mcp.delete.stub +31 -0
  221. package/stubs/scaffold.mcp.delete.test.stub +43 -0
  222. package/stubs/scaffold.mcp.get.stub +21 -0
  223. package/stubs/scaffold.mcp.get.test.stub +31 -0
  224. package/stubs/scaffold.mcp.list.stub +23 -0
  225. package/stubs/scaffold.mcp.list.test.stub +25 -0
  226. package/stubs/scaffold.mcp.update.stub +37 -0
  227. package/stubs/scaffold.mcp.update.test.stub +40 -0
  228. package/stubs/scaffold.migration.stub +2 -0
  229. package/stubs/scaffold.model.stub +35 -0
  230. package/stubs/scaffold.model.test.stub +25 -0
  231. package/stubs/scaffold.test-database.stub +33 -0
  232. package/stubs/scaffold.test-mcp.stub +92 -0
  233. package/stubs/scaffold.validator.stub +18 -0
  234. package/stubs/scaffold.validator.test.stub +26 -0
  235. package/templates/app/AGENTS.md +163 -0
  236. package/templates/app/CLAUDE.md +1 -0
  237. package/templates/app/README.md +28 -13
  238. package/templates/app/app/commands/console.test.ts +26 -0
  239. package/templates/app/app/commands/console.ts +19 -0
  240. package/templates/app/app/commands/db/fresh.test.ts +57 -0
  241. package/templates/app/app/commands/db/fresh.ts +37 -0
  242. package/templates/app/app/commands/db/migrate.test.ts +17 -1
  243. package/templates/app/app/commands/db/migrate.ts +51 -18
  244. package/templates/app/app/commands/db/prepare.test.ts +39 -0
  245. package/templates/app/app/commands/db/prepare.ts +35 -0
  246. package/templates/app/app/commands/db/seed.test.ts +38 -0
  247. package/templates/app/app/commands/db/seed.ts +26 -0
  248. package/templates/app/app/commands/deploy.test.ts +34 -0
  249. package/templates/app/app/commands/deploy.ts +18 -0
  250. package/templates/app/app/commands/emails/preview.test.tsx +63 -0
  251. package/templates/app/app/commands/emails/preview.ts +24 -0
  252. package/templates/app/app/commands/index.ts +57 -2
  253. package/templates/app/app/emails/index.ts +10 -0
  254. package/templates/app/app/http/middleware/access.test.ts +43 -0
  255. package/templates/app/app/http/middleware/access.ts +41 -0
  256. package/templates/app/app/http/middleware/api.ts +12 -3
  257. package/templates/app/app/http/middleware/page.test.ts +4 -1
  258. package/templates/app/app/http/middleware/page.ts +7 -5
  259. package/templates/app/app/initializers/cli.ts +28 -2
  260. package/templates/app/app/initializers/dispatcher.ts +9 -1
  261. package/templates/app/app/initializers/mail-transport.test.ts +27 -0
  262. package/templates/app/app/initializers/mail-transport.ts +26 -0
  263. package/templates/app/app/initializers/mail.test.ts +17 -17
  264. package/templates/app/app/initializers/mail.ts +15 -7
  265. package/templates/app/app/initializers/router.test.ts +7 -0
  266. package/templates/app/app/initializers/router.tsx +13 -3
  267. package/templates/app/app/jobs/email.test.tsx +70 -0
  268. package/templates/app/app/jobs/email.ts +16 -0
  269. package/templates/app/app/jobs/index.ts +6 -2
  270. package/templates/app/app/policies/abilities.ts +13 -0
  271. package/templates/app/app/policies/policy.test.ts +15 -0
  272. package/templates/app/app/policies/policy.ts +30 -0
  273. package/templates/app/app/services/commands.ts +30 -0
  274. package/templates/app/app/services/migrations.test.ts +48 -1
  275. package/templates/app/app/services/migrations.ts +66 -9
  276. package/templates/app/app/test/local-database.ts +67 -0
  277. package/templates/app/app/test/session.ts +28 -0
  278. package/templates/app/bootstrap/console.ts +13 -0
  279. package/templates/app/cloudflare.config.ts +3 -3
  280. package/templates/app/database/seed.ts +12 -0
  281. package/templates/app/package.json +2 -1
  282. package/stubs/policy.stub +0 -16
@@ -0,0 +1,97 @@
1
+ /**
2
+ * The edit form of one {{words}} and the action that saves it: a valid submission lands back on
3
+ * the {{words}}'s page, a refused one renders the form again with each issue beside its field,
4
+ * and an id no {{words}} has, or one `{{path}}.read` hides, is a 404. A visitor
5
+ * `{{path}}.update` refuses gets a 403.
6
+ *
7
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/forms-and-params}
8
+ * @see {@link https://sdxc.dev/docs/identity-and-security/protect-forms}{{header}}
9
+ */
10
+
11
+ import { NotFound } from "@sdxc/data-model";
12
+ import { isFailure, unwrap } from "@sdxc/result";
13
+ import { validate } from "@sdxc/validate";
14
+ import { createController } from "remix/router";
15
+
16
+ import page from "~/app/http/middleware/page";
17
+ import { {{Name}}Schema } from "~/app/http/validators/{{name}}";
18
+ import abilities from "~/app/policies/abilities";
19
+ import DocumentLayout from "~/resources/layouts/document";
20
+ import {
21
+ {{Name}}FormPage,
22
+ {{key}}FormValues,
23
+ submittedValues,
24
+ } from "~/resources/views/{{plural}}/form";
25
+ import routes from "~/routes/web";
26
+
27
+ export default createController(routes.{{pluralKey}}.edit, {
28
+ middleware: page,
29
+ actions: {
30
+ /** Renders the form filled with the {{words}}'s values and fresh honeypot fields. */
31
+ async index(ctx) {
32
+ let row = await ctx.models.{{key}}.find(ctx.params.id);
33
+ if (row === null || !ctx.access.can(abilities.{{path}}.read, { {{key}}: row })) {
34
+ let missing = ctx.intl.t("notFound.title");
35
+ return ctx.render(
36
+ <DocumentLayout assets={ctx.assets} intl={ctx.intl} url={ctx.url} title={missing}>
37
+ <h1>{missing}</h1>
38
+ </DocumentLayout>,
39
+ { status: 404 },
40
+ );
41
+ }
42
+ if (!ctx.access.can(abilities.{{path}}.update, { {{key}}: row })) {
43
+ return new Response(null, { status: 403 });
44
+ }
45
+ return ctx.render(
46
+ <{{Name}}FormPage
47
+ assets={ctx.assets}
48
+ intl={ctx.intl}
49
+ url={ctx.url}
50
+ title={ctx.intl.t("{{pluralKey}}.edit.title")}
51
+ action={routes.{{pluralKey}}.edit.action.href({ id: row.id })}
52
+ values={{{key}}FormValues(row)}
53
+ honeypot={unwrap(await ctx.honeypot.issue())}
54
+ submit={ctx.intl.t("{{pluralKey}}.edit.submit")}
55
+ />,
56
+ );
57
+ },
58
+
59
+ /**
60
+ * Saves the {{words}} and redirects to its page. A submission the schema or the database
61
+ * refuses renders the form again as a 422, keeping what the visitor typed.
62
+ */
63
+ async action(ctx) {
64
+ let { id } = ctx.params;
65
+ let row = await ctx.models.{{key}}.find(id);
66
+ if (row === null || !ctx.access.can(abilities.{{path}}.read, { {{key}}: row })) {
67
+ return new Response(null, { status: 404 });
68
+ }
69
+ if (!ctx.access.can(abilities.{{path}}.update, { {{key}}: row })) {
70
+ return new Response(null, { status: 403 });
71
+ }
72
+ let input = await validate(ctx.formData, {{Name}}Schema);
73
+ let updated = isFailure(input) ? input : await ctx.models.{{key}}.update(id, input.data);
74
+ if (isFailure(updated)) {
75
+ if (updated.error instanceof NotFound) return new Response(null, { status: 404 });
76
+ return ctx.render(
77
+ <{{Name}}FormPage
78
+ assets={ctx.assets}
79
+ intl={ctx.intl}
80
+ url={ctx.url}
81
+ title={ctx.intl.t("{{pluralKey}}.edit.title")}
82
+ action={routes.{{pluralKey}}.edit.action.href({ id })}
83
+ values={submittedValues(ctx.formData)}
84
+ issues={updated.error.issues}
85
+ honeypot={unwrap(await ctx.honeypot.issue())}
86
+ submit={ctx.intl.t("{{pluralKey}}.edit.submit")}
87
+ />,
88
+ { status: 422 },
89
+ );
90
+ }
91
+ return new Response(null, {
92
+ status: 303,
93
+ headers: { location: routes.{{pluralKey}}.show.href({ id }) },
94
+ });
95
+ },
96
+ },
97
+ });
@@ -0,0 +1,91 @@
1
+ /**
2
+ * One {{words}}'s edit form through the app's router as a signed-in member, so the honeypot and
3
+ * access checks run as in the Worker, and as a guest, whom `{{path}}.update` refuses. Its
4
+ * controller loads with the file, keeping its cold import outside each test's time limit.
5
+ *
6
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/forms-and-params}
7
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
8
+ */
9
+
10
+ import { unwrap } from "@sdxc/result";
11
+ import { beforeAll, describe, expect, test } from "vitest";
12
+
13
+ import "~/app/http/controllers/{{plural}}/edit";
14
+ import { honeypot } from "~/app/initializers/honeypot";
15
+ import { createWebRouter } from "~/app/initializers/router";
16
+ import { factories, migrate } from "~/app/test/database";
17
+ import { signIn } from "~/app/test/session";
18
+ import { {{Name}}Factory } from "~/database/factories/{{name}}";
19
+ import routes from "~/routes/web";
20
+
21
+ beforeAll(migrate);
22
+
23
+ /**
24
+ * Posts `fields` to the update action of the {{words}} with `id`, with valid honeypot fields, as
25
+ * `cookie`'s visitor.
26
+ */
27
+ async function submit(id: string, fields: Record<string, string>, cookie: string | null) {
28
+ let { token, tokenField } = unwrap(await honeypot.issue());
29
+ return createWebRouter().fetch(
30
+ new Request(new URL(routes.{{pluralKey}}.edit.action.href({ id }), "https://example.com"), {
31
+ method: "POST",
32
+ headers: cookie === null ? {} : { cookie },
33
+ body: new URLSearchParams({ ...fields, [tokenField]: token }),
34
+ }),
35
+ );
36
+ }
37
+
38
+ /** Fetches the edit form of the {{words}} with `id` as `cookie`'s visitor. */
39
+ function form(id: string, cookie: string | null) {
40
+ return createWebRouter().fetch(
41
+ new Request(new URL(routes.{{pluralKey}}.edit.index.href({ id }), "https://example.com"), {
42
+ headers: cookie === null ? {} : { cookie },
43
+ }),
44
+ );
45
+ }
46
+
47
+ describe("{{plural}}/edit", () => {
48
+ test("renders the form filled with the {{words}}'s values", async () => {
49
+ let row = await factories().create({{Name}}Factory);
50
+
51
+ let response = await form(row.id, await signIn());
52
+
53
+ expect(response.status).toBe(200);
54
+ expect(await response.text()).toContain(
55
+ `action="${routes.{{pluralKey}}.edit.action.href({ id: row.id })}"`,
56
+ );
57
+ });
58
+
59
+ test("saves the {{words}} and redirects to its page", async () => {
60
+ let row = await factories().create({{Name}}Factory);
61
+
62
+ let response = await submit(row.id, { {{formSample}} }, await signIn());
63
+
64
+ expect(response.status).toBe(303);
65
+ expect(response.headers.get("location")).toBe(routes.{{pluralKey}}.show.href({ id: row.id }));
66
+ });
67
+
68
+ test("renders the form again for a refused submission", async () => {
69
+ let row = await factories().create({{Name}}Factory);
70
+
71
+ let response = await submit(row.id, { {{invalidForm}} }, await signIn());
72
+
73
+ expect(response.status).toBe(422);
74
+ });
75
+
76
+ test("answers 404 for an id no {{words}} has", async () => {
77
+ let response = await submit("missing", { {{formSample}} }, await signIn());
78
+
79
+ expect(response.status).toBe(404);
80
+ });
81
+
82
+ test("answers a guest 403 for the form and the action", async () => {
83
+ let row = await factories().create({{Name}}Factory);
84
+
85
+ let page = await form(row.id, null);
86
+ let response = await submit(row.id, { {{formSample}} }, null);
87
+
88
+ expect(page.status).toBe(403);
89
+ expect(response.status).toBe(403);
90
+ });
91
+ });
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Every {{words}} the visitor may read, newest first, each linking to its page, with the way to
3
+ * add another.
4
+ *
5
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/wire-the-router}
6
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/interface-with-remix-ui}{{header}}
7
+ */
8
+
9
+ import { Link } from "@sdxc/ui/link";
10
+ import { LinkButton } from "@sdxc/ui/link-button";
11
+ import { Table } from "@sdxc/ui/table";
12
+ import { createAction } from "remix/router";
13
+
14
+ import page from "~/app/http/middleware/page";
15
+ import abilities from "~/app/policies/abilities";
16
+ import DocumentLayout from "~/resources/layouts/document";
17
+ import routes from "~/routes/web";
18
+
19
+ export default createAction(routes.{{pluralKey}}.index, {
20
+ middleware: page,
21
+ /** Lists the {{pluralWords}} `{{path}}.read` allows, so a hidden one is left out of the list. */
22
+ async handler(ctx) {
23
+ let all = await ctx.models.{{key}}.query().orderBy("created_at", "desc").all();
24
+ let rows = all.filter((row) => ctx.access.can(abilities.{{path}}.read, { {{key}}: row }));
25
+ let title = ctx.intl.t("{{pluralKey}}.index.title");
26
+ return ctx.render(
27
+ <DocumentLayout assets={ctx.assets} intl={ctx.intl} url={ctx.url} title={title}>
28
+ <h1>{title}</h1>
29
+ <p>
30
+ <LinkButton href={routes.{{pluralKey}}.new.index.href()}>
31
+ {ctx.intl.t("{{pluralKey}}.index.new")}
32
+ </LinkButton>
33
+ </p>
34
+ {rows.length === 0 ? (
35
+ <p>{ctx.intl.t("{{pluralKey}}.index.empty")}</p>
36
+ ) : (
37
+ <Table aria-label={title}>
38
+ <Table.Header>
39
+ <Table.Row>
40
+ {{tableColumns}}
41
+ </Table.Row>
42
+ </Table.Header>
43
+ <Table.Body>
44
+ {rows.map((row) => (
45
+ <Table.Row key={row.id}>
46
+ {{tableCells}}
47
+ </Table.Row>
48
+ ))}
49
+ </Table.Body>
50
+ </Table>
51
+ )}
52
+ </DocumentLayout>,
53
+ );
54
+ },
55
+ });
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The {{pluralWords}} index page, rendered through the app's router. Its controller loads with
3
+ * the file, so the cold import of its views stays outside each test's time limit.
4
+ *
5
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/wire-the-router}
6
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
7
+ */
8
+
9
+ import { beforeAll, describe, expect, test } from "vitest";
10
+
11
+ import "~/app/http/controllers/{{plural}}/index";
12
+ import { createWebRouter } from "~/app/initializers/router";
13
+ import { factories, migrate } from "~/app/test/database";
14
+ import { {{Name}}Factory } from "~/database/factories/{{name}}";
15
+ import routes from "~/routes/web";
16
+
17
+ beforeAll(migrate);
18
+
19
+ describe("{{plural}}/index", () => {
20
+ test("lists each {{words}}, linking to its page", async () => {
21
+ let row = await factories().create({{Name}}Factory);
22
+
23
+ let response = await createWebRouter().fetch(
24
+ new URL(routes.{{pluralKey}}.index.href(), "https://example.com"),
25
+ );
26
+
27
+ expect(response.status).toBe(200);
28
+ expect(await response.text()).toContain(
29
+ `href="${routes.{{pluralKey}}.show.href({ id: row.id })}"`,
30
+ );
31
+ });
32
+ });
@@ -0,0 +1,70 @@
1
+ /**
2
+ * The new {{words}} form and the action that creates one: a valid submission lands on the new
3
+ * {{words}}'s page, and a refused one renders the form again with each issue beside its field.
4
+ * A visitor `{{path}}.create` refuses gets a 403 from both.
5
+ *
6
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/forms-and-params}
7
+ * @see {@link https://sdxc.dev/docs/identity-and-security/protect-forms}{{header}}
8
+ */
9
+
10
+ import { isFailure, unwrap } from "@sdxc/result";
11
+ import { validate } from "@sdxc/validate";
12
+ import { createController } from "remix/router";
13
+
14
+ import page from "~/app/http/middleware/page";
15
+ import { {{Name}}Schema } from "~/app/http/validators/{{name}}";
16
+ import abilities from "~/app/policies/abilities";
17
+ import { {{Name}}FormPage, submittedValues } from "~/resources/views/{{plural}}/form";
18
+ import routes from "~/routes/web";
19
+
20
+ export default createController(routes.{{pluralKey}}.new, {
21
+ middleware: page,
22
+ actions: {
23
+ /** Renders the empty form with fresh honeypot fields. */
24
+ async index(ctx) {
25
+ if (!ctx.access.can(abilities.{{path}}.create)) return new Response(null, { status: 403 });
26
+ return ctx.render(
27
+ <{{Name}}FormPage
28
+ assets={ctx.assets}
29
+ intl={ctx.intl}
30
+ url={ctx.url}
31
+ title={ctx.intl.t("{{pluralKey}}.new.title")}
32
+ action={routes.{{pluralKey}}.new.action.href()}
33
+ values={{}}
34
+ honeypot={unwrap(await ctx.honeypot.issue())}
35
+ submit={ctx.intl.t("{{pluralKey}}.new.submit")}
36
+ />,
37
+ );
38
+ },
39
+
40
+ /**
41
+ * Creates the {{words}} and redirects to its page. A submission the schema or the database
42
+ * refuses renders the form again as a 422, keeping what the visitor typed.
43
+ */
44
+ async action(ctx) {
45
+ if (!ctx.access.can(abilities.{{path}}.create)) return new Response(null, { status: 403 });
46
+ let input = await validate(ctx.formData, {{Name}}Schema);
47
+ let created = isFailure(input) ? input : await ctx.models.{{key}}.create(input.data);
48
+ if (isFailure(created)) {
49
+ return ctx.render(
50
+ <{{Name}}FormPage
51
+ assets={ctx.assets}
52
+ intl={ctx.intl}
53
+ url={ctx.url}
54
+ title={ctx.intl.t("{{pluralKey}}.new.title")}
55
+ action={routes.{{pluralKey}}.new.action.href()}
56
+ values={submittedValues(ctx.formData)}
57
+ issues={created.error.issues}
58
+ honeypot={unwrap(await ctx.honeypot.issue())}
59
+ submit={ctx.intl.t("{{pluralKey}}.new.submit")}
60
+ />,
61
+ { status: 422 },
62
+ );
63
+ }
64
+ return new Response(null, {
65
+ status: 303,
66
+ headers: { location: routes.{{pluralKey}}.show.href({ id: created.data.id }) },
67
+ });
68
+ },
69
+ },
70
+ });
@@ -0,0 +1,81 @@
1
+ /**
2
+ * The new {{words}} form through the app's router as a signed-in member, so the honeypot check
3
+ * and the access check run as in the Worker, and as a guest, whom `{{path}}.create` refuses. Its
4
+ * controller loads with the file, keeping its cold import outside each test's time limit.
5
+ *
6
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/forms-and-params}
7
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
8
+ */
9
+
10
+ import { unwrap } from "@sdxc/result";
11
+ import { beforeAll, describe, expect, test } from "vitest";
12
+
13
+ import "~/app/http/controllers/{{plural}}/new";
14
+ import { openDatabase } from "~/app/initializers/database";
15
+ import { honeypot } from "~/app/initializers/honeypot";
16
+ import { createWebRouter } from "~/app/initializers/router";
17
+ import { {{Name}} } from "~/app/models/{{name}}";
18
+ import { migrate } from "~/app/test/database";
19
+ import { signIn } from "~/app/test/session";
20
+ import routes from "~/routes/web";
21
+
22
+ beforeAll(migrate);
23
+
24
+ /** Posts `fields` to the create action with valid honeypot fields, as `cookie`'s visitor. */
25
+ async function submit(fields: Record<string, string>, cookie: string | null) {
26
+ let { token, tokenField } = unwrap(await honeypot.issue());
27
+ return createWebRouter().fetch(
28
+ new Request(new URL(routes.{{pluralKey}}.new.action.href(), "https://example.com"), {
29
+ method: "POST",
30
+ headers: cookie === null ? {} : { cookie },
31
+ body: new URLSearchParams({ ...fields, [tokenField]: token }),
32
+ }),
33
+ );
34
+ }
35
+
36
+ /** Fetches the empty form as `cookie`'s visitor. */
37
+ function form(cookie: string | null) {
38
+ return createWebRouter().fetch(
39
+ new Request(new URL(routes.{{pluralKey}}.new.index.href(), "https://example.com"), {
40
+ headers: cookie === null ? {} : { cookie },
41
+ }),
42
+ );
43
+ }
44
+
45
+ describe("{{plural}}/new", () => {
46
+ test("renders the form", async () => {
47
+ let response = await form(await signIn());
48
+
49
+ expect(response.status).toBe(200);
50
+ expect(await response.text()).toContain('name="{{firstColumn}}"');
51
+ });
52
+
53
+ test("creates a {{words}} and redirects to its page", async () => {
54
+ let response = await submit({ {{formSample}} }, await signIn());
55
+
56
+ let rows = await {{Name}}.bind({ db: openDatabase() }).query().all();
57
+ expect(response.status).toBe(303);
58
+ expect(rows.map((row) => routes.{{pluralKey}}.show.href({ id: row.id }))).toContain(
59
+ response.headers.get("location"),
60
+ );
61
+ });
62
+
63
+ test("renders the form again for a refused submission", async () => {
64
+ let response = await submit({ {{invalidForm}} }, await signIn());
65
+
66
+ expect(response.status).toBe(422);
67
+ expect(await response.text()).toContain('name="{{firstColumn}}"');
68
+ });
69
+
70
+ test("answers a guest 403 for the form and the action, creating nothing", async () => {
71
+ let before = await {{Name}}.bind({ db: openDatabase() }).query().all();
72
+
73
+ let page = await form(null);
74
+ let response = await submit({ {{formSample}} }, null);
75
+
76
+ let after = await {{Name}}.bind({ db: openDatabase() }).query().all();
77
+ expect(page.status).toBe(403);
78
+ expect(response.status).toBe(403);
79
+ expect(after).toHaveLength(before.length);
80
+ });
81
+ });
@@ -0,0 +1,59 @@
1
+ /**
2
+ * One {{words}}'s page: every field, a link to edit it and a button that deletes it. An id no
3
+ * {{words}} has, and one `{{path}}.read` hides, is a 404.
4
+ *
5
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/wire-the-router}
6
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/interface-with-remix-ui}{{header}}
7
+ */
8
+
9
+ import { HoneypotFields } from "@sdxc/honeypot/ui";
10
+ import { unwrap } from "@sdxc/result";
11
+ import { Button } from "@sdxc/ui/button";
12
+ import { LinkButton } from "@sdxc/ui/link-button";
13
+ import { createAction } from "remix/router";
14
+
15
+ import page from "~/app/http/middleware/page";
16
+ import abilities from "~/app/policies/abilities";
17
+ import DocumentLayout from "~/resources/layouts/document";
18
+ import routes from "~/routes/web";
19
+
20
+ export default createAction(routes.{{pluralKey}}.show, {
21
+ middleware: page,
22
+ /** Renders the {{words}} with the delete form's honeypot fields. */
23
+ async handler(ctx) {
24
+ let row = await ctx.models.{{key}}.find(ctx.params.id);
25
+ if (row === null || !ctx.access.can(abilities.{{path}}.read, { {{key}}: row })) {
26
+ let missing = ctx.intl.t("notFound.title");
27
+ return ctx.render(
28
+ <DocumentLayout assets={ctx.assets} intl={ctx.intl} url={ctx.url} title={missing}>
29
+ <h1>{missing}</h1>
30
+ </DocumentLayout>,
31
+ { status: 404 },
32
+ );
33
+ }
34
+ let title = ctx.intl.t("{{pluralKey}}.show.title");
35
+ let fields = unwrap(await ctx.honeypot.issue());
36
+ return ctx.render(
37
+ <DocumentLayout assets={ctx.assets} intl={ctx.intl} url={ctx.url} title={title}>
38
+ <h1>{title}</h1>
39
+ <dl>
40
+ {{detailRows}}
41
+ </dl>
42
+ <p>
43
+ <LinkButton href={routes.{{pluralKey}}.edit.index.href({ id: row.id })}>
44
+ {ctx.intl.t("{{pluralKey}}.show.edit")}
45
+ </LinkButton>{" "}
46
+ <LinkButton href={routes.{{pluralKey}}.index.href()} variant="ghost">
47
+ {ctx.intl.t("{{pluralKey}}.show.back")}
48
+ </LinkButton>
49
+ </p>
50
+ <form method="post" action={routes.{{pluralKey}}.destroy.href({ id: row.id })}>
51
+ <HoneypotFields {...fields} />
52
+ <Button type="submit" color="danger">
53
+ {ctx.intl.t("{{pluralKey}}.show.delete")}
54
+ </Button>
55
+ </form>
56
+ </DocumentLayout>,
57
+ );
58
+ },
59
+ });
@@ -0,0 +1,43 @@
1
+ /**
2
+ * One {{words}}'s page, rendered through the app's router. Its controller loads with the file, so
3
+ * the cold import of its views stays outside each test's time limit.
4
+ *
5
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/wire-the-router}
6
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
7
+ */
8
+
9
+ import { beforeAll, describe, expect, test } from "vitest";
10
+
11
+ import "~/app/http/controllers/{{plural}}/show";
12
+ import { createWebRouter } from "~/app/initializers/router";
13
+ import { factories, migrate } from "~/app/test/database";
14
+ import { {{Name}}Factory } from "~/database/factories/{{name}}";
15
+ import routes from "~/routes/web";
16
+
17
+ beforeAll(migrate);
18
+
19
+ /** Fetches the page of the {{words}} with `id`. */
20
+ function show(id: string) {
21
+ return createWebRouter().fetch(
22
+ new URL(routes.{{pluralKey}}.show.href({ id }), "https://example.com"),
23
+ );
24
+ }
25
+
26
+ describe("{{plural}}/show", () => {
27
+ test("renders the {{words}} with links to edit and delete it", async () => {
28
+ let row = await factories().create({{Name}}Factory);
29
+
30
+ let response = await show(row.id);
31
+
32
+ expect(response.status).toBe(200);
33
+ let html = await response.text();
34
+ expect(html).toContain(`href="${routes.{{pluralKey}}.edit.index.href({ id: row.id })}"`);
35
+ expect(html).toContain(`action="${routes.{{pluralKey}}.destroy.href({ id: row.id })}"`);
36
+ });
37
+
38
+ test("answers 404 for an id no {{words}} has", async () => {
39
+ let response = await show("missing");
40
+
41
+ expect(response.status).toBe(404);
42
+ });
43
+ });
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The {{title}} factory tests seed rows with, through the same model the app writes with. Text
3
+ * values carry the factory's sequence, so two {{pluralWords}} a test creates never look alike.
4
+ *
5
+ * @see {@link https://sdxc.dev/docs/data-and-background-work/data-models}
6
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/test-data}{{header}}
7
+ */
8
+
9
+ import { defineFactory } from "@sdxc/data-model/testing";
10
+
11
+ import { {{Name}} } from "~/app/models/{{name}}";
12
+
13
+ /** A complete {{words}}; a test overrides only the fields it is about. */
14
+ export const {{Name}}Factory = defineFactory({{Name}}, {
15
+ {{attributes}}
16
+ });
@@ -0,0 +1,75 @@
1
+ /**
2
+ * The {{words}} form page the new and edit pages share: one field per column, filled from
3
+ * `values` on an edit or after a refused submission, with each refusal beside its field and the
4
+ * honeypot fields the page middleware checks when it is submitted.
5
+ *
6
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/forms-and-params}
7
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/interface-with-remix-ui}{{header}}
8
+ */
9
+
10
+ import type { DocumentAssets } from "@sdxc/assets-middleware";
11
+ import type { I18n } from "@sdxc/i18n";
12
+ import type { Handle } from "remix/component";
13
+
14
+ import type { {{Name}}Row } from "~/app/models/{{name}}";
15
+
16
+ {{formImports}}
17
+
18
+ import DocumentLayout from "~/resources/layouts/document";
19
+
20
+ /** The form's fields as text, the way a submission carries them: `"true"` for a ticked box. */
21
+ export type {{Name}}FormValues = Readonly<Record<string, string>>;
22
+
23
+ namespace {{Name}}FormPage {
24
+ export interface Props {
25
+ /** The request's document assets, translator and URL, as every page passes them on. */
26
+ assets: DocumentAssets;
27
+ intl: I18n;
28
+ url: URL;
29
+ /** The page's title and heading. */
30
+ title: string;
31
+ /** Where the form posts: the create action, or a row's update action. */
32
+ action: string;
33
+ /** What each field starts with, by the column it edits. */
34
+ values: {{Name}}FormValues;
35
+ /** Why the last submission was refused, shown beside each field it names. */
36
+ issues?: ReadonlyArray<Form.Issue>;
37
+ /** Fresh honeypot fields from `ctx.honeypot.issue()`. */
38
+ honeypot: HoneypotFields.Props;
39
+ /** The submit button's label. */
40
+ submit: string;
41
+ }
42
+ }
43
+
44
+ /** Renders the {{words}} form inside the document layout. */
45
+ export function {{Name}}FormPage(handle: Handle<{{Name}}FormPage.Props>) {
46
+ return () => {
47
+ let { action, assets, honeypot, intl, issues, submit, title, url, values } = handle.props;
48
+ return (
49
+ <DocumentLayout assets={assets} intl={intl} url={url} title={title}>
50
+ <h1>{title}</h1>
51
+ <Form method="post" action={action} issues={issues}>
52
+ <HoneypotFields {...honeypot} />
53
+ {{formFields}}
54
+ <Button type="submit">{submit}</Button>
55
+ </Form>
56
+ </DocumentLayout>
57
+ );
58
+ };
59
+ }
60
+
61
+ /** A {{words}}'s values as the edit form shows them. */
62
+ export function {{key}}FormValues(row: {{Name}}Row): {{Name}}FormValues {
63
+ return {
64
+ {{rowValues}}
65
+ };
66
+ }
67
+
68
+ /** A submission's text fields, so a refused form shows what the visitor typed. */
69
+ export function submittedValues(form: FormData): {{Name}}FormValues {
70
+ let values: Record<string, string> = {};
71
+ for (let [name, value] of form) {
72
+ if (typeof value === "string") values[name] = value;
73
+ }
74
+ return values;
75
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Answers `create_{{snake}}`: writes a {{words}} from every field and answers it as saved, id
3
+ * included. A caller `{{path}}.create` refuses is a forbidden call, and a write the database
4
+ * refuses is a tool error carrying why.
5
+ *
6
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
7
+ * @see {@link https://sdxc.dev/api/mcp}{{header}}
8
+ */
9
+
10
+ import { createTool, ForbiddenError, ToolError } from "@sdxc/mcp";
11
+ import { isFailure } from "@sdxc/result";
12
+
13
+ import toolset from "~/app/mcp/tools";
14
+ import { {{Name}} } from "~/app/models/{{name}}";
15
+ import abilities from "~/app/policies/abilities";
16
+
17
+ export default createTool(toolset.{{pluralKey}}.create, async (ctx) => {
18
+ if (!ctx.access.can(abilities.{{path}}.create)) {
19
+ throw new ForbiddenError("{{path}}.create is not permitted");
20
+ }
21
+ let created = await {{Name}}.bind({ db: ctx.db }).create(ctx.input);
22
+ if (isFailure(created)) throw new ToolError(created.error.message);
23
+ return created.data;
24
+ });