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 20 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
16 changes: 16 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ Object.defineProperties(Request.prototype, {
},
routerPath: {
get () {
warning.emit('FSTDEP017')
return this[kRouteContext].config?.url
}
},
Expand All @@ -181,6 +182,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 } = context
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
const options = {
method: context.config?.method,
url: context.config?.url,
Expand All @@ -189,23 +191,37 @@ Object.defineProperties(Request.prototype, {
logLevel: context.logLevel,
exposeHeadRoute: context.exposeHeadRoute,
prefixTrailingSlash: context.prefixTrailingSlash,
handler,
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
version
}

Object.defineProperties(options, {
config: {
get: () => context.config
},
schema: {
get: () => context.schema
}
})

return Object.freeze(options)
}
},
routerMethod: {
get () {
warning.emit('FSTDEP018')
return this[kRouteContext].config?.method
}
},
routeConfig: {
get () {
warning.emit('FSTDEP016')
return this[kRouteContext][kPublicRouteContext]?.config
}
},
routeSchema: {
get () {
warning.emit('FSTDEP015')
return this[kRouteContext][kPublicRouteContext].schema
}
},
Expand Down
8 changes: 8 additions & 0 deletions lib/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ warning.create('FastifyDeprecation', 'FSTDEP013', 'Direct return of "trailers" f

warning.create('FastifyDeprecation', 'FSTDEP014', 'You are trying to set/access the default route. This property is deprecated. Please, use setNotFoundHandler if you want to custom a 404 handler or the wildcard (*) to match all routes.')

warning.create('FastifyDeprecation', 'FSTDEP015', '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', 'FSTDEP016', 'You are accessing the deprecated "request.routeConfig" property. Use "request.routeOptions.config" instead. Property "req.routeConfig" will be removed in `fastify@5`.')

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

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

warning.create('FastifyWarning', 'FSTWRN001', 'The %s schema for %s: %s is missing. This may indicate the schema is not well specified.', { unlimited: true })

module.exports = warning
38 changes: 38 additions & 0 deletions test/request.deprecated.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

// Tests for some deprecated `request.*` keys. This file should be
// removed when the deprecation is complete.

Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
process.removeAllListeners('warning')

const test = require('tap').test
const Fastify = require('../')

test('Should expose router options via getters on request and reply', t => {
t.plan(7)
const fastify = Fastify()
const expectedSchema = {
params: {
id: { type: 'integer' }
}
}

fastify.get('/test/:id', {
schema: expectedSchema
}, (req, reply) => {
t.equal(req.routeConfig.url, '/test/:id')
t.equal(req.routeConfig.method, 'GET')
t.same(req.routeSchema, expectedSchema)
t.equal(req.routerPath, '/test/:id')
t.equal(req.routerMethod, 'GET')
reply.send()
})

fastify.inject({
method: 'GET',
url: '/test/123456789'
}, (error, res) => {
t.error(error)
t.equal(res.statusCode, 200)
})
})
11 changes: 5 additions & 6 deletions test/router-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ test('Should honor maxParamLength option', t => {
})

test('Should expose router options via getters on request and reply', t => {
t.plan(10)
t.plan(9)
const fastify = Fastify()
const expectedSchema = {
params: {
Expand All @@ -105,11 +105,10 @@ test('Should expose router options via getters on request and reply', t => {
}, (req, reply) => {
t.equal(reply.context.config.url, '/test/:id')
t.equal(reply.context.config.method, 'GET')
t.equal(req.routeConfig.url, '/test/:id')
t.equal(req.routeConfig.method, 'GET')
t.same(req.routeSchema, expectedSchema)
t.equal(req.routerPath, '/test/:id')
t.equal(req.routerMethod, 'GET')
t.same(req.routeOptions.schema, expectedSchema)
t.equal(typeof req.routeOptions.handler, 'function')
t.equal(req.routeOptions.config.url, '/test/:id')
t.equal(req.routeOptions.config.method, 'GET')
Eomm marked this conversation as resolved.
Show resolved Hide resolved
t.equal(req.is404, false)
reply.send({ hello: 'world' })
})
Expand Down