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 all 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
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.

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