@sdxc/kit 2026.10.17 → 2026.10.21
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 +397 -22
- package/changelog.jsonl +1171 -1092
- package/config.schema.json +53 -1
- package/dist/cli.js +12 -0
- 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 +78 -6
- package/dist/commands/make.js +134 -20
- 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 +16 -0
- package/dist/commands/run.js +32 -11
- package/dist/commands/scaffold.d.ts +30 -0
- package/dist/commands/scaffold.js +34 -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 +20 -0
- package/dist/config.js +32 -1
- package/dist/contract.d.ts +101 -3
- package/dist/contract.js +371 -26
- package/dist/create/templates.js +3 -2
- package/dist/dependencies.d.ts +5 -1
- package/dist/dependencies.js +16 -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 +317 -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 +70 -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/jsdoc.d.ts +44 -0
- package/dist/doctor/jsdoc.js +201 -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/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 +31 -3
- package/dist/make/declare.js +284 -9
- 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 +25 -5
- package/dist/make/generate.js +76 -22
- 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 +49 -4
- package/dist/make/index.js +290 -29
- package/dist/make/kinds.d.ts +34 -2
- package/dist/make/kinds.js +92 -8
- 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 +14 -7
- package/dist/make/stubs.js +24 -8
- package/dist/make/test.d.ts +43 -0
- package/dist/make/test.js +150 -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 +39 -0
- package/dist/scaffold/index.js +622 -0
- package/dist/scaffold/render.d.ts +61 -0
- package/dist/scaffold/render.js +417 -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/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/registry/ui/sidebar.json +3 -3
- package/registry/ui/typeset.json +3 -3
- package/stubs/command.spec.stub +15 -0
- package/stubs/controller.api.resource.stub +23 -0
- package/stubs/controller.api.spec.stub +15 -0
- package/stubs/controller.form.stub +2 -2
- package/stubs/controller.form.test.stub +2 -2
- package/stubs/controller.resource.form.stub +67 -0
- package/stubs/controller.resource.stub +28 -0
- package/stubs/controller.spec.stub +15 -0
- package/stubs/email.stub +9 -14
- package/stubs/email.test.stub +9 -9
- package/stubs/factory.stub +1 -1
- package/stubs/mcp.spec.stub +16 -0
- package/stubs/model.stub +3 -3
- package/stubs/policy.test.stub +34 -8
- 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 +3 -0
- package/stubs/scaffold.model.stub +35 -0
- package/stubs/scaffold.model.test.stub +25 -0
- package/stubs/scaffold.spec.stub +7 -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/templates/app/AGENTS.md +167 -0
- package/templates/app/CLAUDE.md +1 -0
- package/templates/app/README.md +33 -15
- 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/fresh.test.ts +57 -0
- package/templates/app/app/commands/db/fresh.ts +37 -0
- package/templates/app/app/commands/db/migrate.test.ts +17 -1
- package/templates/app/app/commands/db/migrate.ts +51 -18
- 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 +57 -2
- package/templates/app/app/commands/typegen.test.ts +20 -1
- package/templates/app/app/commands/typegen.ts +16 -2
- package/templates/app/app/emails/index.ts +10 -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 +12 -3
- package/templates/app/app/http/middleware/page.test.ts +4 -1
- package/templates/app/app/http/middleware/page.ts +7 -5
- package/templates/app/app/initializers/cli.ts +35 -2
- package/templates/app/app/initializers/dispatcher.ts +9 -1
- package/templates/app/app/initializers/mail-transport.test.ts +33 -0
- package/templates/app/app/initializers/mail-transport.ts +26 -0
- package/templates/app/app/initializers/mail.test.ts +17 -17
- package/templates/app/app/initializers/mail.ts +15 -7
- package/templates/app/app/initializers/router.test.ts +7 -0
- package/templates/app/app/initializers/router.tsx +13 -3
- 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/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 +48 -1
- package/templates/app/app/services/migrations.ts +66 -9
- package/templates/app/app/test/cloudflare-workers.ts +2 -0
- package/templates/app/app/test/local-database.ts +67 -0
- package/templates/app/app/test/session.ts +28 -0
- package/templates/app/bootstrap/console.ts +13 -0
- package/templates/app/cloudflare.config.ts +3 -3
- package/templates/app/database/seed.ts +12 -0
- package/templates/app/package.json +3 -1
- package/templates/app/spec/config.jsonc +16 -0
- package/stubs/policy.stub +0 -16
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sdxc middleware`: every chain a request can run, in the order it runs it. Each router's global
|
|
3
|
+
* middleware comes first, then every module in `app/http/middleware/`: a chain such as `page`
|
|
4
|
+
* expanded to its members, or a single middleware, each with the number of routes that run it.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { MiddlewareEntry } from "./routers.js";
|
|
10
|
+
/** One module in the middleware folder. */
|
|
11
|
+
export interface MiddlewareModule {
|
|
12
|
+
file: string;
|
|
13
|
+
/**
|
|
14
|
+
* `chain` for a module whose default export is `createMiddleware(…)`, whose `middleware`
|
|
15
|
+
* lists what it runs in order; `middleware` for one that is a middleware itself.
|
|
16
|
+
*/
|
|
17
|
+
kind: "chain" | "middleware";
|
|
18
|
+
middleware: MiddlewareEntry[];
|
|
19
|
+
/** The routes whose router, guards or controller run it, directly or through a chain. */
|
|
20
|
+
routes: number;
|
|
21
|
+
}
|
|
22
|
+
/** What `sdxc middleware` prints. */
|
|
23
|
+
export interface MiddlewareListing {
|
|
24
|
+
/** Each router's global middleware, which runs before anything else. */
|
|
25
|
+
routers: Array<{
|
|
26
|
+
file: string;
|
|
27
|
+
middleware: MiddlewareEntry[];
|
|
28
|
+
}>;
|
|
29
|
+
modules: MiddlewareModule[];
|
|
30
|
+
}
|
|
31
|
+
/** Reads the routers and the middleware folder of the project at `root`. */
|
|
32
|
+
export declare function listMiddleware(root: string): Promise<MiddlewareListing>;
|
|
33
|
+
/** The listing as text: each router's chain, then each module, members numbered in run order. */
|
|
34
|
+
export declare function formatMiddleware(listing: MiddlewareListing): string;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sdxc middleware`: every chain a request can run, in the order it runs it. Each router's global
|
|
3
|
+
* middleware comes first, then every module in `app/http/middleware/`: a chain such as `page`
|
|
4
|
+
* expanded to its members, or a single middleware, each with the number of routes that run it.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { isAppSource, listFiles } from "../doctor/files.js";
|
|
10
|
+
import { Project } from "./modules.js";
|
|
11
|
+
import { middlewareList } from "./routers.js";
|
|
12
|
+
import { listRoutes } from "./index.js";
|
|
13
|
+
/** The folder the app's middleware lives in. */
|
|
14
|
+
const MIDDLEWARE_FOLDER = "app/http/middleware";
|
|
15
|
+
/** Reads the routers and the middleware folder of the project at `root`. */
|
|
16
|
+
export async function listMiddleware(root) {
|
|
17
|
+
let listing = await listRoutes(root);
|
|
18
|
+
let project = new Project(root);
|
|
19
|
+
let modules = [];
|
|
20
|
+
for (let file of (await listFiles(root, MIDDLEWARE_FOLDER)).filter(isAppSource)) {
|
|
21
|
+
let members = await chainMembers(project, file);
|
|
22
|
+
modules.push({
|
|
23
|
+
file,
|
|
24
|
+
kind: members === null ? "middleware" : "chain",
|
|
25
|
+
middleware: members ?? [],
|
|
26
|
+
routes: 0,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
for (let module of modules) {
|
|
30
|
+
let chains = modules
|
|
31
|
+
.filter((chain) => chain.middleware.some((entry) => entry.from === module.file))
|
|
32
|
+
.map((chain) => chain.file);
|
|
33
|
+
let runs = new Set([module.file, ...chains]);
|
|
34
|
+
module.routes = listing.routes.filter((route) => [...route.middleware.router, ...route.middleware.guards, ...route.middleware.controller].some((entry) => entry.from !== null && runs.has(entry.from))).length;
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
routers: listing.routers.map((router) => ({
|
|
38
|
+
file: router.file,
|
|
39
|
+
middleware: router.middleware,
|
|
40
|
+
})),
|
|
41
|
+
modules,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The members of the chain `file` default-exports as `createMiddleware(a, b, …)`, each array
|
|
46
|
+
* argument expanded, or `null` when its default export is something else.
|
|
47
|
+
*/
|
|
48
|
+
async function chainMembers(project, file) {
|
|
49
|
+
let module = await project.load(file);
|
|
50
|
+
if (module === null)
|
|
51
|
+
return null;
|
|
52
|
+
let found = await project.exported(module, "default");
|
|
53
|
+
if (found === null)
|
|
54
|
+
return null;
|
|
55
|
+
let node = found.node;
|
|
56
|
+
if (node.kind === "identifier") {
|
|
57
|
+
let binding = await project.bind(found.module, node.name);
|
|
58
|
+
if (binding.kind !== "local")
|
|
59
|
+
return null;
|
|
60
|
+
node = binding.node;
|
|
61
|
+
}
|
|
62
|
+
if (node.kind !== "call" || node.callee.kind !== "identifier")
|
|
63
|
+
return null;
|
|
64
|
+
let imported = found.module.imports.get(node.callee.name);
|
|
65
|
+
if (imported?.imported !== "createMiddleware")
|
|
66
|
+
return null;
|
|
67
|
+
let members = [];
|
|
68
|
+
for (let argument of node.args) {
|
|
69
|
+
members.push(...(await middlewareList(project, found.module, argument.node)));
|
|
70
|
+
}
|
|
71
|
+
return members;
|
|
72
|
+
}
|
|
73
|
+
/** The listing as text: each router's chain, then each module, members numbered in run order. */
|
|
74
|
+
export function formatMiddleware(listing) {
|
|
75
|
+
let block = (title, entries) => {
|
|
76
|
+
let width = Math.max(0, ...entries.map((entry) => entry.name.length));
|
|
77
|
+
let digits = String(entries.length).length;
|
|
78
|
+
let lines = entries.map((entry, index) => {
|
|
79
|
+
let from = entry.from === null ? "" : ` ${entry.from}`;
|
|
80
|
+
let number = `${index + 1}.`.padStart(digits + 1);
|
|
81
|
+
return ` ${number} ${entry.name.padEnd(width)}${from}`.trimEnd();
|
|
82
|
+
});
|
|
83
|
+
return [title, ...lines].join("\n");
|
|
84
|
+
};
|
|
85
|
+
let blocks = [
|
|
86
|
+
...listing.routers.map((router) => block(`${router.file}, the router's global middleware:`, router.middleware)),
|
|
87
|
+
...listing.modules.map((module) => {
|
|
88
|
+
let used = `${module.routes} route${module.routes === 1 ? "" : "s"}`;
|
|
89
|
+
return module.kind === "chain"
|
|
90
|
+
? block(`${module.file}, a chain ${used} run:`, module.middleware)
|
|
91
|
+
: `${module.file}, a middleware ${used} run`;
|
|
92
|
+
}),
|
|
93
|
+
];
|
|
94
|
+
return blocks.length === 0 ? "No routers or middleware found.\n" : `${blocks.join("\n\n")}\n`;
|
|
95
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Narrows a route listing to what `sdxc routes` was asked about: a search across names, patterns,
|
|
3
|
+
* methods and controllers, one controller, one method, one contract, paths to leave out, or the
|
|
4
|
+
* routes no router maps, then sorts it. Both run before any output, so the table and `--json` agree.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { RouteListing } from "./index.js";
|
|
10
|
+
/** What to keep; a route stays when it passes every filter given. */
|
|
11
|
+
export interface RouteFilters {
|
|
12
|
+
/**
|
|
13
|
+
* Matched, ignoring case, against the name, pattern, method and `file#export` of the
|
|
14
|
+
* controller, as plain text or as a regular expression, so `/posts/:id` and `^posts\.` both work.
|
|
15
|
+
*/
|
|
16
|
+
grep?: string;
|
|
17
|
+
/** A controller file, with or without `app/http/controllers/` and its extension; a folder keeps every controller in it. */
|
|
18
|
+
controller?: string;
|
|
19
|
+
/** `GET`, `post`, …; a route declared for `ANY` method answers every one, so it stays. */
|
|
20
|
+
method?: string;
|
|
21
|
+
/** A contract in `routes/`, as `web`, `routes/web` or `routes/web.ts`. */
|
|
22
|
+
contract?: string;
|
|
23
|
+
/** Keeps the routes a contract declares and no router maps. */
|
|
24
|
+
unused?: boolean;
|
|
25
|
+
/** Leaves out the routes whose pattern starts with any of these, as `/api` or `/mcp`. */
|
|
26
|
+
exceptPath?: readonly string[];
|
|
27
|
+
}
|
|
28
|
+
/** What `--sort` orders the routes by. */
|
|
29
|
+
export declare const ROUTE_SORTS: readonly ["name", "pattern", "method", "controller"];
|
|
30
|
+
/** One of {@link ROUTE_SORTS}. */
|
|
31
|
+
export type RouteSort = (typeof ROUTE_SORTS)[number];
|
|
32
|
+
/** Whether any filter is set, so the command can tell an empty project from an empty result. */
|
|
33
|
+
export declare function hasFilters(filters: RouteFilters): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* The listing with only the routes that pass every filter, and only the routers that map one of
|
|
36
|
+
* them, so a narrowed table carries no global middleware it does not list. Without filters the
|
|
37
|
+
* listing comes back as it is.
|
|
38
|
+
*/
|
|
39
|
+
export declare function filterRoutes(listing: RouteListing, filters: RouteFilters): RouteListing;
|
|
40
|
+
/**
|
|
41
|
+
* The controller files among `files` no route of `listing` loads, narrowed by
|
|
42
|
+
* `--grep` and `--controller`, which both read the file. A `--method` or `--contract` filter
|
|
43
|
+
* leaves none, since a file no route maps has neither.
|
|
44
|
+
*
|
|
45
|
+
* @param files - The controller sources no module imports statically, project-relative
|
|
46
|
+
* @param listing - The listing before any filter, so a route filtered out still counts as a use
|
|
47
|
+
*/
|
|
48
|
+
export declare function unmappedControllers(files: readonly string[], listing: RouteListing, filters: RouteFilters): string[];
|
|
49
|
+
/**
|
|
50
|
+
* The listing with its routes ordered by `sort`, ties kept in declaration order; a route no
|
|
51
|
+
* router maps sorts last by controller. Without `sort` the listing comes back as it is.
|
|
52
|
+
*/
|
|
53
|
+
export declare function sortRoutes(listing: RouteListing, sort: RouteSort | undefined): RouteListing;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Narrows a route listing to what `sdxc routes` was asked about: a search across names, patterns,
|
|
3
|
+
* methods and controllers, one controller, one method, one contract, paths to leave out, or the
|
|
4
|
+
* routes no router maps, then sorts it. Both run before any output, so the table and `--json` agree.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { isSuccess, wrap } from "@sdxc/result";
|
|
10
|
+
/** The folder a controller lives in, which `--controller` lets a reader leave out. */
|
|
11
|
+
const CONTROLLERS_FOLDER = "app/http/controllers/";
|
|
12
|
+
/** What `--sort` orders the routes by. */
|
|
13
|
+
export const ROUTE_SORTS = ["name", "pattern", "method", "controller"];
|
|
14
|
+
/** Whether any filter is set, so the command can tell an empty project from an empty result. */
|
|
15
|
+
export function hasFilters(filters) {
|
|
16
|
+
return (filters.grep !== undefined ||
|
|
17
|
+
filters.controller !== undefined ||
|
|
18
|
+
filters.method !== undefined ||
|
|
19
|
+
filters.contract !== undefined ||
|
|
20
|
+
filters.unused === true ||
|
|
21
|
+
(filters.exceptPath ?? []).length > 0);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The listing with only the routes that pass every filter, and only the routers that map one of
|
|
25
|
+
* them, so a narrowed table carries no global middleware it does not list. Without filters the
|
|
26
|
+
* listing comes back as it is.
|
|
27
|
+
*/
|
|
28
|
+
export function filterRoutes(listing, filters) {
|
|
29
|
+
if (!hasFilters(filters))
|
|
30
|
+
return listing;
|
|
31
|
+
let search = filters.grep === undefined ? null : matcher(filters.grep);
|
|
32
|
+
let routes = listing.routes.filter((route) => matches(route, filters, search));
|
|
33
|
+
let used = new Set(routes.map((route) => route.router));
|
|
34
|
+
return {
|
|
35
|
+
...listing,
|
|
36
|
+
routes,
|
|
37
|
+
routers: listing.routers.filter((router) => used.has(router.file)),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The controller files among `files` no route of `listing` loads, narrowed by
|
|
42
|
+
* `--grep` and `--controller`, which both read the file. A `--method` or `--contract` filter
|
|
43
|
+
* leaves none, since a file no route maps has neither.
|
|
44
|
+
*
|
|
45
|
+
* @param files - The controller sources no module imports statically, project-relative
|
|
46
|
+
* @param listing - The listing before any filter, so a route filtered out still counts as a use
|
|
47
|
+
*/
|
|
48
|
+
export function unmappedControllers(files, listing, filters) {
|
|
49
|
+
if (filters.method !== undefined || filters.contract !== undefined)
|
|
50
|
+
return [];
|
|
51
|
+
let mapped = new Set(listing.routes.map((route) => route.controller?.file));
|
|
52
|
+
let search = filters.grep === undefined ? null : matcher(filters.grep);
|
|
53
|
+
return files.filter((file) => !mapped.has(file) &&
|
|
54
|
+
(search === null || search(file)) &&
|
|
55
|
+
(filters.controller === undefined || controllerMatches(file, filters.controller)));
|
|
56
|
+
}
|
|
57
|
+
/** Whether `route` passes every filter in `filters`, with `--grep` compiled once as `search`. */
|
|
58
|
+
function matches(route, filters, search) {
|
|
59
|
+
if (filters.unused === true && route.controller !== null)
|
|
60
|
+
return false;
|
|
61
|
+
if ((filters.exceptPath ?? []).some((prefix) => pathStartsWith(route.pattern, prefix))) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
if (filters.method !== undefined) {
|
|
65
|
+
let method = filters.method.toUpperCase();
|
|
66
|
+
if (route.method !== method && route.method !== "ANY")
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
if (filters.contract !== undefined &&
|
|
70
|
+
contractName(route.contract) !== contractName(filters.contract)) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
if (filters.controller !== undefined) {
|
|
74
|
+
let file = route.controller?.file ?? null;
|
|
75
|
+
if (file === null || !controllerMatches(file, filters.controller))
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
if (search !== null) {
|
|
79
|
+
let fields = [route.name, route.pattern, route.method];
|
|
80
|
+
if (route.controller?.file)
|
|
81
|
+
fields.push(route.controller.file);
|
|
82
|
+
let named = route.controller?.export;
|
|
83
|
+
if (named !== undefined && named !== null && named !== "default")
|
|
84
|
+
fields.push(named);
|
|
85
|
+
if (!fields.some(search))
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* A case-insensitive test for `pattern` as plain text or as a regular expression: a URL pattern
|
|
92
|
+
* whose `(`, `.` or `*` mean something to a regular expression still finds itself, and text that
|
|
93
|
+
* is no valid expression searches as plain text alone.
|
|
94
|
+
*/
|
|
95
|
+
function matcher(pattern) {
|
|
96
|
+
let text = pattern.toLowerCase();
|
|
97
|
+
let compiled = wrap(() => new RegExp(pattern, "i"));
|
|
98
|
+
let expression = isSuccess(compiled) ? compiled.data : null;
|
|
99
|
+
return (value) => value.toLowerCase().includes(text) || (expression?.test(value) ?? false);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Whether `pattern` is `prefix` or sits under it by whole segments, so `/api` leaves out
|
|
103
|
+
* `/api/posts` and keeps `/apiary`.
|
|
104
|
+
*/
|
|
105
|
+
function pathStartsWith(pattern, prefix) {
|
|
106
|
+
let base = `/${prefix.trim().replace(/^\/+|\/+$/g, "")}`;
|
|
107
|
+
if (base === "/")
|
|
108
|
+
return true;
|
|
109
|
+
return pattern === base || pattern.startsWith(`${base}/`);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The listing with its routes ordered by `sort`, ties kept in declaration order; a route no
|
|
113
|
+
* router maps sorts last by controller. Without `sort` the listing comes back as it is.
|
|
114
|
+
*/
|
|
115
|
+
export function sortRoutes(listing, sort) {
|
|
116
|
+
if (sort === undefined)
|
|
117
|
+
return listing;
|
|
118
|
+
let key = (route) => {
|
|
119
|
+
if (sort === "controller")
|
|
120
|
+
return route.controller?.file ?? "\uffff";
|
|
121
|
+
return route[sort];
|
|
122
|
+
};
|
|
123
|
+
let routes = [...listing.routes].sort((a, b) => key(a).localeCompare(key(b)));
|
|
124
|
+
return { ...listing, routes };
|
|
125
|
+
}
|
|
126
|
+
/** Whether the controller `file` is `wanted` or sits in the folder `wanted` names. */
|
|
127
|
+
function controllerMatches(file, wanted) {
|
|
128
|
+
let name = controllerName(file);
|
|
129
|
+
let target = controllerName(wanted);
|
|
130
|
+
return name === target || name.startsWith(`${target}/`);
|
|
131
|
+
}
|
|
132
|
+
/** A controller path without `./`, its folder, its extension or a trailing `/index`, lowercased. */
|
|
133
|
+
function controllerName(path) {
|
|
134
|
+
let name = path.trim().toLowerCase().replace(/^\.\//, "").replace(/\/+$/, "");
|
|
135
|
+
if (name.startsWith(CONTROLLERS_FOLDER))
|
|
136
|
+
name = name.slice(CONTROLLERS_FOLDER.length);
|
|
137
|
+
return name.replace(/\.[cm]?[jt]sx?$/, "").replace(/\/index$/, "");
|
|
138
|
+
}
|
|
139
|
+
/** A contract path without `./`, `routes/` or its extension, lowercased. */
|
|
140
|
+
function contractName(path) {
|
|
141
|
+
return path
|
|
142
|
+
.trim()
|
|
143
|
+
.toLowerCase()
|
|
144
|
+
.replace(/^\.\//, "")
|
|
145
|
+
.replace(/^routes\//, "")
|
|
146
|
+
.replace(/\.[cm]?[jt]sx?$/, "");
|
|
147
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two shapes `sdxc routes` prints: a table per route contract for a terminal, under each
|
|
3
|
+
* router's global middleware, and a JSON array an agent parses, the same for every run over
|
|
4
|
+
* the same files.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { RouteListing } from "./index.js";
|
|
10
|
+
/** The listing as pretty-printed JSON: the routes array alone, with a trailing newline. */
|
|
11
|
+
export declare function formatJSON(listing: RouteListing): string;
|
|
12
|
+
/**
|
|
13
|
+
* The listing as text: each router's global middleware once, since every route it maps runs it
|
|
14
|
+
* first, then one aligned table per contract. MIDDLEWARE lists `lazy()` guards, then the
|
|
15
|
+
* controller's chain, in the order they run.
|
|
16
|
+
*/
|
|
17
|
+
export declare function formatTable(listing: RouteListing): string;
|
|
18
|
+
/**
|
|
19
|
+
* The listing as one block per route, every field on its own line, for a terminal too narrow
|
|
20
|
+
* for the table. Middleware is split by where it runs: the router's, `lazy()` guards, then the
|
|
21
|
+
* controller's chain, each in its run order.
|
|
22
|
+
*/
|
|
23
|
+
export declare function formatExpanded(listing: RouteListing): string;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two shapes `sdxc routes` prints: a table per route contract for a terminal, under each
|
|
3
|
+
* router's global middleware, and a JSON array an agent parses, the same for every run over
|
|
4
|
+
* the same files.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
/** Where a router's global middleware wraps, a width every terminal shows whole. */
|
|
10
|
+
const LINE_WIDTH = 100;
|
|
11
|
+
/** The listing as pretty-printed JSON: the routes array alone, with a trailing newline. */
|
|
12
|
+
export function formatJSON(listing) {
|
|
13
|
+
return `${JSON.stringify(listing.routes, null, 2)}\n`;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The listing as text: each router's global middleware once, since every route it maps runs it
|
|
17
|
+
* first, then one aligned table per contract. MIDDLEWARE lists `lazy()` guards, then the
|
|
18
|
+
* controller's chain, in the order they run.
|
|
19
|
+
*/
|
|
20
|
+
export function formatTable(listing) {
|
|
21
|
+
if (listing.routes.length === 0)
|
|
22
|
+
return "No routes found in routes/.\n";
|
|
23
|
+
let lines = [];
|
|
24
|
+
let multiple = listing.routers.length > 1;
|
|
25
|
+
for (let router of listing.routers) {
|
|
26
|
+
lines.push(`Router ${router.file}`);
|
|
27
|
+
if (router.middleware.length === 0)
|
|
28
|
+
lines.push(" (no global middleware)");
|
|
29
|
+
else
|
|
30
|
+
lines.push(...wrap(router.middleware.map((entry) => entry.name)));
|
|
31
|
+
lines.push("");
|
|
32
|
+
}
|
|
33
|
+
let contracts = [...new Set(listing.routes.map((route) => route.contract))];
|
|
34
|
+
for (let contract of contracts) {
|
|
35
|
+
let routes = listing.routes.filter((route) => route.contract === contract);
|
|
36
|
+
let header = [
|
|
37
|
+
"METHOD",
|
|
38
|
+
"PATTERN",
|
|
39
|
+
"NAME",
|
|
40
|
+
...(multiple ? ["ROUTER"] : []),
|
|
41
|
+
"CONTROLLER",
|
|
42
|
+
"MIDDLEWARE",
|
|
43
|
+
];
|
|
44
|
+
let rows = routes.map((route) => [
|
|
45
|
+
route.method,
|
|
46
|
+
route.pattern,
|
|
47
|
+
route.name,
|
|
48
|
+
...(multiple ? [route.router ?? ""] : []),
|
|
49
|
+
controller(route),
|
|
50
|
+
names([...route.middleware.guards, ...route.middleware.controller]),
|
|
51
|
+
]);
|
|
52
|
+
let widths = header.map((cell, column) => Math.max(cell.length, ...rows.map((row) => (row[column] ?? "").length)));
|
|
53
|
+
lines.push(contract);
|
|
54
|
+
for (let row of [header, ...rows]) {
|
|
55
|
+
lines.push(` ${row
|
|
56
|
+
.map((cell, column) => column === row.length - 1 ? cell : cell.padEnd(widths[column] ?? 0))
|
|
57
|
+
.join(" ")
|
|
58
|
+
.trimEnd()}`);
|
|
59
|
+
}
|
|
60
|
+
lines.push("");
|
|
61
|
+
}
|
|
62
|
+
return `${lines.join("\n").trimEnd()}\n`;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The listing as one block per route, every field on its own line, for a terminal too narrow
|
|
66
|
+
* for the table. Middleware is split by where it runs: the router's, `lazy()` guards, then the
|
|
67
|
+
* controller's chain, each in its run order.
|
|
68
|
+
*/
|
|
69
|
+
export function formatExpanded(listing) {
|
|
70
|
+
if (listing.routes.length === 0)
|
|
71
|
+
return "No routes found in routes/.\n";
|
|
72
|
+
let blocks = listing.routes.map((route, index) => {
|
|
73
|
+
let fields = [
|
|
74
|
+
["Method", route.method],
|
|
75
|
+
["Pattern", route.pattern],
|
|
76
|
+
["Name", route.name],
|
|
77
|
+
["Contract", route.contract],
|
|
78
|
+
["Router", route.router ?? ""],
|
|
79
|
+
["Controller", controller(route)],
|
|
80
|
+
["Router middleware", names(route.middleware.router)],
|
|
81
|
+
["Guards", names(route.middleware.guards)],
|
|
82
|
+
["Controller middleware", names(route.middleware.controller)],
|
|
83
|
+
];
|
|
84
|
+
let width = Math.max(...fields.map(([label]) => label.length));
|
|
85
|
+
let lines = fields.map(([label, value]) => `${label.padEnd(width)} | ${value}`.trimEnd());
|
|
86
|
+
let title = `--[ Route ${index + 1} ]`;
|
|
87
|
+
let rule = Math.min(LINE_WIDTH, Math.max(...lines.map((line) => line.length)));
|
|
88
|
+
return [title.padEnd(rule, "-"), ...lines].join("\n");
|
|
89
|
+
});
|
|
90
|
+
return `${blocks.join("\n\n")}\n`;
|
|
91
|
+
}
|
|
92
|
+
/** The controller column: the file with a named export after `#`, or why there is none. */
|
|
93
|
+
function controller(route) {
|
|
94
|
+
if (route.controller === null)
|
|
95
|
+
return "(not mapped)";
|
|
96
|
+
if (route.controller.file === null)
|
|
97
|
+
return "(unresolved)";
|
|
98
|
+
if (route.controller.export === null || route.controller.export === "default") {
|
|
99
|
+
return route.controller.file;
|
|
100
|
+
}
|
|
101
|
+
return `${route.controller.file}#${route.controller.export}`;
|
|
102
|
+
}
|
|
103
|
+
/** Names joined by commas in lines of at most {@link LINE_WIDTH} columns, each indented two spaces. */
|
|
104
|
+
function wrap(items) {
|
|
105
|
+
let lines = [];
|
|
106
|
+
let line = "";
|
|
107
|
+
for (let [index, item] of items.entries()) {
|
|
108
|
+
let piece = index === items.length - 1 ? item : `${item},`;
|
|
109
|
+
if (line !== "" && line.length + 1 + piece.length > LINE_WIDTH - 2) {
|
|
110
|
+
lines.push(` ${line}`);
|
|
111
|
+
line = piece;
|
|
112
|
+
}
|
|
113
|
+
else
|
|
114
|
+
line = line === "" ? piece : `${line} ${piece}`;
|
|
115
|
+
}
|
|
116
|
+
if (line !== "")
|
|
117
|
+
lines.push(` ${line}`);
|
|
118
|
+
return lines;
|
|
119
|
+
}
|
|
120
|
+
/** Middleware names joined in the order they run. */
|
|
121
|
+
function names(entries) {
|
|
122
|
+
return entries.map((entry) => entry.name).join(", ");
|
|
123
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `remix/routes` helpers restated over evaluated values: method routes, `route()` with its
|
|
3
|
+
* prefix, `form()`, `resources()` and `resource()` with `only`, `exclude`, `param` and `names`.
|
|
4
|
+
* They follow the rules the helpers apply at runtime, so a listed pattern is the one the router matches.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Value } from "./modules.js";
|
|
10
|
+
/** The canonical helper an import binds, or `null` when it is not a route helper. */
|
|
11
|
+
export declare function helperName(specifier: string, imported: string): string | null;
|
|
12
|
+
/**
|
|
13
|
+
* Applies the helper `name` to evaluated arguments. An argument the reader could not evaluate
|
|
14
|
+
* leaves the result opaque, and `report` names an entry a route map drops.
|
|
15
|
+
*/
|
|
16
|
+
export declare function applyHelper(name: string, args: Value[], report: (message: string) => void): Value;
|
|
17
|
+
/**
|
|
18
|
+
* Joins two patterns as a route prefix does: `/` on either side leaves the other, and otherwise
|
|
19
|
+
* exactly one slash separates them.
|
|
20
|
+
*/
|
|
21
|
+
export declare function joinPatterns(base: string, pattern: string): string;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `remix/routes` helpers restated over evaluated values: method routes, `route()` with its
|
|
3
|
+
* prefix, `form()`, `resources()` and `resource()` with `only`, `exclude`, `param` and `names`.
|
|
4
|
+
* They follow the rules the helpers apply at runtime, so a listed pattern is the one the router matches.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
/** The specifiers the route helpers are imported from. */
|
|
10
|
+
const ROUTE_MODULES = new Set([
|
|
11
|
+
"remix/routes",
|
|
12
|
+
"remix/fetch-router",
|
|
13
|
+
"remix/fetch-router/routes",
|
|
14
|
+
"@remix-run/fetch-router",
|
|
15
|
+
"@remix-run/fetch-router/routes",
|
|
16
|
+
]);
|
|
17
|
+
/** Every exported helper name, shorthand and long form, to its canonical name. */
|
|
18
|
+
const HELPERS = {
|
|
19
|
+
route: "route",
|
|
20
|
+
createRoutes: "route",
|
|
21
|
+
get: "GET",
|
|
22
|
+
createGetRoute: "GET",
|
|
23
|
+
post: "POST",
|
|
24
|
+
createPostRoute: "POST",
|
|
25
|
+
put: "PUT",
|
|
26
|
+
createPutRoute: "PUT",
|
|
27
|
+
patch: "PATCH",
|
|
28
|
+
createPatchRoute: "PATCH",
|
|
29
|
+
del: "DELETE",
|
|
30
|
+
createDeleteRoute: "DELETE",
|
|
31
|
+
head: "HEAD",
|
|
32
|
+
createHeadRoute: "HEAD",
|
|
33
|
+
options: "OPTIONS",
|
|
34
|
+
createOptionsRoute: "OPTIONS",
|
|
35
|
+
form: "form",
|
|
36
|
+
createFormRoutes: "form",
|
|
37
|
+
resources: "resources",
|
|
38
|
+
createResourcesRoutes: "resources",
|
|
39
|
+
resource: "resource",
|
|
40
|
+
createResourceRoutes: "resource",
|
|
41
|
+
};
|
|
42
|
+
/** `resources()`'s actions in the order it declares them, with each one's method and pattern. */
|
|
43
|
+
const RESOURCES_ACTIONS = [
|
|
44
|
+
["index", "GET", () => "/"],
|
|
45
|
+
["new", "GET", () => "/new"],
|
|
46
|
+
["show", "GET", (param) => `/:${param}`],
|
|
47
|
+
["create", "POST", () => "/"],
|
|
48
|
+
["edit", "GET", (param) => `/:${param}/edit`],
|
|
49
|
+
["update", "PUT", (param) => `/:${param}`],
|
|
50
|
+
["destroy", "DELETE", (param) => `/:${param}`],
|
|
51
|
+
];
|
|
52
|
+
/** `resource()`'s actions, a singular resource with no index and no param. */
|
|
53
|
+
const RESOURCE_ACTIONS = [
|
|
54
|
+
["new", "GET", () => "/new"],
|
|
55
|
+
["show", "GET", () => "/"],
|
|
56
|
+
["create", "POST", () => "/"],
|
|
57
|
+
["edit", "GET", () => "/edit"],
|
|
58
|
+
["update", "PUT", () => "/"],
|
|
59
|
+
["destroy", "DELETE", () => "/"],
|
|
60
|
+
];
|
|
61
|
+
/** The opaque value. */
|
|
62
|
+
const OPAQUE = { kind: "opaque" };
|
|
63
|
+
/** The canonical helper an import binds, or `null` when it is not a route helper. */
|
|
64
|
+
export function helperName(specifier, imported) {
|
|
65
|
+
if (!ROUTE_MODULES.has(specifier))
|
|
66
|
+
return null;
|
|
67
|
+
return HELPERS[imported] ?? null;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Applies the helper `name` to evaluated arguments. An argument the reader could not evaluate
|
|
71
|
+
* leaves the result opaque, and `report` names an entry a route map drops.
|
|
72
|
+
*/
|
|
73
|
+
export function applyHelper(name, args, report) {
|
|
74
|
+
let [first, second] = args;
|
|
75
|
+
if (name === "route") {
|
|
76
|
+
if (first?.kind === "string" && second?.kind === "map")
|
|
77
|
+
return build(first.value, second, report);
|
|
78
|
+
if (first?.kind === "map")
|
|
79
|
+
return build("/", first, report);
|
|
80
|
+
return OPAQUE;
|
|
81
|
+
}
|
|
82
|
+
if (first?.kind !== "string")
|
|
83
|
+
return OPAQUE;
|
|
84
|
+
let options = second?.kind === "map" ? second : null;
|
|
85
|
+
if (name === "form") {
|
|
86
|
+
let names = mapOf(options?.entries.get("names"));
|
|
87
|
+
let method = stringOf(options?.entries.get("formMethod")) ?? "POST";
|
|
88
|
+
let defs = new Map([
|
|
89
|
+
[stringOf(names?.entries.get("index")) ?? "index", route("GET", "/")],
|
|
90
|
+
[stringOf(names?.entries.get("action")) ?? "action", route(method, "/")],
|
|
91
|
+
]);
|
|
92
|
+
return build(first.value, { kind: "map", entries: defs }, report);
|
|
93
|
+
}
|
|
94
|
+
if (name === "resources" || name === "resource") {
|
|
95
|
+
let actions = name === "resources" ? RESOURCES_ACTIONS : RESOURCE_ACTIONS;
|
|
96
|
+
let only = stringsOf(options?.entries.get("only"));
|
|
97
|
+
let exclude = stringsOf(options?.entries.get("exclude"));
|
|
98
|
+
let param = stringOf(options?.entries.get("param")) ?? "id";
|
|
99
|
+
let names = mapOf(options?.entries.get("names"));
|
|
100
|
+
let defs = new Map();
|
|
101
|
+
for (let [action, method, pattern] of actions) {
|
|
102
|
+
if (only !== null && !only.includes(action))
|
|
103
|
+
continue;
|
|
104
|
+
if (only === null && exclude !== null && exclude.includes(action))
|
|
105
|
+
continue;
|
|
106
|
+
defs.set(stringOf(names?.entries.get(action)) ?? action, route(method, pattern(param)));
|
|
107
|
+
}
|
|
108
|
+
return build(first.value, { kind: "map", entries: defs }, report);
|
|
109
|
+
}
|
|
110
|
+
return route(name, first.value);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Joins two patterns as a route prefix does: `/` on either side leaves the other, and otherwise
|
|
114
|
+
* exactly one slash separates them.
|
|
115
|
+
*/
|
|
116
|
+
export function joinPatterns(base, pattern) {
|
|
117
|
+
if (pattern === "" || pattern === "/")
|
|
118
|
+
return base === "" ? "/" : base;
|
|
119
|
+
if (base === "" || base === "/")
|
|
120
|
+
return pattern.startsWith("/") ? pattern : `/${pattern}`;
|
|
121
|
+
return `${base.replace(/\/+$/, "")}/${pattern.replace(/^\/+/, "")}`;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* A route map with `base` joined onto every route under `defs`, each a new route object. A bare
|
|
125
|
+
* string is an `ANY` route and `{ method, pattern }` a route, as `route()` reads them.
|
|
126
|
+
*/
|
|
127
|
+
function build(base, defs, report) {
|
|
128
|
+
let entries = new Map();
|
|
129
|
+
for (let [key, def] of defs.entries) {
|
|
130
|
+
if (def.kind === "route")
|
|
131
|
+
entries.set(key, route(def.method, joinPatterns(base, def.pattern)));
|
|
132
|
+
else if (def.kind === "string")
|
|
133
|
+
entries.set(key, route("ANY", joinPatterns(base, def.value)));
|
|
134
|
+
else if (def.kind === "map") {
|
|
135
|
+
let pattern = stringOf(def.entries.get("pattern"));
|
|
136
|
+
if (pattern !== null) {
|
|
137
|
+
let method = stringOf(def.entries.get("method")) ?? "ANY";
|
|
138
|
+
entries.set(key, route(method, joinPatterns(base, pattern)));
|
|
139
|
+
}
|
|
140
|
+
else
|
|
141
|
+
entries.set(key, build(base, def, report));
|
|
142
|
+
}
|
|
143
|
+
else
|
|
144
|
+
report(`\`${key}\` is declared with code sdxc routes does not evaluate; it is left out`);
|
|
145
|
+
}
|
|
146
|
+
return { kind: "map", entries };
|
|
147
|
+
}
|
|
148
|
+
/** A new route object, so each declaration has its own identity. */
|
|
149
|
+
function route(method, pattern) {
|
|
150
|
+
return { kind: "route", method, pattern };
|
|
151
|
+
}
|
|
152
|
+
/** The string a value holds, or `null` when it holds anything else. */
|
|
153
|
+
function stringOf(value) {
|
|
154
|
+
return value?.kind === "string" ? value.value : null;
|
|
155
|
+
}
|
|
156
|
+
/** The map a value holds, or `null` when it holds anything else. */
|
|
157
|
+
function mapOf(value) {
|
|
158
|
+
return value?.kind === "map" ? value : null;
|
|
159
|
+
}
|
|
160
|
+
/** The strings of an array value, or `null` when it is not an array of strings. */
|
|
161
|
+
function stringsOf(value) {
|
|
162
|
+
if (value?.kind !== "array")
|
|
163
|
+
return null;
|
|
164
|
+
return value.items.flatMap((item) => (item.kind === "string" ? [item.value] : []));
|
|
165
|
+
}
|