@sdxc/kit 2026.10.16 → 2026.10.17

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 (114) hide show
  1. package/README.md +1 -0
  2. package/changelog.jsonl +57 -0
  3. package/dist/author.d.ts +36 -0
  4. package/dist/author.js +107 -0
  5. package/dist/commands/create.js +9 -4
  6. package/dist/commands/run.d.ts +2 -2
  7. package/dist/commands/run.js +5 -3
  8. package/dist/contract.js +1 -1
  9. package/dist/create/templates.d.ts +8 -1
  10. package/dist/create/templates.js +25 -2
  11. package/dist/init.d.ts +5 -3
  12. package/dist/init.js +15 -7
  13. package/dist/make/declare.d.ts +4 -4
  14. package/dist/make/declare.js +124 -21
  15. package/dist/make/generate.js +8 -2
  16. package/dist/make/index.js +5 -1
  17. package/dist/make/kinds.d.ts +2 -1
  18. package/dist/make/kinds.js +3 -2
  19. package/dist/testing.d.ts +12 -2
  20. package/dist/testing.js +22 -3
  21. package/package.json +3 -3
  22. package/stubs/command.stub +4 -0
  23. package/stubs/command.test.stub +3 -0
  24. package/stubs/controller.api.stub +21 -0
  25. package/stubs/controller.api.test.stub +18 -0
  26. package/stubs/controller.form.stub +39 -9
  27. package/stubs/controller.form.test.stub +61 -0
  28. package/stubs/controller.stub +15 -8
  29. package/stubs/controller.test.stub +3 -0
  30. package/stubs/email.stub +12 -30
  31. package/stubs/email.test.stub +6 -1
  32. package/stubs/factory.stub +3 -0
  33. package/stubs/initializer.stub +2 -0
  34. package/stubs/initializer.test.stub +3 -0
  35. package/stubs/job.cron.stub +4 -0
  36. package/stubs/job.stub +4 -0
  37. package/stubs/job.test.stub +3 -0
  38. package/stubs/mcp.resource.stub +3 -0
  39. package/stubs/mcp.stub +3 -0
  40. package/stubs/mcp.test.stub +3 -0
  41. package/stubs/middleware.stub +6 -1
  42. package/stubs/middleware.test.stub +3 -0
  43. package/stubs/model.stub +10 -1
  44. package/stubs/model.test.stub +3 -0
  45. package/stubs/policy.stub +3 -0
  46. package/stubs/policy.test.stub +3 -0
  47. package/stubs/service.stub +4 -0
  48. package/stubs/service.test.stub +3 -0
  49. package/stubs/validator.stub +3 -0
  50. package/stubs/validator.test.stub +3 -0
  51. package/templates/app/.env.example +16 -0
  52. package/templates/app/README.md +62 -35
  53. package/templates/app/app/commands/about.test.ts +1 -1
  54. package/templates/app/app/commands/db/console.ts +6 -0
  55. package/templates/app/app/commands/db/migrate.test.ts +30 -0
  56. package/templates/app/app/commands/db/migrate.ts +55 -0
  57. package/templates/app/app/commands/index.ts +16 -0
  58. package/templates/app/app/commands/typegen.test.ts +17 -0
  59. package/templates/app/app/commands/typegen.ts +16 -0
  60. package/templates/app/app/http/controllers/api/health.tsx +15 -0
  61. package/templates/app/app/http/controllers/api/not-found.tsx +16 -0
  62. package/templates/app/app/http/controllers/api/openapi.tsx +17 -0
  63. package/templates/app/app/http/controllers/home.tsx +21 -13
  64. package/templates/app/app/http/controllers/mcp.tsx +16 -0
  65. package/templates/app/app/http/controllers/not-found.tsx +23 -18
  66. package/templates/app/app/http/controllers/robots.test.ts +18 -0
  67. package/templates/app/app/http/controllers/robots.ts +24 -0
  68. package/templates/app/app/http/controllers/sitemap.test.ts +28 -0
  69. package/templates/app/app/http/controllers/sitemap.ts +25 -0
  70. package/templates/app/app/http/middleware/api.ts +10 -0
  71. package/templates/app/app/http/middleware/page.test.ts +40 -0
  72. package/templates/app/app/http/middleware/page.ts +49 -0
  73. package/templates/app/app/http/middleware/server-timing.test.ts +15 -0
  74. package/templates/app/app/http/middleware/server-timing.ts +42 -0
  75. package/templates/app/app/initializers/assets.ts +1 -1
  76. package/templates/app/app/initializers/captcha.ts +11 -0
  77. package/templates/app/app/initializers/cli.ts +42 -2
  78. package/templates/app/app/initializers/database.test.ts +18 -0
  79. package/templates/app/app/initializers/database.ts +18 -0
  80. package/templates/app/app/initializers/dispatcher.ts +18 -0
  81. package/templates/app/app/initializers/honeypot.ts +11 -0
  82. package/templates/app/app/initializers/i18n.test.ts +16 -0
  83. package/templates/app/app/initializers/i18n.ts +28 -0
  84. package/templates/app/app/initializers/local-database.test.ts +33 -0
  85. package/templates/app/app/initializers/local-database.ts +53 -0
  86. package/templates/app/app/initializers/mail.test.ts +27 -0
  87. package/templates/app/app/initializers/mail.ts +11 -0
  88. package/templates/app/app/initializers/mcp.ts +1 -1
  89. package/templates/app/app/initializers/openapi.ts +21 -0
  90. package/templates/app/app/initializers/router.test.ts +61 -0
  91. package/templates/app/app/initializers/router.tsx +72 -26
  92. package/templates/app/app/initializers/seo.ts +15 -0
  93. package/templates/app/app/initializers/session.test.ts +34 -0
  94. package/templates/app/app/initializers/session.ts +29 -0
  95. package/templates/app/app/initializers/spam.ts +10 -0
  96. package/templates/app/app/models/index.ts +10 -0
  97. package/templates/app/app/services/migrations.test.ts +45 -0
  98. package/templates/app/app/services/migrations.ts +60 -0
  99. package/templates/app/app/test/cloudflare-workers.ts +25 -0
  100. package/templates/app/bootstrap/cli.ts +1 -1
  101. package/templates/app/bootstrap/worker.ts +4 -4
  102. package/templates/app/cloudflare.config.ts +29 -1
  103. package/templates/app/config/constants.ts +19 -0
  104. package/templates/app/config/problems.ts +24 -0
  105. package/templates/app/config/security-policy.ts +34 -0
  106. package/templates/app/config/security-txt.ts +28 -0
  107. package/templates/app/database/migrations/.gitkeep +0 -0
  108. package/templates/app/package.json +35 -5
  109. package/templates/app/resources/layouts/document.tsx +28 -7
  110. package/templates/app/resources/locales/en.json +10 -0
  111. package/templates/app/routes/api.ts +29 -0
  112. package/templates/app/routes/web.ts +6 -4
  113. package/templates/app/vite.config.ts +4 -0
  114. package/templates/app/config/router-context.d.ts +0 -20
@@ -0,0 +1,61 @@
1
+ /**
2
+ * The {{title}} form, rendered and submitted through the app's router, so every middleware
3
+ * and the page chain's checks run as they do in the Worker.
4
+ *
5
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/forms-and-params}
6
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
7
+ */
8
+
9
+ import { unwrap } from "@sdxc/result";
10
+ import { describe, expect, test } from "vitest";
11
+
12
+ import { honeypot } from "~/app/initializers/honeypot";
13
+ import { createWebRouter } from "~/app/initializers/router";
14
+ import routes from "{{routes}}";
15
+
16
+ /** Posts `form` to the form's action. */
17
+ function submit(form: Record<string, string>) {
18
+ return createWebRouter().fetch(
19
+ new Request(new URL(routes.{{path}}.action.href(), "https://example.com"), {
20
+ method: "POST",
21
+ body: new URLSearchParams(form),
22
+ }),
23
+ );
24
+ }
25
+
26
+ describe("{{name}}", () => {
27
+ test("renders the form with its honeypot fields and CAPTCHA widget", async () => {
28
+ let response = await createWebRouter().fetch(
29
+ new URL(routes.{{path}}.index.href(), "https://example.com"),
30
+ );
31
+
32
+ expect(response.status).toBe(200);
33
+ let html = await response.text();
34
+ expect(html).toContain(`name="${honeypot.tokenField}"`);
35
+ expect(html).toContain('class="cf-turnstile"');
36
+ });
37
+
38
+ test("refuses a submission without the honeypot fields", async () => {
39
+ let response = await submit({});
40
+
41
+ expect(response.status).toBe(400);
42
+ });
43
+
44
+ test("refuses a submission that filled the trap", async () => {
45
+ let { token, tokenField, trapField } = unwrap(await honeypot.issue());
46
+
47
+ let response = await submit({ [tokenField]: token, [trapField]: "https://spam.example" });
48
+
49
+ expect(response.status).toBe(400);
50
+ });
51
+
52
+ test("refuses a submission without a CAPTCHA token", async () => {
53
+ let { token, tokenField } = unwrap(await honeypot.issue());
54
+
55
+ let response = await submit({ [tokenField]: token });
56
+
57
+ expect(response.status).toBe(403);
58
+ });
59
+
60
+ test.todo("saves a {{title}} submission");
61
+ });
@@ -1,17 +1,24 @@
1
1
  /**
2
2
  * The {{title}} page: describe what a visitor finds here and why the route exists.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/wire-the-router}
5
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/interface-with-remix-ui}{{header}}
3
6
  */
4
7
 
5
8
  import { createAction } from "remix/router";
6
9
 
10
+ import page from "~/app/http/middleware/page";
7
11
  import DocumentLayout from "~/resources/layouts/document";
8
12
  import routes from "{{routes}}";
9
13
 
10
- /** Renders the {{title}} page. */
11
- export default createAction(routes.{{path}}, (ctx) =>
12
- ctx.render(
13
- <DocumentLayout assets={ctx.assets} title="{{title}}">
14
- <h1>{{title}}</h1>
15
- </DocumentLayout>,
16
- ),
17
- );
14
+ export default createAction(routes.{{path}}, {
15
+ middleware: page,
16
+ /** Renders the {{title}} page. */
17
+ handler: (ctx) =>
18
+ ctx.render(
19
+ <DocumentLayout assets={ctx.assets} intl={ctx.intl} url={ctx.url} title="{{title}}">
20
+ <h1>{{title}}</h1>
21
+ {/* TODO: render the page */}
22
+ </DocumentLayout>,
23
+ ),
24
+ });
@@ -1,5 +1,8 @@
1
1
  /**
2
2
  * The {{title}} controller.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/wire-the-router}
5
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
3
6
  */
4
7
 
5
8
  import { describe, expect, test } from "vitest";
package/stubs/email.stub CHANGED
@@ -1,44 +1,26 @@
1
1
  /**
2
2
  * The {{title}} email: describe who receives it and what it tells them.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/data-and-background-work/send-email}
5
+ * @see {@link https://sdxc.dev/api/mail}{{header}}
3
6
  */
4
7
 
5
- import type { Handle } from "remix/component";
6
- import type { Email as EmailContract } from "@sdxc/mail";
8
+ import { defineEmail, Email } from "@sdxc/mail";
7
9
 
8
- import { Email } from "@sdxc/mail";
9
-
10
- /** What the {{title}} email says. */
10
+ /** What the {{title}} email says, already translated for its reader. */
11
11
  export interface {{Name}}Copy {
12
12
  heading: string;
13
13
  body: string;
14
14
  }
15
15
 
16
- /** Renders the email's body from copy the caller wrote. */
17
- function {{Name}}Body(handle: Handle<{{Name}}Copy>) {
18
- return () => (
16
+ /** The {{title}} email, sent with `new {{Name}}Email(recipient, copy)`. */
17
+ export const {{Name}}Email = defineEmail<{{Name}}Copy>({
18
+ subject: (copy) => copy.heading,
19
+ body: (handle) => () => (
19
20
  <Email.Layout preview={handle.props.heading} title={handle.props.heading}>
20
21
  <Email.Heading>{handle.props.heading}</Email.Heading>
21
22
  <Email.Text>{handle.props.body}</Email.Text>
23
+ {/* TODO: lay out the rest of the email */}
22
24
  </Email.Layout>
23
- );
24
- }
25
-
26
- /** The {{title}} email, addressed to one recipient. */
27
- export class {{Name}}Email implements EmailContract {
28
- constructor(
29
- private recipient: string,
30
- private copy: {{Name}}Copy,
31
- ) {}
32
-
33
- get to() {
34
- return { email: this.recipient };
35
- }
36
-
37
- get subject() {
38
- return this.copy.heading;
39
- }
40
-
41
- body() {
42
- return <{{Name}}Body {...this.copy} />;
43
- }
44
- }
25
+ ),
26
+ });
@@ -1,16 +1,21 @@
1
1
  /**
2
2
  * The {{title}} email.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/data-and-background-work/send-email}
5
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
3
6
  */
4
7
 
8
+ import { render } from "@sdxc/mail";
5
9
  import { describe, expect, test } from "vitest";
6
10
 
7
11
  import { {{Name}}Email } from "{{specifier}}";
8
12
 
9
13
  describe("{{Name}}Email", () => {
10
- test("is addressed to its recipient under its heading", () => {
14
+ test("is addressed to its recipient under its heading", async () => {
11
15
  let email = new {{Name}}Email("ada@example.com", { heading: "Hello", body: "Welcome." });
12
16
 
13
17
  expect(email.to).toEqual({ email: "ada@example.com" });
14
18
  expect(email.subject).toBe("Hello");
19
+ expect((await render(email.body())).text).toContain("Welcome.");
15
20
  });
16
21
  });
@@ -1,5 +1,8 @@
1
1
  /**
2
2
  * The {{title}} factory tests seed rows with, through the same model the app writes with.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/data-and-background-work/data-models}
5
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/test-data}{{header}}
3
6
  */
4
7
 
5
8
  import { defineFactory } from "@sdxc/data-model/testing";
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * The app's one {{title}}: describe what it is configured with and who reads it. Build it
3
3
  * here, once, so every caller shares the same instance.
4
+ *
5
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/wire-the-router}{{header}}
4
6
  */
5
7
 
6
8
  /** The {{title}} every caller imports. */
@@ -1,5 +1,8 @@
1
1
  /**
2
2
  * The {{title}} initializer.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/wire-the-router}
5
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
3
6
  */
4
7
 
5
8
  import { describe, expect, test } from "vitest";
@@ -1,6 +1,9 @@
1
1
  /**
2
2
  * The scheduled {{title}} job: describe what each run sweeps or reports, and why on this
3
3
  * schedule.
4
+ *
5
+ * @see {@link https://sdxc.dev/docs/data-and-background-work/jobs-and-cron}
6
+ * @see {@link https://sdxc.dev/api/jobs}{{header}}
4
7
  */
5
8
 
6
9
  import { createJobHandler } from "@sdxc/jobs";
@@ -9,4 +12,5 @@ import jobs from "~/app/jobs";
9
12
 
10
13
  export default createJobHandler(jobs.{{path}}, async (ctx) => {
11
14
  ctx.log.set({ job: { name: "{{name}}" } });
15
+ // TODO: do the work, through the services on `ctx`
12
16
  });
package/stubs/job.stub CHANGED
@@ -1,5 +1,8 @@
1
1
  /**
2
2
  * The {{title}} job: describe what it changes and why it runs off the request.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/data-and-background-work/jobs-and-cron}
5
+ * @see {@link https://sdxc.dev/api/jobs}{{header}}
3
6
  */
4
7
 
5
8
  import { createJobHandler } from "@sdxc/jobs";
@@ -8,4 +11,5 @@ import jobs from "~/app/jobs";
8
11
 
9
12
  export default createJobHandler(jobs.{{path}}, async (ctx) => {
10
13
  ctx.log.set({ job: { name: "{{name}}" } });
14
+ // TODO: do the work, through the services on `ctx`
11
15
  });
@@ -1,5 +1,8 @@
1
1
  /**
2
2
  * The {{title}} job.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/data-and-background-work/jobs-and-cron}
5
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
3
6
  */
4
7
 
5
8
  import { describe, expect, test } from "vitest";
@@ -1,6 +1,9 @@
1
1
  /**
2
2
  * Answers the `{{uri}}` resource: describe what a read returns, and `null` for one that does
3
3
  * not exist.
4
+ *
5
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
6
+ * @see {@link https://sdxc.dev/api/mcp}{{header}}
4
7
  */
5
8
 
6
9
  import { createResource } from "@sdxc/mcp";
package/stubs/mcp.stub CHANGED
@@ -1,6 +1,9 @@
1
1
  /**
2
2
  * Answers the `{{tool}}` tool: describe what it returns, and what a model should do when it
3
3
  * finds nothing.
4
+ *
5
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
6
+ * @see {@link https://sdxc.dev/api/mcp}{{header}}
4
7
  */
5
8
 
6
9
  import { createTool } from "@sdxc/mcp";
@@ -1,5 +1,8 @@
1
1
  /**
2
2
  * The {{title}} MCP handler.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
5
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
3
6
  */
4
7
 
5
8
  import { describe, expect, test } from "vitest";
@@ -1,11 +1,16 @@
1
1
  /**
2
2
  * The {{title}} middleware: describe what it publishes on the context or which requests it
3
3
  * answers before the handler runs.
4
+ *
5
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/wire-the-router}{{header}}
4
6
  */
5
7
 
6
8
  import type { Middleware } from "remix/router";
7
9
 
8
10
  /** Creates the middleware; add it to a router's chain or to a route's `lazy()` guards. */
9
11
  export default function {{key}}(): Middleware {
10
- return (_context, next) => next();
12
+ return (_context, next) => {
13
+ // TODO: publish on the context, or answer before the handler runs
14
+ return next();
15
+ };
11
16
  }
@@ -1,5 +1,8 @@
1
1
  /**
2
2
  * The {{title}} middleware.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/wire-the-router}
5
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
3
6
  */
4
7
 
5
8
  import { describe, expect, test } from "vitest";
package/stubs/model.stub CHANGED
@@ -1,12 +1,16 @@
1
1
  /**
2
2
  * The {{title}} model and its table: describe what a row records and the rules that keep it
3
3
  * consistent. Keep the columns in step with the migration that creates the table.
4
+ *
5
+ * @see {@link https://sdxc.dev/docs/data-and-background-work/data-models}
6
+ * @see {@link https://sdxc.dev/api/data-model}{{header}}
4
7
  */
5
8
 
6
9
  import type { TableRow } from "remix/data-table";
7
10
 
8
11
  import { column as c, table } from "remix/data-table";
9
12
  import { createModel } from "@sdxc/data-model";
13
+ import { generateUUID } from "@sdxc/uuid/v7";
10
14
 
11
15
  /** The `{{snake}}` table, as its migration creates it. */
12
16
  export const {{key}}Table = table({
@@ -15,10 +19,15 @@ export const {{key}}Table = table({
15
19
  id: c.text().primaryKey(),
16
20
  created_at: c.integer(),
17
21
  updated_at: c.integer(),
22
+ // TODO: add the columns the migration creates
18
23
  },
24
+ /** `create` and `update` stamp `created_at` and `updated_at` with the database's clock. */
25
+ timestamps: true,
19
26
  });
20
27
 
21
28
  /** One {{title}} row as it reads back from the database. */
22
29
  export type {{Name}}Row = TableRow<typeof {{key}}Table>;
23
30
 
24
- export const {{Name}} = createModel({{key}}Table, {});
31
+ export const {{Name}} = createModel({{key}}Table, { generateId: generateUUID });
32
+
33
+ export default {{Name}};
@@ -1,5 +1,8 @@
1
1
  /**
2
2
  * The {{title}} model.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/data-and-background-work/data-models}
5
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
3
6
  */
4
7
 
5
8
  import { describe, expect, test } from "vitest";
package/stubs/policy.stub CHANGED
@@ -1,5 +1,8 @@
1
1
  /**
2
2
  * The {{title}} policy: describe who may do what, and why the rule sits here.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/identity-and-security/authorization}
5
+ * @see {@link https://sdxc.dev/api/authz}{{header}}
3
6
  */
4
7
 
5
8
  /** Who an authorization rule is asked about; `null` for an anonymous visitor. */
@@ -1,5 +1,8 @@
1
1
  /**
2
2
  * The {{title}} policy.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/identity-and-security/authorization}
5
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
3
6
  */
4
7
 
5
8
  import { describe, expect, test } from "vitest";
@@ -1,6 +1,9 @@
1
1
  /**
2
2
  * {{title}}: describe the rule this service carries out across models, and what a caller can
3
3
  * rely on when it succeeds.
4
+ *
5
+ * @see {@link https://sdxc.dev/docs/conventions/result-everywhere}
6
+ * @see {@link https://sdxc.dev/api/result}{{header}}
4
7
  */
5
8
 
6
9
  import type { Result } from "@sdxc/result";
@@ -9,5 +12,6 @@ import { success } from "@sdxc/result";
9
12
 
10
13
  /** Carries out {{title}}; a failure says why nothing changed. */
11
14
  export function {{key}}(): Result<void, Error> {
15
+ // TODO: carry out the rule across models
12
16
  return success(undefined);
13
17
  }
@@ -1,5 +1,8 @@
1
1
  /**
2
2
  * The {{title}} service.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/conventions/result-everywhere}
5
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
3
6
  */
4
7
 
5
8
  import { isSuccess } from "@sdxc/result";
@@ -1,5 +1,8 @@
1
1
  /**
2
2
  * The fields the {{title}} form submits, validated before anything trusts them.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/forms-and-params}
5
+ * @see {@link https://sdxc.dev/api/validate}{{header}}
3
6
  */
4
7
 
5
8
  import * as s from "remix/data-schema";
@@ -1,5 +1,8 @@
1
1
  /**
2
2
  * The {{title}} form's validation.
3
+ *
4
+ * @see {@link https://sdxc.dev/docs/building-remix-apps/forms-and-params}
5
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
3
6
  */
4
7
 
5
8
  import { isFailure, isSuccess } from "@sdxc/result";
@@ -0,0 +1,16 @@
1
+ # The Worker's secrets, with values for local development. `sdxc create` copies this file to
2
+ # `.dev.vars`, which `bun dev` reads; copy it yourself in a fresh clone. Set each one for
3
+ # production with `cf workers secrets update <NAME>` before the first deploy.
4
+
5
+ # Signs the session cookie. Use a long random value in production; changing it signs every
6
+ # visitor out.
7
+ SESSION_SECRET=development-session-secret
8
+
9
+ # Signs the honeypot fields every page form carries. Use a long random value in production.
10
+ HONEYPOT_SECRET=development-honeypot-secret
11
+
12
+ # Cloudflare Turnstile, which a form asks a visitor to pass. These are Cloudflare's test keys,
13
+ # which render a visible widget that always passes; create a widget in the Cloudflare dashboard
14
+ # for the production keys.
15
+ TURNSTILE_SITE_KEY=1x00000000000000000000AA
16
+ TURNSTILE_SECRET_KEY=1x0000000000000000000000000000000AA
@@ -1,57 +1,84 @@
1
1
  # App
2
2
 
3
- A Remix v3 Worker on Cloudflare, built on the `@sdxc/*` packages.
3
+ A Remix v3 app on Cloudflare Workers, built on the `@sdxc/*` packages.
4
4
 
5
- ## Develop
5
+ ## Start
6
6
 
7
7
  ```sh
8
8
  bun install
9
- bun cf:typegen
9
+ sdxc run typegen
10
+ sdxc run db migrate
10
11
  bun dev
11
12
  ```
12
13
 
13
- `bun cf:typegen` writes the Worker's types from `cloudflare.config.ts`; run it again after
14
- changing a binding.
14
+ In a fresh clone, copy `.env.example` to `.dev.vars` first. Run `sdxc run typegen` again
15
+ whenever you change a binding in `cloudflare.config.ts`.
15
16
 
16
- ## Add code
17
-
18
- `sdxc make <kind> <name>` writes a file of that kind where it belongs, with a test beside it,
19
- and adds any `@sdxc/*` package the file imports to `package.json`. The `sdxc` command comes
20
- from `npm add -g @sdxc/kit`:
17
+ ## Everyday commands
21
18
 
22
19
  ```sh
23
- sdxc make controller about
20
+ sdxc make controller about # a page, mapped in routes/web.ts
21
+ sdxc make controller api/keys --routes api # a JSON endpoint
22
+ sdxc make migration create-posts # then: sdxc run db migrate
23
+ sdxc make model post # then: ctx.models.post
24
24
  sdxc make job reports/daily --cron "0 8 * * *"
25
- sdxc make mcp search-posts
26
- ```
27
-
28
- A controller, a job and an MCP tool or resource are also declared and mapped, so they answer
29
- as soon as the command finishes.
25
+ sdxc make email welcome # then: ctx.email.later(new WelcomeEmail(to, copy))
26
+ sdxc make command users/grant # then: sdxc run users grant
30
27
 
31
- ## Run commands
32
-
33
- `bootstrap/cli.ts` is the app's own command line, for work that belongs to the app but to no
34
- request. It runs on Bun, outside the Worker, through its package script:
35
-
36
- ```sh
37
- bun run cli about
38
- bun run cli --help
28
+ sdxc run db console # SQL prompt over the local database
29
+ bun check # format, lint and types
30
+ bun test
39
31
  ```
40
32
 
41
- With `@sdxc/kit` installed globally, `sdxc run about` does the same from any folder in the
42
- project.
43
-
44
- `app/commands/index.ts` declares each command, and `sdxc make command users/grant` writes a
45
- new one with its test, declared and mapped.
46
-
47
- ## Check and deploy
33
+ `sdxc make` writes the file where it belongs with a test beside it, and maps it so it works
34
+ right away. `sdxc` comes with the project; `npm add -g @sdxc/kit` puts it on your path.
35
+
36
+ ## Where things go
37
+
38
+ | Folder | What lives there |
39
+ | ---------------------- | ----------------------------------------------------------- |
40
+ | `routes/` | URL contracts: `web.ts` for pages, `api.ts` for endpoints |
41
+ | `app/http/` | Controllers, middleware and input validators |
42
+ | `app/models/` | Database models, one per table |
43
+ | `app/jobs/` | Background jobs |
44
+ | `app/emails/` | Emails |
45
+ | `app/commands/` | Commands for `sdxc run` |
46
+ | `app/initializers/` | The one instance of each service: database, mailer, router… |
47
+ | `resources/` | Layouts, components and translations (`resources/locales/`) |
48
+ | `database/migrations/` | SQL migrations |
49
+ | `config/` | Site settings, security policy, security.txt, API problems |
50
+
51
+ ## What a handler gets
52
+
53
+ Every request, job and command can read:
54
+
55
+ - `ctx.db` and `ctx.models`: the D1 database and its models.
56
+ - `ctx.email` (requests and commands): sends email, `later()` after the response or
57
+ `await send()` before it. A command prints it instead of sending it.
58
+ - `ctx.log`, with a `trace_id` shared by a request and the jobs it starts.
59
+ - `ctx.timing`: adds entries to the `Server-Timing` header (requests only).
60
+ - `ctx.ip` and `ctx.userAgent` (requests only): the client's address, or `null`, and its parsed
61
+ `User-Agent` header.
62
+
63
+ Pages also get `ctx.session`, `ctx.intl` for translated copy, and forms protected by a
64
+ honeypot, Turnstile and a spam filter. Before a page loads a script, style, image or font from
65
+ another origin, add that origin to `config/security-policy.ts`. API errors use the catalog in
66
+ `config/problems.ts`.
67
+
68
+ ## Before the first deploy
69
+
70
+ 1. Create the resources and put their ids in `cloudflare.config.ts`:
71
+ `bunx cf d1 create <name>`, `bunx cf kv namespace create <name>`, and
72
+ `bunx cf queues create <name>` (the `name` in `package.json`).
73
+ 2. Set every secret in `.env.example` with `bunx cf workers secrets update <NAME>`.
74
+ 3. Edit `config/constants.ts`: your domain, site name and description, and a `MAIL_FROM`
75
+ address on a domain with Cloudflare Email Routing.
76
+ 4. Put your contact in `config/security-txt.ts`.
77
+ 5. Deploy:
48
78
 
49
79
  ```sh
50
80
  bun check
51
- bun test
52
81
  bun build
82
+ sdxc run db migrate --remote
53
83
  bun cf:deploy
54
84
  ```
55
-
56
- Create the job queue once before the first deploy: `bunx cf queues create <name>`, using the
57
- `name` in `package.json`.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * The `about` command, run through the app's command line as `bun run cli about` runs it.
2
+ * The `about` command, run through the app's command line as `sdxc run about` runs it.
3
3
  */
4
4
 
5
5
  import { invoke } from "@sdxc/cli/test";
@@ -0,0 +1,6 @@
1
+ /**
2
+ * `db console`: a `sql>` prompt over the local database, or one statement with `--execute`, for
3
+ * looking at the rows `bun dev` serves without writing a command for it.
4
+ */
5
+
6
+ export { dbHandler as default } from "@sdxc/database-middleware/cli";
@@ -0,0 +1,30 @@
1
+ /**
2
+ * `db migrate --remote`, which hands the chain to `cf d1 migrations apply` for the production
3
+ * database named in `cloudflare.config.ts`.
4
+ */
5
+
6
+ import { invoke } from "@sdxc/cli/test";
7
+ import { describe, expect, test } from "vitest";
8
+
9
+ import { cli } from "~/app/initializers/cli";
10
+
11
+ describe("db migrate", () => {
12
+ test("--remote applies the chain through cf to the configured database", async () => {
13
+ let run = await invoke(cli, ["db", "migrate", "--remote"], {
14
+ exec: {
15
+ "cf d1 migrations apply 00000000-0000-4000-8000-000000000000 --dir database/migrations": {
16
+ code: 0,
17
+ },
18
+ },
19
+ });
20
+
21
+ expect(run.code).toBe(0);
22
+ expect(run.exec[0]?.command.slice(0, 5)).toEqual([
23
+ "cf",
24
+ "d1",
25
+ "migrations",
26
+ "apply",
27
+ "00000000-0000-4000-8000-000000000000",
28
+ ]);
29
+ });
30
+ });