feat: support cloneRouteConfig option in findRoute - #6981
Open
lx3133584 wants to merge 1 commit into
Open
Conversation
Fixes #null Signed-off-by: Liang Xu <lx3133584@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds opt-in cloned route configuration access to findRoute.
Changes:
- Adds
cloneRouteConfigruntime 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.
| 'use strict' | ||
|
|
||
| const FindMyWay = require('find-my-way') | ||
| const clone = require('rfdc')({ circles: false, proto: false }) |
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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
fastify.findRoutepurposefully restricts exposed route properties to{ handler, params, searchParams }to avoid exposing the internalstoreor allowing accidental runtime mutations of route configuration. However, plugins and handlers (such as@fastify/corsresolving target route configuration during OPTIONS preflight requests) need a safe, immutable way to inspect the target route's custom configuration.Solution
cloneRouteConfigboolean option tofastify.findRoute.cloneRouteConfig: trueis passed,findRouteclones the route'sconfigusingrfdcand includes it in the returned result asconfig.types/instance.d.tsandtest/types/route.tst.ts.docs/Reference/Server.md.Testing
test/find-route.test.jsverifying cloned config retrieval and ensuring mutations on the returned config do not affect the internal route store.