@sdxc/kit 2026.10.16 → 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.
- package/README.md +391 -20
- package/changelog.jsonl +1160 -1035
- package/config.schema.json +47 -1
- package/dist/author.d.ts +36 -0
- package/dist/author.js +107 -0
- package/dist/cli.js +12 -0
- package/dist/commands/create.js +9 -4
- package/dist/commands/deploy.d.ts +23 -0
- package/dist/commands/deploy.js +167 -0
- package/dist/commands/destroy.d.ts +22 -0
- package/dist/commands/destroy.js +92 -0
- package/dist/commands/doctor.d.ts +26 -0
- package/dist/commands/doctor.js +65 -0
- package/dist/commands/initializers.d.ts +16 -0
- package/dist/commands/initializers.js +23 -0
- package/dist/commands/make.d.ts +72 -6
- package/dist/commands/make.js +127 -19
- package/dist/commands/middleware.d.ts +16 -0
- package/dist/commands/middleware.js +21 -0
- package/dist/commands/models.d.ts +18 -0
- package/dist/commands/models.js +29 -0
- package/dist/commands/notes.d.ts +17 -0
- package/dist/commands/notes.js +27 -0
- package/dist/commands/routes.d.ts +25 -0
- package/dist/commands/routes.js +50 -0
- package/dist/commands/run.d.ts +18 -2
- package/dist/commands/run.js +35 -12
- package/dist/commands/scaffold.d.ts +29 -0
- package/dist/commands/scaffold.js +33 -0
- package/dist/commands/schedule.d.ts +16 -0
- package/dist/commands/schedule.js +21 -0
- package/dist/commands/stats.d.ts +16 -0
- package/dist/commands/stats.js +21 -0
- package/dist/commands/ui/update.d.ts +6 -3
- package/dist/commands/ui/update.js +24 -7
- package/dist/commands/upgrade.d.ts +3 -1
- package/dist/commands/upgrade.js +88 -23
- package/dist/config.d.ts +18 -0
- package/dist/config.js +28 -1
- package/dist/contract.d.ts +97 -3
- package/dist/contract.js +360 -26
- package/dist/create/templates.d.ts +8 -1
- package/dist/create/templates.js +26 -2
- package/dist/dependencies.d.ts +4 -1
- package/dist/dependencies.js +13 -1
- package/dist/deploy/plan.d.ts +49 -0
- package/dist/deploy/plan.js +68 -0
- package/dist/destroy.d.ts +40 -0
- package/dist/destroy.js +311 -0
- package/dist/doctor/check.d.ts +71 -0
- package/dist/doctor/check.js +53 -0
- package/dist/doctor/checks.d.ts +23 -0
- package/dist/doctor/checks.js +68 -0
- package/dist/doctor/define.d.ts +59 -0
- package/dist/doctor/define.js +16 -0
- package/dist/doctor/emails.d.ts +18 -0
- package/dist/doctor/emails.js +243 -0
- package/dist/doctor/env.d.ts +26 -0
- package/dist/doctor/env.js +88 -0
- package/dist/doctor/files.d.ts +14 -0
- package/dist/doctor/files.js +34 -0
- package/dist/doctor/fixture.d.ts +23 -0
- package/dist/doctor/fixture.js +46 -0
- package/dist/doctor/install.d.ts +17 -0
- package/dist/doctor/install.js +37 -0
- package/dist/doctor/installed.d.ts +11 -0
- package/dist/doctor/installed.js +79 -0
- package/dist/doctor/layout.d.ts +23 -0
- package/dist/doctor/layout.js +83 -0
- package/dist/doctor/migrations.d.ts +16 -0
- package/dist/doctor/migrations.js +96 -0
- package/dist/doctor/models.d.ts +13 -0
- package/dist/doctor/models.js +69 -0
- package/dist/doctor/pins.d.ts +15 -0
- package/dist/doctor/pins.js +79 -0
- package/dist/doctor/project.d.ts +58 -0
- package/dist/doctor/project.js +261 -0
- package/dist/doctor/remix.d.ts +14 -0
- package/dist/doctor/remix.js +44 -0
- package/dist/doctor/routes.d.ts +11 -0
- package/dist/doctor/routes.js +49 -0
- package/dist/doctor/typegen.d.ts +13 -0
- package/dist/doctor/typegen.js +57 -0
- package/dist/doctor/ui.d.ts +14 -0
- package/dist/doctor/ui.js +51 -0
- package/dist/init.d.ts +5 -3
- package/dist/init.js +15 -7
- package/dist/initializers.d.ts +25 -0
- package/dist/initializers.js +89 -0
- package/dist/lock.d.ts +2 -2
- package/dist/make/actions.d.ts +23 -0
- package/dist/make/actions.js +82 -0
- package/dist/make/declare.d.ts +32 -4
- package/dist/make/declare.js +405 -27
- package/dist/make/email.d.ts +23 -0
- package/dist/make/email.js +111 -0
- package/dist/make/from-fields.d.ts +30 -0
- package/dist/make/from-fields.js +162 -0
- package/dist/make/from-model.d.ts +39 -0
- package/dist/make/from-model.js +161 -0
- package/dist/make/generate.d.ts +16 -5
- package/dist/make/generate.js +43 -14
- package/dist/make/guard.d.ts +35 -0
- package/dist/make/guard.js +73 -0
- package/dist/make/imports.d.ts +6 -0
- package/dist/make/imports.js +19 -0
- package/dist/make/index.d.ts +47 -4
- package/dist/make/index.js +287 -25
- package/dist/make/kinds.d.ts +31 -3
- package/dist/make/kinds.js +82 -9
- package/dist/make/mount.d.ts +25 -0
- package/dist/make/mount.js +135 -0
- package/dist/make/schema-diff.d.ts +58 -0
- package/dist/make/schema-diff.js +609 -0
- package/dist/make/schema.d.ts +76 -0
- package/dist/make/schema.js +206 -0
- package/dist/make/stubs.d.ts +7 -5
- package/dist/make/stubs.js +16 -5
- package/dist/make/test.d.ts +43 -0
- package/dist/make/test.js +149 -0
- package/dist/make/trigger.d.ts +31 -0
- package/dist/make/trigger.js +163 -0
- package/dist/models.d.ts +42 -0
- package/dist/models.js +149 -0
- package/dist/notes.d.ts +28 -0
- package/dist/notes.js +66 -0
- package/dist/reference.d.ts +27 -0
- package/dist/reference.js +103 -0
- package/dist/releases.d.ts +9 -0
- package/dist/releases.js +29 -8
- package/dist/routes/chains.d.ts +34 -0
- package/dist/routes/chains.js +95 -0
- package/dist/routes/filter.d.ts +53 -0
- package/dist/routes/filter.js +147 -0
- package/dist/routes/format.d.ts +23 -0
- package/dist/routes/format.js +123 -0
- package/dist/routes/helpers.d.ts +21 -0
- package/dist/routes/helpers.js +165 -0
- package/dist/routes/index.d.ts +56 -0
- package/dist/routes/index.js +167 -0
- package/dist/routes/modules.d.ts +116 -0
- package/dist/routes/modules.js +235 -0
- package/dist/routes/parse.d.ts +161 -0
- package/dist/routes/parse.js +621 -0
- package/dist/routes/routers.d.ts +69 -0
- package/dist/routes/routers.js +327 -0
- package/dist/scaffold/fields.d.ts +129 -0
- package/dist/scaffold/fields.js +421 -0
- package/dist/scaffold/index.d.ts +37 -0
- package/dist/scaffold/index.js +613 -0
- package/dist/scaffold/render.d.ts +51 -0
- package/dist/scaffold/render.js +332 -0
- package/dist/schedule.d.ts +45 -0
- package/dist/schedule.js +124 -0
- package/dist/stats.d.ts +37 -0
- package/dist/stats.js +126 -0
- package/dist/testing.d.ts +12 -2
- package/dist/testing.js +22 -3
- package/dist/ui/commands.d.ts +10 -3
- package/dist/ui/commands.js +85 -15
- package/dist/ui/registry.d.ts +8 -8
- package/dist/upgrade/agents.d.ts +49 -0
- package/dist/upgrade/agents.js +110 -0
- package/dist/upgrade/changelog.d.ts +2 -2
- package/dist/upgrade/project.d.ts +4 -0
- package/dist/upgrade/project.js +11 -1
- package/dist/upgrade/prompt.d.ts +11 -0
- package/dist/upgrade/prompt.js +50 -7
- package/package.json +6 -4
- package/registry/ui/alert-dialog.json +2 -2
- package/registry/ui/attachment.json +2 -2
- package/registry/ui/calendar.json +2 -2
- package/registry/ui/color-picker.json +2 -2
- package/registry/ui/command.json +2 -2
- package/registry/ui/date-picker.json +2 -2
- package/registry/ui/date-range-picker.json +2 -2
- package/registry/ui/dialog.json +2 -2
- package/registry/ui/file-trigger.json +2 -2
- package/registry/ui/heading-scope.json +2 -2
- package/registry/ui/message-scroller.json +2 -2
- package/registry/ui/number-field.json +2 -2
- package/registry/ui/range-calendar.json +2 -2
- package/registry/ui/resizable.json +2 -2
- package/registry/ui/select.json +2 -2
- package/registry/ui/shared-element.json +2 -2
- package/stubs/command.stub +4 -0
- package/stubs/command.test.stub +3 -0
- package/stubs/controller.api.resource.stub +23 -0
- package/stubs/controller.api.stub +21 -0
- package/stubs/controller.api.test.stub +18 -0
- package/stubs/controller.form.stub +40 -10
- package/stubs/controller.form.test.stub +61 -0
- package/stubs/controller.resource.form.stub +67 -0
- package/stubs/controller.resource.stub +28 -0
- package/stubs/controller.stub +15 -8
- package/stubs/controller.test.stub +3 -0
- package/stubs/email.stub +15 -38
- package/stubs/email.test.stub +12 -7
- package/stubs/factory.stub +4 -1
- package/stubs/initializer.stub +2 -0
- package/stubs/initializer.test.stub +3 -0
- package/stubs/job.cron.stub +4 -0
- package/stubs/job.stub +4 -0
- package/stubs/job.test.stub +3 -0
- package/stubs/mcp.resource.stub +3 -0
- package/stubs/mcp.stub +3 -0
- package/stubs/mcp.test.stub +3 -0
- package/stubs/middleware.stub +6 -1
- package/stubs/middleware.test.stub +3 -0
- package/stubs/model.stub +12 -3
- package/stubs/model.test.stub +3 -0
- package/stubs/policy.test.stub +36 -7
- package/stubs/scaffold.api.create.stub +34 -0
- package/stubs/scaffold.api.create.test.stub +37 -0
- package/stubs/scaffold.api.destroy.stub +34 -0
- package/stubs/scaffold.api.destroy.test.stub +44 -0
- package/stubs/scaffold.api.index.stub +23 -0
- package/stubs/scaffold.api.index.test.stub +31 -0
- package/stubs/scaffold.api.show.stub +27 -0
- package/stubs/scaffold.api.show.test.stub +41 -0
- package/stubs/scaffold.api.update.stub +44 -0
- package/stubs/scaffold.api.update.test.stub +46 -0
- package/stubs/scaffold.command.create.stub +22 -0
- package/stubs/scaffold.command.create.test.stub +30 -0
- package/stubs/scaffold.command.delete.stub +20 -0
- package/stubs/scaffold.command.delete.test.stub +39 -0
- package/stubs/scaffold.command.list.stub +18 -0
- package/stubs/scaffold.command.list.test.stub +31 -0
- package/stubs/scaffold.command.show.stub +19 -0
- package/stubs/scaffold.command.show.test.stub +37 -0
- package/stubs/scaffold.controller.destroy.stub +35 -0
- package/stubs/scaffold.controller.destroy.test.stub +65 -0
- package/stubs/scaffold.controller.edit.stub +97 -0
- package/stubs/scaffold.controller.edit.test.stub +91 -0
- package/stubs/scaffold.controller.index.stub +55 -0
- package/stubs/scaffold.controller.index.test.stub +32 -0
- package/stubs/scaffold.controller.new.stub +70 -0
- package/stubs/scaffold.controller.new.test.stub +81 -0
- package/stubs/scaffold.controller.show.stub +59 -0
- package/stubs/scaffold.controller.show.test.stub +43 -0
- package/stubs/scaffold.factory.stub +16 -0
- package/stubs/scaffold.form.stub +75 -0
- package/stubs/scaffold.mcp.create.stub +24 -0
- package/stubs/scaffold.mcp.create.test.stub +36 -0
- package/stubs/scaffold.mcp.delete.stub +31 -0
- package/stubs/scaffold.mcp.delete.test.stub +43 -0
- package/stubs/scaffold.mcp.get.stub +21 -0
- package/stubs/scaffold.mcp.get.test.stub +31 -0
- package/stubs/scaffold.mcp.list.stub +23 -0
- package/stubs/scaffold.mcp.list.test.stub +25 -0
- package/stubs/scaffold.mcp.update.stub +37 -0
- package/stubs/scaffold.mcp.update.test.stub +40 -0
- package/stubs/scaffold.migration.stub +2 -0
- package/stubs/scaffold.model.stub +35 -0
- package/stubs/scaffold.model.test.stub +25 -0
- package/stubs/scaffold.test-database.stub +33 -0
- package/stubs/scaffold.test-mcp.stub +92 -0
- package/stubs/scaffold.validator.stub +18 -0
- package/stubs/scaffold.validator.test.stub +26 -0
- package/stubs/service.stub +4 -0
- package/stubs/service.test.stub +3 -0
- package/stubs/validator.stub +3 -0
- package/stubs/validator.test.stub +3 -0
- package/templates/app/.env.example +16 -0
- package/templates/app/AGENTS.md +163 -0
- package/templates/app/CLAUDE.md +1 -0
- package/templates/app/README.md +79 -37
- package/templates/app/app/commands/about.test.ts +1 -1
- package/templates/app/app/commands/console.test.ts +26 -0
- package/templates/app/app/commands/console.ts +19 -0
- package/templates/app/app/commands/db/console.ts +6 -0
- package/templates/app/app/commands/db/fresh.test.ts +57 -0
- package/templates/app/app/commands/db/fresh.ts +37 -0
- package/templates/app/app/commands/db/migrate.test.ts +46 -0
- package/templates/app/app/commands/db/migrate.ts +88 -0
- package/templates/app/app/commands/db/prepare.test.ts +39 -0
- package/templates/app/app/commands/db/prepare.ts +35 -0
- package/templates/app/app/commands/db/seed.test.ts +38 -0
- package/templates/app/app/commands/db/seed.ts +26 -0
- package/templates/app/app/commands/deploy.test.ts +34 -0
- package/templates/app/app/commands/deploy.ts +18 -0
- package/templates/app/app/commands/emails/preview.test.tsx +63 -0
- package/templates/app/app/commands/emails/preview.ts +24 -0
- package/templates/app/app/commands/index.ts +72 -1
- package/templates/app/app/commands/typegen.test.ts +17 -0
- package/templates/app/app/commands/typegen.ts +16 -0
- package/templates/app/app/emails/index.ts +10 -0
- package/templates/app/app/http/controllers/api/health.tsx +15 -0
- package/templates/app/app/http/controllers/api/not-found.tsx +16 -0
- package/templates/app/app/http/controllers/api/openapi.tsx +17 -0
- package/templates/app/app/http/controllers/home.tsx +21 -13
- package/templates/app/app/http/controllers/mcp.tsx +16 -0
- package/templates/app/app/http/controllers/not-found.tsx +23 -18
- package/templates/app/app/http/controllers/robots.test.ts +18 -0
- package/templates/app/app/http/controllers/robots.ts +24 -0
- package/templates/app/app/http/controllers/sitemap.test.ts +28 -0
- package/templates/app/app/http/controllers/sitemap.ts +25 -0
- package/templates/app/app/http/middleware/access.test.ts +43 -0
- package/templates/app/app/http/middleware/access.ts +41 -0
- package/templates/app/app/http/middleware/api.ts +19 -0
- package/templates/app/app/http/middleware/page.test.ts +43 -0
- package/templates/app/app/http/middleware/page.ts +51 -0
- package/templates/app/app/http/middleware/server-timing.test.ts +15 -0
- package/templates/app/app/http/middleware/server-timing.ts +42 -0
- package/templates/app/app/initializers/assets.ts +1 -1
- package/templates/app/app/initializers/captcha.ts +11 -0
- package/templates/app/app/initializers/cli.ts +68 -2
- package/templates/app/app/initializers/database.test.ts +18 -0
- package/templates/app/app/initializers/database.ts +18 -0
- package/templates/app/app/initializers/dispatcher.ts +26 -0
- package/templates/app/app/initializers/honeypot.ts +11 -0
- package/templates/app/app/initializers/i18n.test.ts +16 -0
- package/templates/app/app/initializers/i18n.ts +28 -0
- package/templates/app/app/initializers/local-database.test.ts +33 -0
- package/templates/app/app/initializers/local-database.ts +53 -0
- package/templates/app/app/initializers/mail-transport.test.ts +27 -0
- package/templates/app/app/initializers/mail-transport.ts +26 -0
- package/templates/app/app/initializers/mail.test.ts +27 -0
- package/templates/app/app/initializers/mail.ts +19 -0
- package/templates/app/app/initializers/mcp.ts +1 -1
- package/templates/app/app/initializers/openapi.ts +21 -0
- package/templates/app/app/initializers/router.test.ts +68 -0
- package/templates/app/app/initializers/router.tsx +81 -25
- package/templates/app/app/initializers/seo.ts +15 -0
- package/templates/app/app/initializers/session.test.ts +34 -0
- package/templates/app/app/initializers/session.ts +29 -0
- package/templates/app/app/initializers/spam.ts +10 -0
- package/templates/app/app/jobs/email.test.tsx +70 -0
- package/templates/app/app/jobs/email.ts +16 -0
- package/templates/app/app/jobs/index.ts +6 -2
- package/templates/app/app/models/index.ts +10 -0
- package/templates/app/app/policies/abilities.ts +13 -0
- package/templates/app/app/policies/policy.test.ts +15 -0
- package/templates/app/app/policies/policy.ts +30 -0
- package/templates/app/app/services/commands.ts +30 -0
- package/templates/app/app/services/migrations.test.ts +92 -0
- package/templates/app/app/services/migrations.ts +117 -0
- package/templates/app/app/test/cloudflare-workers.ts +25 -0
- package/templates/app/app/test/local-database.ts +67 -0
- package/templates/app/app/test/session.ts +28 -0
- package/templates/app/bootstrap/cli.ts +1 -1
- package/templates/app/bootstrap/console.ts +13 -0
- package/templates/app/bootstrap/worker.ts +4 -4
- package/templates/app/cloudflare.config.ts +30 -2
- package/templates/app/config/constants.ts +19 -0
- package/templates/app/config/problems.ts +24 -0
- package/templates/app/config/security-policy.ts +34 -0
- package/templates/app/config/security-txt.ts +28 -0
- package/templates/app/database/migrations/.gitkeep +0 -0
- package/templates/app/database/seed.ts +12 -0
- package/templates/app/package.json +37 -6
- package/templates/app/resources/layouts/document.tsx +28 -7
- package/templates/app/resources/locales/en.json +10 -0
- package/templates/app/routes/api.ts +29 -0
- package/templates/app/routes/web.ts +6 -4
- package/templates/app/vite.config.ts +4 -0
- package/stubs/policy.stub +0 -13
- package/templates/app/config/router-context.d.ts +0 -20
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `emails preview`, run through the app's command line with a sample email mapped onto the
|
|
3
|
+
* registry for this file alone.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { invoke } from "@sdxc/cli/test";
|
|
7
|
+
import { createEmail, Email, email, emails } from "@sdxc/mail";
|
|
8
|
+
import * as s from "remix/data-schema";
|
|
9
|
+
import { beforeAll, describe, expect, test } from "vitest";
|
|
10
|
+
|
|
11
|
+
import { cli } from "~/app/initializers/cli";
|
|
12
|
+
import { mail } from "~/app/initializers/mail";
|
|
13
|
+
|
|
14
|
+
/** The emails this test maps: one with a preview, one without. */
|
|
15
|
+
const SAMPLES = emails({
|
|
16
|
+
sample: email({ props: s.object({ heading: s.string() }) }),
|
|
17
|
+
bare: email({ props: s.object({}) }),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
beforeAll(() => {
|
|
21
|
+
mail.map(
|
|
22
|
+
SAMPLES.sample,
|
|
23
|
+
createEmail(SAMPLES.sample, {
|
|
24
|
+
subject: ({ heading }) => heading,
|
|
25
|
+
body: (handle) => () => (
|
|
26
|
+
<Email.Layout lang={handle.props.locale} title={handle.props.heading}>
|
|
27
|
+
<Email.Text>Rendered in {handle.props.locale}</Email.Text>
|
|
28
|
+
</Email.Layout>
|
|
29
|
+
),
|
|
30
|
+
preview: () => ({ heading: "Sample heading" }),
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
mail.map(
|
|
34
|
+
SAMPLES.bare,
|
|
35
|
+
createEmail(SAMPLES.bare, { subject: () => "Bare", body: () => () => <p>Bare</p> }),
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("emails preview", () => {
|
|
40
|
+
test("prints the subject and the text part rendered from the preview props", async () => {
|
|
41
|
+
let run = await invoke(cli, ["emails", "preview", "sample", "--locale", "en"]);
|
|
42
|
+
|
|
43
|
+
expect(run.code).toBe(0);
|
|
44
|
+
expect(run.stdout).toMatch(/^Subject: Sample heading\n\n[^]*Rendered in en\n$/);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("--html prints the HTML document", async () => {
|
|
48
|
+
let run = await invoke(cli, ["emails", "preview", "sample", "--html"]);
|
|
49
|
+
|
|
50
|
+
expect(run.code).toBe(0);
|
|
51
|
+
expect(run.stdout).toContain('lang="en"');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("fails for an email without a preview, and for one nothing maps", async () => {
|
|
55
|
+
let bare = await invoke(cli, ["emails", "preview", "bare"]);
|
|
56
|
+
let unmapped = await invoke(cli, ["emails", "preview", "billing.receipt"]);
|
|
57
|
+
|
|
58
|
+
expect(bare.code).toBe(1);
|
|
59
|
+
expect(bare.stderr).toContain("has no preview");
|
|
60
|
+
expect(unmapped.code).toBe(1);
|
|
61
|
+
expect(unmapped.stderr).toContain('"billing.receipt"');
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `emails preview <name>`: renders a declared email from its template's `preview()` props, in
|
|
3
|
+
* the language `--locale` names, and prints it as the print transport shows a sent message, so
|
|
4
|
+
* copy is checked from a terminal without sending anything or starting the dev server.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createCommand, success } from "@sdxc/cli";
|
|
8
|
+
import { isFailure } from "@sdxc/result";
|
|
9
|
+
|
|
10
|
+
import commandset from "~/app/commands";
|
|
11
|
+
import { mail } from "~/app/initializers/mail";
|
|
12
|
+
|
|
13
|
+
/** Prints the subject, a blank line and the text part, or the HTML document with `--html`. */
|
|
14
|
+
export default createCommand(commandset.emails.preview, async (ctx) => {
|
|
15
|
+
let rendered = await mail.preview(ctx.arguments.name, ctx.flags.locale);
|
|
16
|
+
if (isFailure(rendered)) return rendered;
|
|
17
|
+
if (ctx.flags.html) {
|
|
18
|
+
ctx.stdout(`${rendered.data.html}\n`);
|
|
19
|
+
return success();
|
|
20
|
+
}
|
|
21
|
+
let text = rendered.data.text.endsWith("\n") ? rendered.data.text : `${rendered.data.text}\n`;
|
|
22
|
+
ctx.stdout(`Subject: ${rendered.data.subject}\n\n${text}`);
|
|
23
|
+
return success();
|
|
24
|
+
});
|
|
@@ -4,7 +4,14 @@
|
|
|
4
4
|
* and a folder of handlers is a `commands({ … })` group here.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { command, commands, flag } from "@sdxc/cli";
|
|
7
|
+
import { argument, command, commands, flag } from "@sdxc/cli";
|
|
8
|
+
import { DB_COMMAND } from "@sdxc/database-middleware/cli";
|
|
9
|
+
import * as s from "remix/data-schema";
|
|
10
|
+
|
|
11
|
+
/** Declared on the commands that change only the local database, so `--remote` is refused by name. */
|
|
12
|
+
const LOCAL_ONLY = flag.boolean("remote", {
|
|
13
|
+
description: "Refused: this command changes only the local database",
|
|
14
|
+
});
|
|
8
15
|
|
|
9
16
|
/** The app's commands; `sdxc make command <name>` declares one here and maps its handler. */
|
|
10
17
|
export default commands({
|
|
@@ -12,4 +19,68 @@ export default commands({
|
|
|
12
19
|
description: "Print the app's name, its @sdxc/cli and remix versions, and the runtime",
|
|
13
20
|
flags: { json: flag.boolean("json", { description: "Print it as JSON" }) },
|
|
14
21
|
}),
|
|
22
|
+
console: command({
|
|
23
|
+
description: "Open a REPL with the local database as db and the app's models as models",
|
|
24
|
+
examples: ["console"],
|
|
25
|
+
}),
|
|
26
|
+
db: commands({
|
|
27
|
+
console: DB_COMMAND,
|
|
28
|
+
fresh: command({
|
|
29
|
+
description: "Drop every local table and apply every migration again",
|
|
30
|
+
flags: {
|
|
31
|
+
seed: flag.boolean("seed", { description: "Run db seed afterwards" }),
|
|
32
|
+
remote: LOCAL_ONLY,
|
|
33
|
+
},
|
|
34
|
+
examples: ["db fresh", "db fresh --seed"],
|
|
35
|
+
}),
|
|
36
|
+
migrate: command({
|
|
37
|
+
description: "Apply the migrations in database/migrations the database lacks",
|
|
38
|
+
flags: {
|
|
39
|
+
remote: flag.boolean("remote", {
|
|
40
|
+
description: "Apply them to the production database instead of the local copy",
|
|
41
|
+
}),
|
|
42
|
+
pending: flag.boolean("pending", {
|
|
43
|
+
description: "Print the ones the database lacks, one per line, and apply nothing",
|
|
44
|
+
}),
|
|
45
|
+
},
|
|
46
|
+
examples: ["db migrate", "db migrate --remote", "db migrate --remote --pending"],
|
|
47
|
+
}),
|
|
48
|
+
prepare: command({
|
|
49
|
+
description: "Apply pending local migrations, and seed a database that had none applied",
|
|
50
|
+
flags: { remote: LOCAL_ONLY },
|
|
51
|
+
examples: ["db prepare"],
|
|
52
|
+
}),
|
|
53
|
+
seed: command({
|
|
54
|
+
description: "Run the steps in database/seed.ts on the local database",
|
|
55
|
+
flags: { remote: LOCAL_ONLY },
|
|
56
|
+
examples: ["db seed"],
|
|
57
|
+
}),
|
|
58
|
+
}),
|
|
59
|
+
emails: commands({
|
|
60
|
+
preview: command({
|
|
61
|
+
description: "Print a declared email rendered from its template's preview() props",
|
|
62
|
+
arguments: {
|
|
63
|
+
name: argument(s.string(), { description: "The email's name, such as teams.invite" }),
|
|
64
|
+
},
|
|
65
|
+
flags: {
|
|
66
|
+
locale: flag.string("locale", {
|
|
67
|
+
description: "The language to render it in; the fallback language without one",
|
|
68
|
+
}),
|
|
69
|
+
html: flag.boolean("html", { description: "Print the HTML part instead of the text" }),
|
|
70
|
+
},
|
|
71
|
+
examples: ["emails preview teams.invite", "emails preview teams.invite --locale es --html"],
|
|
72
|
+
}),
|
|
73
|
+
}),
|
|
74
|
+
deploy: command({
|
|
75
|
+
description: "Upload the build in .cloudflare/output to Cloudflare with cf deploy",
|
|
76
|
+
flags: {
|
|
77
|
+
dryRun: flag.boolean("dry-run", {
|
|
78
|
+
description: "Run cf's checks on the build and upload nothing",
|
|
79
|
+
}),
|
|
80
|
+
},
|
|
81
|
+
examples: ["deploy", "deploy --dry-run"],
|
|
82
|
+
}),
|
|
83
|
+
typegen: command({
|
|
84
|
+
description: "Write the Worker's env types from cloudflare.config.ts",
|
|
85
|
+
}),
|
|
15
86
|
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `typegen` command, which hands type generation to `cf` in the project's root.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { invoke } from "@sdxc/cli/test";
|
|
6
|
+
import { describe, expect, test } from "vitest";
|
|
7
|
+
|
|
8
|
+
import { cli } from "~/app/initializers/cli";
|
|
9
|
+
|
|
10
|
+
describe("typegen", () => {
|
|
11
|
+
test("generates the Worker's types with cf", async () => {
|
|
12
|
+
let run = await invoke(cli, ["typegen"], { exec: { "cf workers types": { code: 0 } } });
|
|
13
|
+
|
|
14
|
+
expect(run.code).toBe(0);
|
|
15
|
+
expect(run.exec.map((call) => call.command)).toEqual([["cf", "workers", "types"]]);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `typegen`: writes the Worker's `env` types from `cloudflare.config.ts` through `cf workers
|
|
3
|
+
* types`. Run it after adding or changing a binding or a secret, so `env` and every handler
|
|
4
|
+
* type check against what the Worker receives. Safe to run twice.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createCommand, success } from "@sdxc/cli";
|
|
8
|
+
import { isFailure } from "@sdxc/result";
|
|
9
|
+
|
|
10
|
+
import commandset from "~/app/commands";
|
|
11
|
+
import { PROJECT_ROOT } from "~/app/initializers/local-database";
|
|
12
|
+
|
|
13
|
+
export default createCommand(commandset.typegen, async (ctx) => {
|
|
14
|
+
let generated = await ctx.exec(["cf", "workers", "types"], { cwd: PROJECT_ROOT });
|
|
15
|
+
return isFailure(generated) ? generated : success();
|
|
16
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every email the app sends, declared in one map with the schema of its props. Importing it
|
|
3
|
+
* costs the schemas alone, so a controller or a job sends an email without loading the
|
|
4
|
+
* template that renders it.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { emails } from "@sdxc/mail";
|
|
8
|
+
|
|
9
|
+
/** The app's emails; `sdxc make email <name>` declares one here and maps its template. */
|
|
10
|
+
export default emails({});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The health check: it answers as soon as the Worker runs, so a monitor tells a deploy that
|
|
3
|
+
* serves from one that fails to start.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { createAction } from "remix/router";
|
|
7
|
+
|
|
8
|
+
import api from "~/app/http/middleware/api";
|
|
9
|
+
import routes from "~/routes/api";
|
|
10
|
+
|
|
11
|
+
export default createAction(routes.api.health, {
|
|
12
|
+
middleware: api,
|
|
13
|
+
/** GET /api/health, answered as the `health` operation documents it. */
|
|
14
|
+
handler: () => Response.json({ status: "ok" }),
|
|
15
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The API's 404: an unknown path under `/api` answers an RFC 9457 problem document, which a
|
|
3
|
+
* client parses the same way as every other error the API returns.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { createAction } from "remix/router";
|
|
7
|
+
|
|
8
|
+
import api from "~/app/http/middleware/api";
|
|
9
|
+
import problems from "~/config/problems";
|
|
10
|
+
import routes from "~/routes/api";
|
|
11
|
+
|
|
12
|
+
export default createAction(routes.api.notFound, {
|
|
13
|
+
middleware: api,
|
|
14
|
+
/** Any method on an unknown `/api` path answers the `notFound` problem. */
|
|
15
|
+
handler: () => problems.notFound(),
|
|
16
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The API's OpenAPI document, served as JSON or, for `?format=yaml`, YAML. It is built from
|
|
3
|
+
* the operations in `routes/api.ts`, so it describes exactly what the handlers parse.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { openapiHandler } from "@sdxc/openapi/router";
|
|
7
|
+
import { createAction } from "remix/router";
|
|
8
|
+
|
|
9
|
+
import api from "~/app/http/middleware/api";
|
|
10
|
+
import { createApiDocument } from "~/app/initializers/openapi";
|
|
11
|
+
import routes from "~/routes/api";
|
|
12
|
+
|
|
13
|
+
export default createAction(routes.api.openapi, {
|
|
14
|
+
middleware: api,
|
|
15
|
+
/** GET /api/openapi.json, naming the origin the request reached as the API's server. */
|
|
16
|
+
handler: (ctx) => openapiHandler(() => createApiDocument(ctx.url.origin).build())(ctx),
|
|
17
|
+
});
|
|
@@ -1,22 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The home page, the first thing a visitor sees. It renders through the document layout,
|
|
3
|
-
* which links the stylesheets and client entry the
|
|
3
|
+
* which links the stylesheets and client entry the page middleware resolved.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import { Trans } from "@sdxc/i18n/ui";
|
|
6
7
|
import { createAction } from "remix/router";
|
|
7
8
|
|
|
9
|
+
import page from "~/app/http/middleware/page";
|
|
8
10
|
import DocumentLayout from "~/resources/layouts/document";
|
|
9
11
|
import routes from "~/routes/web";
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
<
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
)
|
|
13
|
+
export default createAction(routes.home, {
|
|
14
|
+
middleware: page,
|
|
15
|
+
/** GET / — the welcome page. */
|
|
16
|
+
handler: (ctx) =>
|
|
17
|
+
ctx.render(
|
|
18
|
+
<DocumentLayout
|
|
19
|
+
assets={ctx.assets}
|
|
20
|
+
intl={ctx.intl}
|
|
21
|
+
url={ctx.url}
|
|
22
|
+
title={ctx.intl.t("home.title")}
|
|
23
|
+
>
|
|
24
|
+
<h1>{ctx.intl.t("home.title")}</h1>
|
|
25
|
+
<p>
|
|
26
|
+
<Trans i18nKey="home.body" components={{ file: <code />, command: <code /> }} />
|
|
27
|
+
</p>
|
|
28
|
+
</DocumentLayout>,
|
|
29
|
+
),
|
|
30
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The MCP endpoint an agent connects to. It forwards each request to the app's MCP server,
|
|
3
|
+
* whose tools and resources live in `app/mcp/`.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { createAction } from "remix/router";
|
|
7
|
+
|
|
8
|
+
import api from "~/app/http/middleware/api";
|
|
9
|
+
import mcp from "~/app/initializers/mcp";
|
|
10
|
+
import routes from "~/routes/api";
|
|
11
|
+
|
|
12
|
+
export default createAction(routes.mcp, {
|
|
13
|
+
middleware: api,
|
|
14
|
+
/** POST /mcp, answered over Streamable HTTP. */
|
|
15
|
+
handler: (ctx) => mcp.fetch(ctx),
|
|
16
|
+
});
|
|
@@ -1,25 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The
|
|
3
|
-
*
|
|
2
|
+
* The 404 page for any path no other route claims, with a way back home. An unknown path
|
|
3
|
+
* under `/api` answers a problem document instead, from its own route.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import
|
|
6
|
+
import { createAction } from "remix/router";
|
|
7
7
|
|
|
8
|
+
import page from "~/app/http/middleware/page";
|
|
8
9
|
import DocumentLayout from "~/resources/layouts/document";
|
|
9
10
|
import routes from "~/routes/web";
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
12
|
+
export default createAction(routes.notFound, {
|
|
13
|
+
middleware: page,
|
|
14
|
+
/** Any method on an unknown path renders the 404 document. */
|
|
15
|
+
handler: (ctx) =>
|
|
16
|
+
ctx.render(
|
|
17
|
+
<DocumentLayout
|
|
18
|
+
assets={ctx.assets}
|
|
19
|
+
intl={ctx.intl}
|
|
20
|
+
url={ctx.url}
|
|
21
|
+
title={ctx.intl.t("notFound.title")}
|
|
22
|
+
>
|
|
23
|
+
<h1>{ctx.intl.t("notFound.title")}</h1>
|
|
24
|
+
<p>
|
|
25
|
+
<a href={routes.home.href()}>{ctx.intl.t("notFound.back")}</a>
|
|
26
|
+
</p>
|
|
27
|
+
</DocumentLayout>,
|
|
28
|
+
{ status: 404 },
|
|
29
|
+
),
|
|
30
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/robots.txt`, fetched through the app's router.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, expect, test } from "vitest";
|
|
6
|
+
|
|
7
|
+
import { createWebRouter } from "~/app/initializers/router";
|
|
8
|
+
|
|
9
|
+
describe("robots", () => {
|
|
10
|
+
test("lets every crawler in and points it at the sitemap on SITE_URL", async () => {
|
|
11
|
+
let response = await createWebRouter().fetch("https://preview.example.net/robots.txt");
|
|
12
|
+
|
|
13
|
+
let body = await response.text();
|
|
14
|
+
expect(response.headers.get("content-type")).toBe("text/plain; charset=utf-8");
|
|
15
|
+
expect(body).toContain("User-agent: *");
|
|
16
|
+
expect(body).toContain("Sitemap: https://example.com/sitemap.xml");
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/robots.txt`: every crawler may read every page, and the sitemap lists them. Add a rule here
|
|
3
|
+
* before a path should stay out of search results.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { text } from "@sdxc/http/response";
|
|
7
|
+
import { stringify } from "@sdxc/robots";
|
|
8
|
+
import { createAction } from "remix/router";
|
|
9
|
+
|
|
10
|
+
import { seo } from "~/app/initializers/seo";
|
|
11
|
+
import routes from "~/routes/web";
|
|
12
|
+
|
|
13
|
+
export default createAction(routes.robots, {
|
|
14
|
+
/** GET /robots.txt, cached for a day since it changes only with a deploy. */
|
|
15
|
+
handler: () =>
|
|
16
|
+
text(
|
|
17
|
+
stringify({
|
|
18
|
+
groups: [{ userAgents: ["*"], rules: [{ allow: true, pattern: "/" }] }],
|
|
19
|
+
sitemaps: [seo.absolute(routes.sitemap.href())],
|
|
20
|
+
records: [],
|
|
21
|
+
}),
|
|
22
|
+
{ headers: { "Cache-Control": "public, max-age=86400" } },
|
|
23
|
+
),
|
|
24
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/sitemap.xml`, fetched through the app's router.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, expect, test } from "vitest";
|
|
6
|
+
|
|
7
|
+
import { createWebRouter } from "~/app/initializers/router";
|
|
8
|
+
|
|
9
|
+
describe("sitemap", () => {
|
|
10
|
+
test("lists the home page at its canonical URL, cached for an hour", async () => {
|
|
11
|
+
let response = await createWebRouter().fetch("https://preview.example.net/sitemap.xml");
|
|
12
|
+
|
|
13
|
+
expect(response.headers.get("content-type")).toBe("text/xml; charset=utf-8");
|
|
14
|
+
expect(response.headers.get("cache-control")).toBe("public, max-age=3600");
|
|
15
|
+
let body = await response.text();
|
|
16
|
+
expect(body).toContain("<loc>https://example.com/</loc>");
|
|
17
|
+
expect(body).toContain("<priority>1</priority>");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("leaves out robots.txt and the sitemap itself", async () => {
|
|
21
|
+
let response = await createWebRouter().fetch("https://preview.example.net/sitemap.xml");
|
|
22
|
+
let body = await response.text();
|
|
23
|
+
|
|
24
|
+
expect(body).not.toContain("robots.txt");
|
|
25
|
+
expect(body).not.toContain("sitemap.xml");
|
|
26
|
+
expect(body).not.toContain("preview.example.net");
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/sitemap.xml`: every public page, at its canonical URL, for search engines to find. Every
|
|
3
|
+
* GET route in `routes/web.ts` needs a decision here, so a new route fails to typecheck until
|
|
4
|
+
* it is listed or excluded.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { xml } from "@sdxc/http/response";
|
|
8
|
+
import { fromRoutes } from "@sdxc/sitemap/remix";
|
|
9
|
+
import { createAction } from "remix/router";
|
|
10
|
+
|
|
11
|
+
import { seo } from "~/app/initializers/seo";
|
|
12
|
+
import routes from "~/routes/web";
|
|
13
|
+
|
|
14
|
+
export default createAction(routes.sitemap, {
|
|
15
|
+
/** GET /sitemap.xml, cached for an hour, with URLs on `SITE_URL` whichever host served it. */
|
|
16
|
+
handler: () => {
|
|
17
|
+
let sitemap = fromRoutes(routes, seo.baseUrl, {
|
|
18
|
+
home: { priority: 1 },
|
|
19
|
+
robots: false,
|
|
20
|
+
sitemap: false,
|
|
21
|
+
notFound: false,
|
|
22
|
+
});
|
|
23
|
+
return xml(sitemap.toString(), { headers: { "Cache-Control": "public, max-age=3600" } });
|
|
24
|
+
},
|
|
25
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page chain's principal: a guest holds no role and a visitor signed in through the session
|
|
3
|
+
* holds `member`, read through a router running the same session and access middleware.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { session } from "remix/middleware/session";
|
|
7
|
+
import { createRouter } from "remix/router";
|
|
8
|
+
import { describe, expect, test } from "vitest";
|
|
9
|
+
|
|
10
|
+
import access, { rolesOf, signedIn } from "~/app/http/middleware/access";
|
|
11
|
+
import { sessionCookie, sessionStorage } from "~/app/initializers/session";
|
|
12
|
+
import { signIn } from "~/app/test/session";
|
|
13
|
+
|
|
14
|
+
/** A router answering the principal it bound: its roles, its actor, and whether access is bound. */
|
|
15
|
+
function createProbe() {
|
|
16
|
+
let router = createRouter({ middleware: [session(sessionCookie, sessionStorage), access] });
|
|
17
|
+
router.get("/", (ctx) =>
|
|
18
|
+
Response.json({
|
|
19
|
+
roles: rolesOf(ctx.session),
|
|
20
|
+
actor: signedIn(ctx.session) ?? null,
|
|
21
|
+
bound: ctx.access !== undefined,
|
|
22
|
+
}),
|
|
23
|
+
);
|
|
24
|
+
return router;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe("access", () => {
|
|
28
|
+
test("binds a visitor without a session as a guest", async () => {
|
|
29
|
+
let response = await createProbe().fetch("https://example.com/");
|
|
30
|
+
|
|
31
|
+
expect(await response.json()).toEqual({ roles: [], actor: null, bound: true });
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("binds a signed-in visitor as a member", async () => {
|
|
35
|
+
let cookie = await signIn({ id: "42" });
|
|
36
|
+
|
|
37
|
+
let response = await createProbe().fetch(
|
|
38
|
+
new Request("https://example.com/", { headers: { cookie } }),
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
expect(await response.json()).toEqual({ roles: ["member"], actor: { id: "42" }, bound: true });
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Binds the request's principal as `ctx.access`. A visitor whose session holds an `actor` is a
|
|
3
|
+
* `member` and anyone else a guest, so signing someone in is `ctx.session.set("actor", { id })`.
|
|
4
|
+
* Every ability loads up front, so a handler checks with `ctx.access.can(…)` synchronously.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { access } from "@sdxc/authz/middleware/router";
|
|
8
|
+
import { Session } from "remix/session";
|
|
9
|
+
|
|
10
|
+
import type { Actor } from "~/app/policies/policy";
|
|
11
|
+
|
|
12
|
+
import abilities from "~/app/policies/abilities";
|
|
13
|
+
import policy from "~/app/policies/policy";
|
|
14
|
+
|
|
15
|
+
/** The session key a sign-in stores the visitor under, and `signedIn` reads back. */
|
|
16
|
+
export const ACTOR_KEY = "actor";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The signed-in visitor the session holds, or `undefined` for a guest. A request without a
|
|
20
|
+
* session, or one whose stored value has no string `id`, reads as a guest.
|
|
21
|
+
*/
|
|
22
|
+
export function signedIn(session: Session | undefined): Actor | undefined {
|
|
23
|
+
let value: unknown = session?.get(ACTOR_KEY);
|
|
24
|
+
if (typeof value !== "object" || value === null || !("id" in value)) return undefined;
|
|
25
|
+
return typeof value.id === "string" ? { id: value.id } : undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The roles the session's visitor holds: `member` once signed in, none for a guest. */
|
|
29
|
+
export function rolesOf(session: Session | undefined): string[] {
|
|
30
|
+
return signedIn(session) === undefined ? [] : ["member"];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The page chain's binding, after the session middleware, with the visitor as the `actor` fact
|
|
35
|
+
* conditions read.
|
|
36
|
+
*/
|
|
37
|
+
export default access(policy, {
|
|
38
|
+
roles: (ctx) => rolesOf(ctx.get(Session)),
|
|
39
|
+
facts: { actor: (ctx) => signedIn(ctx.get(Session)) },
|
|
40
|
+
load: [abilities],
|
|
41
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The middleware every API endpoint declares: CORS, so a browser on another origin can call
|
|
3
|
+
* it, and the caller's access. An endpoint reads its own body, as JSON or a form, so it is left
|
|
4
|
+
* unparsed here.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { access } from "@sdxc/authz/middleware/router";
|
|
8
|
+
import { cors } from "remix/middleware/cors";
|
|
9
|
+
import { createMiddleware } from "remix/router";
|
|
10
|
+
|
|
11
|
+
import abilities from "~/app/policies/abilities";
|
|
12
|
+
import policy from "~/app/policies/policy";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Answers CORS preflights, adds the CORS headers to every endpoint's response, and binds every
|
|
16
|
+
* caller, an MCP client included, as a guest with every ability loaded. Once the API
|
|
17
|
+
* authenticates its callers, pass their roles and `actor` to `access` here.
|
|
18
|
+
*/
|
|
19
|
+
export default createMiddleware(cors(), access(policy, { load: [abilities] }));
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page chain's security headers and language, checked through the home page, since every
|
|
3
|
+
* page declares the same chain and so sends the same policy. The home controller loads with the
|
|
4
|
+
* file, so the cold import of the chain and its document stays outside each test's time limit.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, expect, test } from "vitest";
|
|
8
|
+
|
|
9
|
+
import { createWebRouter } from "~/app/initializers/router";
|
|
10
|
+
|
|
11
|
+
import "~/app/http/controllers/home";
|
|
12
|
+
|
|
13
|
+
describe("page", () => {
|
|
14
|
+
test("sends the Content-Security-Policy with a page", async () => {
|
|
15
|
+
let response = await createWebRouter().fetch("https://example.com/");
|
|
16
|
+
|
|
17
|
+
expect(response.headers.get("content-security-policy")).toContain("default-src 'self'");
|
|
18
|
+
expect(response.headers.get("x-frame-options")).toBe("DENY");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("advertises no nonce when the page renders no inline script", async () => {
|
|
22
|
+
let response = await createWebRouter().fetch("https://example.com/");
|
|
23
|
+
|
|
24
|
+
expect(response.headers.get("content-security-policy")).not.toContain("'nonce-");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("renders in the language the visitor asks for, or the fallback", async () => {
|
|
28
|
+
let response = await createWebRouter().fetch(
|
|
29
|
+
new Request("https://example.com/", { headers: { "accept-language": "fr-FR, en;q=0.5" } }),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
let html = await response.text();
|
|
33
|
+
expect(html).toContain('<html lang="en"');
|
|
34
|
+
expect(html).toContain("<h1>Welcome</h1>");
|
|
35
|
+
expect(html).toContain("<code>app/http/controllers/home.tsx</code>");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("links the canonical URL on SITE_URL, whichever host served the page", async () => {
|
|
39
|
+
let response = await createWebRouter().fetch("https://preview.example.net/?utm_source=mail");
|
|
40
|
+
|
|
41
|
+
expect(await response.text()).toContain('<link rel="canonical" href="https://example.com/"');
|
|
42
|
+
});
|
|
43
|
+
});
|