Skip to content

See how your app actually fits together. An interactive, plain-English map of your codebase, drawn by Claude or Codex.

Fastify: a plugin registered without a prefix sits under its parent's

Richard Soarese59b02a419c1
diff --git a/skills/floorplan/scripts/lib/extract.mjs b/skills/floorplan/scripts/lib/extract.mjs
index 828f882..67e6235 100644
--- a/skills/floorplan/scripts/lib/extract.mjs
+++ b/skills/floorplan/scripts/lib/extract.mjs
@@ -194,9 +194,11 @@ function extractJs(src, filePath) {
if (args.length) mounts.push({ prefix: m[3], var: args[args.length - 1], line: at(m.index), ...(args.length > 1 ? { mw: args.slice(0, -1).map((s) => s.split('.').pop()) } : {}) });
}
// Fastify plugins: app.register(ticketRoutes, { prefix: '/api/tickets' }), or register(import('./x.js'), …).
+ // A plugin registered with no prefix still sits under its parent's, so it's a mount with an empty one.
// Only an imported plugin counts: one defined in this file would put its prefix on every route here.
- for (const m of all(/\.register\(\s*(?:await\s+)?(?:(?:import|require)\(\s*["']([^"'\n]+)["']\s*\)|([A-Za-z_$][\w$]*))\s*,\s*\{([^{}]*)\}/g, code)) {
- const prefix = /\bprefix\s*:\s*["'`](\/[^"'`$]*)["'`]/.exec(m[3])?.[1];
+ for (const m of all(/\.register\(\s*(?:await\s+)?(?:(?:import|require)\(\s*["']([^"'\n]+)["']\s*\)|([A-Za-z_$][\w$]*))\s*(?:,\s*\{([^{}]*)\}\s*)?\)/g, code)) {
+ const opts = m[3] || '';
+ const prefix = /\bprefix\s*:\s*["'`](\/[^"'`$]*)["'`]/.exec(opts)?.[1] ?? (/\bprefix\s*:/.test(opts) ? null : '');
if (prefix == null) continue;
if (m[1]) mounts.push({ prefix, spec: m[1], line: at(m.index) });
else if (imports.some((i) => i.locals && Object.hasOwn(i.locals, m[2]))) mounts.push({ prefix, var: m[2], line: at(m.index) });
diff --git a/tests/fastify.test.mjs b/tests/fastify.test.mjs
index 32bd630..dcc18de 100644
--- a/tests/fastify.test.mjs
+++ b/tests/fastify.test.mjs
@@ -36,21 +36,23 @@ test('register(plugin, { prefix }) is a mount: imported by name, by import() or
`});`,
`app.register(import('./routes/health.js'), { prefix: '/health' });`,
`app.register(require('./routes/legacy'), { prefix: '/legacy' });`,
+ `app.register(ticketRoutes);`,
`app.register(local, { prefix: '/local' });`,
`app.register(cors, { origin: true });`,
'app.register(ticketRoutes, { prefix: `/v${n}` });',
].join('\n'), 'typescript', 'api/app.ts');
- // a plugin defined in the same file, one with no prefix, and a prefix built at run time are left alone
+ // a plugin with no prefix sits under its parent's (an empty mount); a plugin defined in the same
+ // file, a register without a plugin of ours (cors) and a prefix built at run time are left alone
assert.deepEqual(r.mounts.map((m) => `${m.prefix} ${m.var || m.spec}`), [
- '/api/tickets ticketRoutes', '/api/invites invites', '/health ./routes/health.js', '/legacy ./routes/legacy',
+ '/api/tickets ticketRoutes', '/api/invites invites', '/health ./routes/health.js', '/legacy ./routes/legacy', ' ticketRoutes',
]);
// and a prefix isn't mistaken for a request the file makes
assert.deepEqual(r.calls, []);
});
-test('endpoints get the prefix their plugin is registered under, through nested registers', () => {
+test('endpoints get the prefix their plugin is registered under, through nested registers with or without a prefix', () => {
assert.deepEqual(routeKeys(graph), [
- 'GET /api/v1/buildings', 'GET /api/v1/buildings/:buildingId/faq', 'GET /api/v1/invites', 'GET /api/v1/tickets',
+ 'GET /api/v1/buildings', 'GET /api/v1/buildings/:buildingId/faq', 'GET /api/v1/buildings/:buildingId/notes', 'GET /api/v1/invites', 'GET /api/v1/tickets',
'GET /api/v1/tickets/:id', 'GET /health', 'POST /api/v1/invites/accept', 'POST /api/v1/tickets/:id/accept',
]);
});
diff --git a/tests/fixtures/fastify-app/api/src/routes/buildings/index.ts b/tests/fixtures/fastify-app/api/src/routes/buildings/index.ts
index a774021..b7ffbc2 100644
--- a/tests/fixtures/fastify-app/api/src/routes/buildings/index.ts
+++ b/tests/fixtures/fastify-app/api/src/routes/buildings/index.ts
@@ -1,8 +1,11 @@
import type { FastifyInstance } from 'fastify';
import faqRoutes from './faq.js';
+import noteRoutes from './notes.js';
export default async function buildingRoutes(app: FastifyInstance) {
app.get('/', async () => []);
// a plugin registered inside a plugin: its routes sit under both prefixes
await app.register(faqRoutes, { prefix: '/:buildingId/faq' });
+ // registered with no prefix of its own: its routes sit under this plugin's
+ await app.register(noteRoutes);
}
diff --git a/tests/fixtures/fastify-app/api/src/routes/buildings/notes.ts b/tests/fixtures/fastify-app/api/src/routes/buildings/notes.ts
new file mode 100644
index 0000000..7962c63
--- /dev/null
+++ b/tests/fixtures/fastify-app/api/src/routes/buildings/notes.ts
@@ -0,0 +1,5 @@
+import type { FastifyInstance } from 'fastify';
+
+export default async function noteRoutes(app: FastifyInstance) {
+ app.get('/:buildingId/notes', async () => []);
+}