Skip to content

Commit

Permalink
feat: access handler name add properties to req route options (#4470)
Browse files Browse the repository at this point in the history
* fix: 'Error' is not assignable to parameter of type 'null'

* chore: add handler, schema and config to req.routeOptions

* rollback not needed change

* Update lib/request.js

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>

* chore: use deepFreeze

Signed-off-by: cesarvspr <vinicius_spr@hotmail.com>

* chore: use Object.defineProperty

Signed-off-by: cesarvspr <vinicius_spr@hotmail.com>

* chore: use defineProperties

Signed-off-by: cesarvspr <vinicius_spr@hotmail.com>

* chore: add tests

Signed-off-by: cesarvspr <vinicius_spr@hotmail.com>

* fix: test count exceeds plan

Signed-off-by: cesarvspr <vinicius_spr@hotmail.com>

* fix: test count exceeds plan

Signed-off-by: cesarvspr <vinicius_spr@hotmail.com>

* fix: add TODO

Signed-off-by: cesarvspr <vinicius_spr@hotmail.com>

* chore: review improvements

* fix: proper warning codes

* Update test/request.deprecated.test.js

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>

* Apply suggestions from code review

* add documentation

* add missing documentation

* Update docs/Reference/Request.md

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>

---------

Signed-off-by: cesarvspr <vinicius_spr@hotmail.com>
Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>
Co-authored-by: Frazer Smith <frazer.dev@outlook.com>
Co-authored-by: Uzlopak <aras.abbasi@googlemail.com>
Co-authored-by: Carlos Fuentes <me@metcoder.dev>
  • Loading branch information
5 people committed Sep 4, 2023
1 parent 7053ea7 commit 59050e5
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 13 deletions.
18 changes: 11 additions & 7 deletions docs/Reference/Request.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,32 @@ Request is a core Fastify object containing the following fields:
- `url` - the URL of the incoming request
- `originalUrl` - similar to `url`, this allows you to access the
original `url` in case of internal re-routing
- `routerMethod` - the method defined for the router that is handling the
request
- `routerPath` - the path pattern defined for the router that is handling the
request
- `routerMethod` - Deprecated, use `request.routeOptions.method` instead. The
method defined for the router that is handling the request
- `routerPath` - Deprecated, use `request.routeOptions.config.url` instead. The
path pattern defined for the router that is handling the request
- `is404` - true if request is being handled by 404 handler, false if it is not
- `connection` - Deprecated, use `socket` instead. The underlying connection of
the incoming request.
- `socket` - the underlying connection of the incoming request
- `context` - A Fastify internal object. You should not use it directly or
modify it. It is useful to access one special key:
- `context.config` - The route [`config`](./Routes.md#routes-config) object.
- `routeSchema` - the scheme definition set for the router that is
handling the request
- `routeConfig` - The route [`config`](./Routes.md#routes-config)
- `routeSchema` - Deprecated, use `request.routeOptions.schema` instead. The
scheme definition set for the router that is handling the request
- `routeConfig` - Deprecated, use `request.routeOptions.config` instead. The
route [`config`](./Routes.md#routes-config)
object.
- `routeOptions` - The route [`option`](./Routes.md#routes-options) object
- `bodyLimit` - either server limit or route limit
- `config` - the [`config`](./Routes.md#routes-config) object for this route
- `method` - the http method for the route
- `url` - the path of the URL to match this route
- `handler` - the handler for this route
- `attachValidation` - attach `validationError` to request
(if there is a schema defined)
- `logLevel` - log level defined for this route
- `schema` - the JSON schemas definition for this route
- `version` - a semver compatible string that defines the version of the endpoint
- `exposeHeadRoute` - creates a sibling HEAD route for any GET routes
- `prefixTrailingSlash` - string used to determine how to handle passing /
Expand Down
15 changes: 15 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 @@ -189,23 +190,37 @@ Object.defineProperties(Request.prototype, {
logLevel: context.logLevel,
exposeHeadRoute: context.exposeHeadRoute,
prefixTrailingSlash: context.prefixTrailingSlash,
handler: context.handler,
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.

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')
t.equal(req.is404, false)
reply.send({ hello: 'world' })
})
Expand Down

0 comments on commit 59050e5

Please sign in to comment.