@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,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `GET /api/{{plural}}/:id` through the app's router, so the API chain and its access checks run
|
|
3
|
+
* as they do in the Worker.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/http-apis/json-apis}
|
|
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/api/{{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/api";
|
|
16
|
+
|
|
17
|
+
beforeAll(migrate);
|
|
18
|
+
|
|
19
|
+
/** Fetches the {{words}} with `id`. */
|
|
20
|
+
function show(id: string) {
|
|
21
|
+
return createWebRouter().fetch(
|
|
22
|
+
new URL(routes.api.{{pluralKey}}.show.href({ id }), "https://example.com"),
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
describe("api/{{plural}}/show", () => {
|
|
27
|
+
test("answers the {{words}} as data", 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
|
+
expect(((await response.json()) as { data: { id: string } }).data.id).toBe(row.id);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("answers the notFound problem for an id no {{words}} has", async () => {
|
|
37
|
+
let response = await show("missing");
|
|
38
|
+
|
|
39
|
+
expect(response.status).toBe(404);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `PATCH /api/{{plural}}/:id`: changes the fields the JSON body names, typed by the
|
|
3
|
+
* `{{CONSTANT}}_UPDATE` operation in `routes/api.ts`; the others keep their values. An id no
|
|
4
|
+
* {{words}} has, or one `{{path}}.read` hides, is the `notFound` problem, a refused body the
|
|
5
|
+
* `validationError` one, and a caller `{{path}}.update` refuses gets a 403.
|
|
6
|
+
*
|
|
7
|
+
* @see {@link https://sdxc.dev/docs/http-apis/json-apis}
|
|
8
|
+
* @see {@link https://sdxc.dev/docs/identity-and-security/authorization}{{header}}
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { NotFound } from "@sdxc/data-model";
|
|
12
|
+
import { issuesFrom } from "@sdxc/problem";
|
|
13
|
+
import { isFailure } from "@sdxc/result";
|
|
14
|
+
import { createAction } from "remix/router";
|
|
15
|
+
|
|
16
|
+
import api from "~/app/http/middleware/api";
|
|
17
|
+
import abilities from "~/app/policies/abilities";
|
|
18
|
+
import problems from "~/config/problems";
|
|
19
|
+
import routes, { {{CONSTANT}}_UPDATE } from "~/routes/api";
|
|
20
|
+
|
|
21
|
+
export default createAction(routes.api.{{pluralKey}}.update, {
|
|
22
|
+
middleware: api,
|
|
23
|
+
/** Answers the saved {{words}} as `data`. */
|
|
24
|
+
async handler(ctx) {
|
|
25
|
+
let { id } = ctx.params;
|
|
26
|
+
let row = await ctx.models.{{key}}.find(id);
|
|
27
|
+
if (row === null || !ctx.access.can(abilities.{{path}}.read, { {{key}}: row })) {
|
|
28
|
+
return problems.notFound();
|
|
29
|
+
}
|
|
30
|
+
if (!ctx.access.can(abilities.{{path}}.update, { {{key}}: row })) {
|
|
31
|
+
return new Response(null, { status: 403 });
|
|
32
|
+
}
|
|
33
|
+
let input = await {{CONSTANT}}_UPDATE.parse(ctx.request, ctx.params);
|
|
34
|
+
if (isFailure(input)) {
|
|
35
|
+
return problems.validationError({ extensions: { errors: issuesFrom(input.error) } });
|
|
36
|
+
}
|
|
37
|
+
let updated = await ctx.models.{{key}}.update(id, input.data.body);
|
|
38
|
+
if (isFailure(updated)) {
|
|
39
|
+
if (updated.error instanceof NotFound) return problems.notFound();
|
|
40
|
+
return problems.validationError({ extensions: { errors: issuesFrom(updated.error) } });
|
|
41
|
+
}
|
|
42
|
+
return Response.json({ data: updated.data });
|
|
43
|
+
},
|
|
44
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `PATCH /api/{{plural}}/:id` through the app's router. The API binds every caller as a guest
|
|
3
|
+
* until it authenticates them, and the policy grants a guest reads alone, so the write answers 403.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/http-apis/json-apis}
|
|
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/api/{{plural}}/update";
|
|
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/api";
|
|
16
|
+
|
|
17
|
+
beforeAll(migrate);
|
|
18
|
+
|
|
19
|
+
/** Sends `body` as JSON to the endpoint of the {{words}} with `id`. */
|
|
20
|
+
function submit(id: string, body: Record<string, unknown>) {
|
|
21
|
+
return createWebRouter().fetch(
|
|
22
|
+
new Request(new URL(routes.api.{{pluralKey}}.update.href({ id }), "https://example.com"), {
|
|
23
|
+
method: "PATCH",
|
|
24
|
+
headers: { "content-type": "application/json" },
|
|
25
|
+
body: JSON.stringify(body),
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe("api/{{plural}}/update", () => {
|
|
31
|
+
test("answers 403 to a guest, who may only read", async () => {
|
|
32
|
+
let row = await factories().create({{Name}}Factory);
|
|
33
|
+
|
|
34
|
+
let response = await submit(row.id, { {{changedJson}} });
|
|
35
|
+
|
|
36
|
+
expect(response.status).toBe(403);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("answers the notFound problem for an id no {{words}} has", async () => {
|
|
40
|
+
let response = await submit("missing", { {{changedJson}} });
|
|
41
|
+
|
|
42
|
+
expect(response.status).toBe(404);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test.todo("changes the {{words}} for a caller {{path}}.update admits");
|
|
46
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `{{plural}} create`: writes a {{words}} from a flag per field and prints its id, so a script can
|
|
3
|
+
* pass it on. Each run writes another {{words}}; a write the database refuses fails the run.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/your-apps-command-line}
|
|
6
|
+
* @see {@link https://sdxc.dev/api/cli}{{header}}
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { createCommand, success } from "@sdxc/cli";
|
|
10
|
+
import { isFailure } from "@sdxc/result";
|
|
11
|
+
|
|
12
|
+
import commandset from "~/app/commands";
|
|
13
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
14
|
+
|
|
15
|
+
export default createCommand(commandset{{pluralMember}}.create, async (ctx) => {
|
|
16
|
+
let created = await {{Name}}.bind({ db: ctx.db }).create({
|
|
17
|
+
{{createValues}}
|
|
18
|
+
});
|
|
19
|
+
if (isFailure(created)) return created;
|
|
20
|
+
ctx.stdout(`${created.data.id}\n`);
|
|
21
|
+
return success();
|
|
22
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `{{plural}} create` command, run through the app's command line over the test database.
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/your-apps-command-line}
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { invoke } from "@sdxc/cli/test";
|
|
9
|
+
import { beforeAll, describe, expect, test, vi } from "vitest";
|
|
10
|
+
|
|
11
|
+
import { openDatabase } from "~/app/initializers/database";
|
|
12
|
+
import { cli } from "~/app/initializers/cli";
|
|
13
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
14
|
+
import { migrate } from "~/app/test/database";
|
|
15
|
+
|
|
16
|
+
vi.mock("~/app/initializers/local-database", async (importOriginal) => ({
|
|
17
|
+
...(await importOriginal<object>()),
|
|
18
|
+
openLocalDatabase: (await import("~/app/initializers/database")).openDatabase,
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
beforeAll(migrate);
|
|
22
|
+
|
|
23
|
+
describe("{{plural}} create", () => {
|
|
24
|
+
test("writes a {{words}} from its flags and prints its id", async () => {
|
|
25
|
+
let run = await invoke(cli, ["{{plural}}", "create", {{createArgv}}]);
|
|
26
|
+
|
|
27
|
+
expect(run.code).toBe(0);
|
|
28
|
+
expect(await {{Name}}.bind({ db: openDatabase() }).find(run.stdout.trim())).not.toBeNull();
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `{{plural}} delete <id>`: deletes one {{words}} and says so; an id no {{words}} has fails the
|
|
3
|
+
* run, so running it twice fails the second time.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/your-apps-command-line}
|
|
6
|
+
* @see {@link https://sdxc.dev/api/cli}{{header}}
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { createCommand, success } from "@sdxc/cli";
|
|
10
|
+
import { isFailure } from "@sdxc/result";
|
|
11
|
+
|
|
12
|
+
import commandset from "~/app/commands";
|
|
13
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
14
|
+
|
|
15
|
+
export default createCommand(commandset{{pluralMember}}.delete, async (ctx) => {
|
|
16
|
+
let deleted = await {{Name}}.bind({ db: ctx.db }).delete(ctx.arguments.id);
|
|
17
|
+
if (isFailure(deleted)) return deleted;
|
|
18
|
+
ctx.stdout(`Deleted ${ctx.arguments.id}\n`);
|
|
19
|
+
return success();
|
|
20
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `{{plural}} delete` command, run through the app's command line over the test database.
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/your-apps-command-line}
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { invoke } from "@sdxc/cli/test";
|
|
9
|
+
import { beforeAll, describe, expect, test, vi } from "vitest";
|
|
10
|
+
|
|
11
|
+
import { openDatabase } from "~/app/initializers/database";
|
|
12
|
+
import { cli } from "~/app/initializers/cli";
|
|
13
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
14
|
+
import { factories, migrate } from "~/app/test/database";
|
|
15
|
+
import { {{Name}}Factory } from "~/database/factories/{{name}}";
|
|
16
|
+
|
|
17
|
+
vi.mock("~/app/initializers/local-database", async (importOriginal) => ({
|
|
18
|
+
...(await importOriginal<object>()),
|
|
19
|
+
openLocalDatabase: (await import("~/app/initializers/database")).openDatabase,
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
beforeAll(migrate);
|
|
23
|
+
|
|
24
|
+
describe("{{plural}} delete", () => {
|
|
25
|
+
test("deletes the {{words}} with the id", async () => {
|
|
26
|
+
let row = await factories().create({{Name}}Factory);
|
|
27
|
+
|
|
28
|
+
let run = await invoke(cli, ["{{plural}}", "delete", row.id]);
|
|
29
|
+
|
|
30
|
+
expect(run.code).toBe(0);
|
|
31
|
+
expect(await {{Name}}.bind({ db: openDatabase() }).find(row.id)).toBeNull();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("fails for an id no {{words}} has", async () => {
|
|
35
|
+
let run = await invoke(cli, ["{{plural}}", "delete", "missing"]);
|
|
36
|
+
|
|
37
|
+
expect(run.code).toBe(1);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `{{plural}} list`: one line per {{words}}, newest first, its id then each field, tab-separated
|
|
3
|
+
* so `cut` and `sort` read it. It changes nothing, so it is safe to run any time.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/your-apps-command-line}
|
|
6
|
+
* @see {@link https://sdxc.dev/api/cli}{{header}}
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { createCommand, success } from "@sdxc/cli";
|
|
10
|
+
|
|
11
|
+
import commandset from "~/app/commands";
|
|
12
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
13
|
+
|
|
14
|
+
export default createCommand(commandset{{pluralMember}}.list, async (ctx) => {
|
|
15
|
+
let rows = await {{Name}}.bind({ db: ctx.db }).query().orderBy("created_at", "desc").all();
|
|
16
|
+
for (let row of rows) ctx.stdout(`${[row.id, {{listValues}}].join("\t")}\n`);
|
|
17
|
+
return success();
|
|
18
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `{{plural}} list` command, run through the app's command line over the test database.
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/your-apps-command-line}
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { invoke } from "@sdxc/cli/test";
|
|
9
|
+
import { beforeAll, describe, expect, test, vi } from "vitest";
|
|
10
|
+
|
|
11
|
+
import { cli } from "~/app/initializers/cli";
|
|
12
|
+
import { factories, migrate } from "~/app/test/database";
|
|
13
|
+
import { {{Name}}Factory } from "~/database/factories/{{name}}";
|
|
14
|
+
|
|
15
|
+
vi.mock("~/app/initializers/local-database", async (importOriginal) => ({
|
|
16
|
+
...(await importOriginal<object>()),
|
|
17
|
+
openLocalDatabase: (await import("~/app/initializers/database")).openDatabase,
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
beforeAll(migrate);
|
|
21
|
+
|
|
22
|
+
describe("{{plural}} list", () => {
|
|
23
|
+
test("prints a line per {{words}}, starting with its id", async () => {
|
|
24
|
+
let row = await factories().create({{Name}}Factory);
|
|
25
|
+
|
|
26
|
+
let run = await invoke(cli, ["{{plural}}", "list"]);
|
|
27
|
+
|
|
28
|
+
expect(run.code).toBe(0);
|
|
29
|
+
expect(run.stdout).toContain(`${row.id}\t`);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `{{plural}} show <id>`: prints one {{words}} as JSON, every field included; an id no {{words}}
|
|
3
|
+
* has fails the run. It changes nothing, so it is safe to run any time.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/your-apps-command-line}
|
|
6
|
+
* @see {@link https://sdxc.dev/api/cli}{{header}}
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { createCommand, failure, success } from "@sdxc/cli";
|
|
10
|
+
|
|
11
|
+
import commandset from "~/app/commands";
|
|
12
|
+
import { {{Name}} } from "~/app/models/{{name}}";
|
|
13
|
+
|
|
14
|
+
export default createCommand(commandset{{pluralMember}}.show, async (ctx) => {
|
|
15
|
+
let row = await {{Name}}.bind({ db: ctx.db }).find(ctx.arguments.id);
|
|
16
|
+
if (row === null) return failure(new Error(`No {{words}} has the id ${ctx.arguments.id}`));
|
|
17
|
+
ctx.stdout(`${JSON.stringify(row, null, 2)}\n`);
|
|
18
|
+
return success();
|
|
19
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `{{plural}} show` command, run through the app's command line over the test database.
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/your-apps-command-line}
|
|
5
|
+
* @see {@link https://sdxc.dev/docs/operations-and-testing/testing}{{header}}
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { invoke } from "@sdxc/cli/test";
|
|
9
|
+
import { beforeAll, describe, expect, test, vi } from "vitest";
|
|
10
|
+
|
|
11
|
+
import { cli } from "~/app/initializers/cli";
|
|
12
|
+
import { factories, migrate } from "~/app/test/database";
|
|
13
|
+
import { {{Name}}Factory } from "~/database/factories/{{name}}";
|
|
14
|
+
|
|
15
|
+
vi.mock("~/app/initializers/local-database", async (importOriginal) => ({
|
|
16
|
+
...(await importOriginal<object>()),
|
|
17
|
+
openLocalDatabase: (await import("~/app/initializers/database")).openDatabase,
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
beforeAll(migrate);
|
|
21
|
+
|
|
22
|
+
describe("{{plural}} show", () => {
|
|
23
|
+
test("prints the {{words}} as JSON", async () => {
|
|
24
|
+
let row = await factories().create({{Name}}Factory);
|
|
25
|
+
|
|
26
|
+
let run = await invoke(cli, ["{{plural}}", "show", row.id]);
|
|
27
|
+
|
|
28
|
+
expect(run.code).toBe(0);
|
|
29
|
+
expect(JSON.parse(run.stdout)).toMatchObject({ id: row.id });
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("fails for an id no {{words}} has", async () => {
|
|
33
|
+
let run = await invoke(cli, ["{{plural}}", "show", "missing"]);
|
|
34
|
+
|
|
35
|
+
expect(run.code).toBe(1);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deletes one {{words}} and sends the visitor back to the list. An id no {{words}} has, or one
|
|
3
|
+
* `{{path}}.read` hides, is a 404, and a visitor `{{path}}.delete` refuses gets a 403.
|
|
4
|
+
* The show page's delete button posts here, with the honeypot fields the page middleware checks.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://sdxc.dev/docs/building-remix-apps/wire-the-router}
|
|
7
|
+
* @see {@link https://sdxc.dev/docs/identity-and-security/protect-forms}{{header}}
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { isFailure } from "@sdxc/result";
|
|
11
|
+
import { createAction } from "remix/router";
|
|
12
|
+
|
|
13
|
+
import page from "~/app/http/middleware/page";
|
|
14
|
+
import abilities from "~/app/policies/abilities";
|
|
15
|
+
import routes from "~/routes/web";
|
|
16
|
+
|
|
17
|
+
export default createAction(routes.{{pluralKey}}.destroy, {
|
|
18
|
+
middleware: page,
|
|
19
|
+
/** Deletes the {{words}}, then redirects to the index. */
|
|
20
|
+
async handler(ctx) {
|
|
21
|
+
let row = await ctx.models.{{key}}.find(ctx.params.id);
|
|
22
|
+
if (row === null || !ctx.access.can(abilities.{{path}}.read, { {{key}}: row })) {
|
|
23
|
+
return new Response(null, { status: 404 });
|
|
24
|
+
}
|
|
25
|
+
if (!ctx.access.can(abilities.{{path}}.delete, { {{key}}: row })) {
|
|
26
|
+
return new Response(null, { status: 403 });
|
|
27
|
+
}
|
|
28
|
+
let deleted = await ctx.models.{{key}}.delete(ctx.params.id);
|
|
29
|
+
if (isFailure(deleted)) return new Response(null, { status: 404 });
|
|
30
|
+
return new Response(null, {
|
|
31
|
+
status: 303,
|
|
32
|
+
headers: { location: routes.{{pluralKey}}.index.href() },
|
|
33
|
+
});
|
|
34
|
+
},
|
|
35
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deleting a {{words}} through the app's router as a signed-in member, so the honeypot and access
|
|
3
|
+
* checks run as in the Worker, and as a guest, whom `{{path}}.delete` refuses. Its controller
|
|
4
|
+
* 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/wire-the-router}
|
|
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}}/destroy";
|
|
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 { factories, migrate } from "~/app/test/database";
|
|
19
|
+
import { signIn } from "~/app/test/session";
|
|
20
|
+
import { {{Name}}Factory } from "~/database/factories/{{name}}";
|
|
21
|
+
import routes from "~/routes/web";
|
|
22
|
+
|
|
23
|
+
beforeAll(migrate);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Posts to the delete action of the {{words}} with `id`, with valid honeypot fields, as
|
|
27
|
+
* `cookie`'s visitor.
|
|
28
|
+
*/
|
|
29
|
+
async function destroy(id: string, cookie: string | null) {
|
|
30
|
+
let { token, tokenField } = unwrap(await honeypot.issue());
|
|
31
|
+
return createWebRouter().fetch(
|
|
32
|
+
new Request(new URL(routes.{{pluralKey}}.destroy.href({ id }), "https://example.com"), {
|
|
33
|
+
method: "POST",
|
|
34
|
+
headers: cookie === null ? {} : { cookie },
|
|
35
|
+
body: new URLSearchParams({ [tokenField]: token }),
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe("{{plural}}/destroy", () => {
|
|
41
|
+
test("deletes the {{words}} and redirects to the index", async () => {
|
|
42
|
+
let row = await factories().create({{Name}}Factory);
|
|
43
|
+
|
|
44
|
+
let response = await destroy(row.id, await signIn());
|
|
45
|
+
|
|
46
|
+
expect(response.status).toBe(303);
|
|
47
|
+
expect(response.headers.get("location")).toBe(routes.{{pluralKey}}.index.href());
|
|
48
|
+
expect(await {{Name}}.bind({ db: openDatabase() }).find(row.id)).toBeNull();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("answers 404 for an id no {{words}} has", async () => {
|
|
52
|
+
let response = await destroy("missing", await signIn());
|
|
53
|
+
|
|
54
|
+
expect(response.status).toBe(404);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("answers a guest 403 and keeps the {{words}}", async () => {
|
|
58
|
+
let row = await factories().create({{Name}}Factory);
|
|
59
|
+
|
|
60
|
+
let response = await destroy(row.id, null);
|
|
61
|
+
|
|
62
|
+
expect(response.status).toBe(403);
|
|
63
|
+
expect(await {{Name}}.bind({ db: openDatabase() }).find(row.id)).not.toBeNull();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -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
|
+
});
|