Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: access handler name add properties to req route options #4470

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b55808e
fix: 'Error' is not assignable to parameter of type 'null'
cesarvspr Dec 15, 2022
c8b3e19
chore: add handler, schema and config to req.routeOptions
cesarvspr Dec 17, 2022
8adce4d
rollback not needed change
cesarvspr Dec 17, 2022
59236db
Update lib/request.js
cesarvspr Dec 19, 2022
e617fb8
chore: use deepFreeze
cesarvspr Dec 19, 2022
7b13dd8
chore: use Object.defineProperty
cesarvspr Dec 24, 2022
5012a19
chore: use defineProperties
cesarvspr Dec 24, 2022
e3b72dc
chore: add tests
cesarvspr Dec 24, 2022
61dcac5
fix: test count exceeds plan
cesarvspr Dec 24, 2022
b8e10f8
fix: test count exceeds plan
cesarvspr Dec 24, 2022
9cf1318
fix: add TODO
cesarvspr Dec 24, 2022
398b158
chore: review improvements
cesarvspr Dec 24, 2022
d869181
Merge branch 'main' of github.com:fastify/fastify into (#4439)-Access…
cesarvspr Dec 28, 2022
c0b4b34
fix: proper warning codes
cesarvspr Dec 28, 2022
81a87aa
Merge branch 'main' into (#4439)-Access-handler-name-add-properties-t…
Eomm Mar 12, 2023
acc8b31
Merge branch 'main' into (#4439)-Access-handler-name-add-properties-t…
Fdawgs Mar 13, 2023
b9ad2b5
Merge branch 'main' into (#4439)-Access-handler-name-add-properties-t…
Eomm May 14, 2023
3a64cef
Update test/request.deprecated.test.js
Uzlopak Jul 8, 2023
5eb061b
Merge branch 'main' into (#4439)-Access-handler-name-add-properties-t…
Uzlopak Aug 6, 2023
c13caa1
Merge branch 'main' into (#4439)-Access-handler-name-add-properties-t…
Uzlopak Aug 21, 2023
3e3d05c
Apply suggestions from code review
Uzlopak Aug 21, 2023
8e5602e
add documentation
Uzlopak Aug 21, 2023
1c1901d
add missing documentation
Uzlopak Aug 21, 2023
232cc3e
Update docs/Reference/Request.md
Uzlopak Aug 23, 2023
53ecf4a
Merge branch 'main' into (#4439)-Access-handler-name-add-properties-t…
metcoder95 Aug 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
kPublicRouteContext
} = require('./symbols')
const { FST_ERR_REQ_INVALID_VALIDATION_INVOCATION } = require('./errors')
const { deepFreezeObject } = require('./initialConfigValidation').utils

const HTTP_PART_SYMBOL_MAP = {
body: kSchemaBody,
Expand Down Expand Up @@ -171,6 +172,7 @@ Object.defineProperties(Request.prototype, {
const routeLimit = context._parserOptions.limit
const serverLimit = context.server.initialConfig.bodyLimit
const version = context.server.hasConstraintStrategy('version') ? this.raw.headers['accept-version'] : undefined
const { handler, config } = context
Copy link
Member

Choose a reason for hiding this comment

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

You are taking the reference here and config is an object.
When you deep frozen the object, altering config in else where will be break.

The getter should be immutable, but the hidden value should be allowed to modify by the core.

Copy link
Contributor Author

@cesarvspr cesarvspr Dec 24, 2022

Choose a reason for hiding this comment

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

ptal

const options = {
method: context.config.method,
url: context.config.url,
Expand All @@ -179,9 +181,12 @@ Object.defineProperties(Request.prototype, {
logLevel: context.logLevel,
exposeHeadRoute: context.exposeHeadRoute,
prefixTrailingSlash: context.prefixTrailingSlash,
version
schema: context[kPublicRouteContext].schema,
handler,
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
version,
config
}
return Object.freeze(options)
return deepFreezeObject(options)
}
},
routerMethod: {
Expand All @@ -191,11 +196,13 @@ Object.defineProperties(Request.prototype, {
},
routeConfig: {
get () {
warning.emit('FSTDEP015')
return this[kRouteContext][kPublicRouteContext].config
}
},
routeSchema: {
get () {
warning.emit('FSTDEP014')
Eomm marked this conversation as resolved.
Show resolved Hide resolved
return this[kRouteContext][kPublicRouteContext].schema
}
},
Expand Down
4 changes: 4 additions & 0 deletions lib/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ warning.create('FastifyDeprecation', 'FSTDEP012', 'Request#context property acce

warning.create('FastifyDeprecation', 'FSTDEP013', 'Direct return of "trailers" function is deprecated. Please use "callback" or "async-await" for return value. The support of direct return will removed in `fastify@5`.')

warning.create('FastifyDeprecation', 'FSTDEP014', 'You are accessing the deprecated "request.routeSchema" property. Use "request.routeOptions.schema" instead. Property "req.routeSchema" will be removed in `fastify@5`.')

warning.create('FastifyDeprecation', 'FSTDEP015', 'You are accessing the deprecated "request.routeConfig" property. Use "request.routeOptions.config" instead. Property "req.routeConfig" will be removed in `fastify@5`.')

module.exports = warning