Skip to content

feat: support cloneRouteConfig option in findRoute - #6981

Open
lx3133584 wants to merge 1 commit into
fastify:mainfrom
lx3133584:feat/find-route-clone-config
Open

feat: support cloneRouteConfig option in findRoute#6981
lx3133584 wants to merge 1 commit into
fastify:mainfrom
lx3133584:feat/find-route-clone-config

Conversation

@lx3133584

Copy link
Copy Markdown

Problem

fastify.findRoute purposefully restricts exposed route properties to { handler, params, searchParams } to avoid exposing the internal store or allowing accidental runtime mutations of route configuration. However, plugins and handlers (such as @fastify/cors resolving target route configuration during OPTIONS preflight requests) need a safe, immutable way to inspect the target route's custom configuration.

Solution

  • Added cloneRouteConfig boolean option to fastify.findRoute.
  • When cloneRouteConfig: true is passed, findRoute clones the route's config using rfdc and includes it in the returned result as config.
  • Updated TypeScript definitions in types/instance.d.ts and test/types/route.tst.ts.
  • Updated docs/Reference/Server.md.

Testing

  • Added unit tests in test/find-route.test.js verifying cloned config retrieval and ensuring mutations on the returned config do not affect the internal route store.
  • Verified TypeScript type tests and eslint pass cleanly.

Fixes #null

Signed-off-by: Liang Xu <lx3133584@users.noreply.github.com>
@github-actions github-actions Bot added documentation Improvements or additions to documentation typescript TypeScript related labels Aug 27, 2026
@Fdawgs
Fdawgs requested a balanced review from Copilot August 28, 2026 14:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds opt-in cloned route configuration access to findRoute.

Changes:

  • Adds cloneRouteConfig runtime support and tests.
  • Updates TypeScript declarations and type tests.
  • Documents the new option.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
lib/route.js Clones route config when requested.
types/instance.d.ts Types the new option and result.
test/find-route.test.js Tests cloned config behavior.
test/types/route.tst.ts Tests TypeScript signatures.
docs/Reference/Server.md Documents config retrieval.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread lib/route.js
'use strict'

const FindMyWay = require('find-my-way')
const clone = require('rfdc')({ circles: false, proto: false })
Comment thread test/find-route.test.js
Comment on lines +215 to +222
// Ensure modifying cloned config does not mutate original route context
routeWithConfig.config.custom = 'mutated'
const secondFind = fastify.findRoute({
method: 'GET',
url: '/artists/:artistId',
cloneRouteConfig: true
})
t.assert.deepStrictEqual(secondFind.config.custom, 'value')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation typescript TypeScript related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants