Skip to content

Commit

Permalink
lib: deprecate fastify default route
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGSS committed Dec 26, 2022
1 parent 04a743a commit ae2632b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
13 changes: 10 additions & 3 deletions docs/Reference/Server.md
Expand Up @@ -1011,6 +1011,9 @@ Note that the array contains the `fastify.server.address()` too.
#### getDefaultRoute
<a id="getDefaultRoute"></a>

**Notice**: this method is deprecated and should be removed in the next Fastify
major version.

The `defaultRoute` handler handles requests that do not match any URL specified
by your Fastify application. This defaults to the 404 handler, but can be
overridden with [setDefaultRoute](#setdefaultroute). Method to get the
Expand All @@ -1023,15 +1026,19 @@ const defaultRoute = fastify.getDefaultRoute()
#### setDefaultRoute
<a id="setDefaultRoute"></a>

**Note**: The default 404 handler, or one set using `setNotFoundHandler`, will
**Notice**: this method is deprecated and should be removed in the next Fastify
major version. Please, consider to use `setNotFoundHandler` or a wildcard
matching route.

The default 404 handler, or one set using `setNotFoundHandler`, will
never trigger if the default route is overridden. This sets the handler for the
Fastify application, not just the current instance context. Use
[setNotFoundHandler](#setnotfoundhandler) if you want to customize 404 handling
instead.

This method set the `defaultRoute` for the server. Note that, its purpose is
This method sets the `defaultRoute` for the server. Note that, its purpose is
to interact with the underlying raw requests. Unlike other Fastify handlers, the
arguments received are from type [RawRequest](./TypeScript.md#rawrequest) and
arguments received are of type [RawRequest](./TypeScript.md#rawrequest) and
[RawReply](./TypeScript.md#rawreply) respectively.

```js
Expand Down
2 changes: 2 additions & 0 deletions lib/route.js
Expand Up @@ -89,9 +89,11 @@ function buildRouting (options) {
hasRoute,
prepareRoute,
getDefaultRoute: function () {
warning.emit('FSTDEP014')
return router.defaultRoute
},
setDefaultRoute: function (defaultRoute) {
warning.emit('FSTDEP014')
if (typeof defaultRoute !== 'function') {
throw new FST_ERR_DEFAULT_ROUTE_INVALID_TYPE()
}
Expand Down
2 changes: 2 additions & 0 deletions lib/warnings.js
Expand Up @@ -25,4 +25,6 @@ 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 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.')

module.exports = warning
20 changes: 20 additions & 0 deletions test/default-route.test.js
Expand Up @@ -4,6 +4,26 @@ const t = require('tap')
const test = t.test
const Fastify = require('..')

test('setDefaultRoute should emit a deprecation warning', t => {
t.plan(2)

const fastify = Fastify()
const defaultRoute = (req, res) => {
res.end('hello from defaultRoute')
}

process.on('warning', onWarning)
function onWarning (warning) {
t.equal(warning.name, 'FastifyDeprecation')
t.equal(warning.code, 'FSTDEP014')
}

t.teardown(() => process.removeListener('warning', onWarning))

fastify.setDefaultRoute(defaultRoute)
fastify.getDefaultRoute()
})

test('should fail if defaultRoute is not a function', t => {
t.plan(1)

Expand Down

0 comments on commit ae2632b

Please sign in to comment.