Skip to content

feat: detect object-form Fastify route declarations - #34

Merged
haydenschmitty merged 6 commits into
haydenschmitty:mainfrom
Bl0ck154:feat/fastify-route-object
Nov 15, 2026
Merged

haydenschmitty merged 6 commits into
haydenschmitty:mainfrom
Bl0ck154:feat/fastify-route-object

Conversation

@Bl0ck154

@Bl0ck154 Bl0ck154 commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Complementary to #30 and PR #31: PR #31 resolves prefixes for registered plugins, while this handles a different unsupported declaration shape, fastify.route({ method, url, handler }).

  • Detect static method/url or path properties; supports a method array.
  • Skip computed values and avoid nested schema properties being mistaken for top-level routes.
  • Check end-to-end website call-to-route matching.
  • No dependencies.

Tests: npm test (164 passed, 1 skipped). See fork PR Bl0ck154#2.

@haydenschmitty

Copy link
Copy Markdown
Owner

Thanks @Bl0ck154. The parsing looks good and the tests pass, but there's one thing to fix before we can merge: routes in this form that do have a sign-in check get reported as open.

Health reads the check on app.post('/x', { preHandler: requireAuth }, handler), but not when it's inside the object. On a small sample app with this PR:

app.post('/api/plain-guarded', { preHandler: requireAuth }, handler);  // left alone, correct
app.route({ method: 'POST', url: '/api/object-prehandler', preHandler: requireAuth, handler });  // reported, wrong
app.route({ method: 'POST', url: '/api/object-onrequest', onRequest: [app.authenticate], handler });  // reported, wrong
app.route({ method: 'POST', url: '/api/object-open', handler });  // reported, correct

The wrong ones show as "Anyone could change data", and the same happens when the object spans several lines. We've just released a batch of false-alarm fixes in 0.6.2, so we can't bring this kind back. Please:

  1. Read the checks inside the route object. Have jsRoutes in scripts/lib/health.mjs treat preHandler, onRequest and preValidation in the object the way it treats them in the plain form, on one line or several, as a single function or an array.
  2. Add tests next to your others, using near misses as well: a route with no check is still reported, a check on one route doesn't cover the route beside it, and the word preHandler inside a nested schema doesn't count as a check.

main is moving today, so please merge it in when you're done. We'll take care of any CHANGELOG.md conflict.

… neighbors

Signed-off-by: Bl0ck154 <36800583+Bl0ck154@users.noreply.github.com>
Signed-off-by: Bl0ck154 <36800583+Bl0ck154@users.noreply.github.com>
@Bl0ck154

Bl0ck154 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the sample. I updated #34 to read preHandler, onRequest and preValidation from the top level of app.route({ ... }). Nested schema fields are ignored, and a guard on one route does not carry over to the next. Added a fixture for protected, open and near-miss routes, including a multiline hook array. Merged current main; npm test: 205 passed, 1 skipped. GitHub now shows the PR as mergeable with six green checks.

@Bl0ck154

Copy link
Copy Markdown
Contributor Author

Brought this up to date with main and resolved the conflicts. The new Fastify tests passed in GitHub Actions. Could you take another look?

@Bl0ck154

Copy link
Copy Markdown
Contributor Author

Updated this for the latest main and kept the new guard-name checks. All six Node/Linux/Windows jobs passed on 04fb6d5. Could you re-review?

@haydenschmitty
haydenschmitty merged commit 5e58544 into haydenschmitty:main Nov 15, 2026
6 checks passed
haydenschmitty added a commit that referenced this pull request Nov 15, 2026
The object-form route reader only looked at receivers with fixed names, so
`const api = Fastify(); api.route({...})` lost its endpoints. It now uses the
same test as app.get(): a router this file made, or a name that reads as one.
A route object's own `url:` line was also read as a request from the server
to itself; those are dropped. Adds the test file to the tests table and a
plain-language changelog line crediting #34.
haydenschmitty added a commit that referenced this pull request Nov 15, 2026
#29 made any object with method, path and a handle, handler, action, resolve
or fetch key an endpoint, anywhere, so a website's list of the requests it
makes ({ method, path, action: 'createOrder' }, or resolve and fetch
functions) became endpoints. Now the value must be a function, read by the
same tableHandler as keyed tables, and action, resolve and fetch count only
in a file that serves (Bun.serve, Deno.serve, createServer, listen(port),
`export default { fetch }`, a router or other routes) or beside an entry
with handle or handler.

Listed and Bun.serve routes use main's `table` framework, so Health and the
handler docs treat them as keyed tables already were. A Fastify
`app.route({ method, path, handler })` read by #34's code isn't counted again
by the list reader, a route's own path line isn't a request from its own
file, and Health finds the code after an Express route by the next route
below it rather than the next one listed, which could be a table entry above.
@haydenschmitty

Copy link
Copy Markdown
Owner

Merged and released in 0.10.0. I also made it find route objects on routers with any name (const api = Fastify(); api.route({...})). Thank you!

Update with claude plugin update floorplan@floorplan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants