Skip to content

Commit

Permalink
remove app.use and fix middleware tests (#3506)
Browse files Browse the repository at this point in the history
  • Loading branch information
genzyy committed Dec 4, 2021
1 parent 6975c8f commit e488a09
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 98 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ matters to you.
* <a href="./docs/Routes.md"><code><b>Routes</b></code></a>
* <a href="./docs/Encapsulation.md"><code><b>Encapsulation</b></code></a>
* <a href="./docs/Logging.md"><code><b>Logging</b></code></a>
* <a href="./docs/Middleware.md"><code><b>Middleware</b></code></a>
* <a href="./docs/Hooks.md"><code><b>Hooks</b></code></a>
* <a href="./docs/Decorators.md"><code><b>Decorators</b></code></a>
* <a href="./docs/Validation-and-Serialization.md"><code><b>Validation and Serialization</b></code></a>
Expand Down
53 changes: 0 additions & 53 deletions docs/Middleware.md

This file was deleted.

12 changes: 12 additions & 0 deletions docs/Migration-Guide-V4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# V4 Migration Guide

This guide is intended to help with migration from Fastify v3 to v4.

Before migrating to v4, please ensure that you have fixed all deprecation warningx from v3.
All v3 deprecations have been removed and they will no longer work after upgrading.

## Breaking Changes

### Deprecation of `app.use()`

Starting this version of Fastify, we have deprecated the use of `app.use()`. We have decided not to support the use of middlewares. Both [`middie`](https://github.com/fastify/middie) and [`fastify-express`](https://github.com/fastify/fastify-express) will still be there and maintained. Use Fastify's [hooks](./Hooks.md) instead.
10 changes: 0 additions & 10 deletions fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const { defaultInitOptions } = getSecuredInitialConfig

const {
FST_ERR_BAD_URL,
FST_ERR_MISSING_MIDDLEWARE,
AVVIO_ERRORS_MAP,
appendStackTrace
} = require('./lib/errors')
Expand Down Expand Up @@ -324,11 +323,6 @@ function fastify (options) {
}
})

// We are adding `use` to the fastify prototype so the user
// can still access it (and get the expected error), but `decorate`
// will not detect it, and allow the user to override it.
Object.setPrototypeOf(fastify, { use })

if (options.schemaErrorFormatter) {
validateSchemaErrorFormatter(options.schemaErrorFormatter)
fastify[kSchemaErrorFormatter] = options.schemaErrorFormatter.bind(fastify)
Expand Down Expand Up @@ -493,10 +487,6 @@ function fastify (options) {
}
}

function use () {
throw new FST_ERR_MISSING_MIDDLEWARE()
}

// Used exclusively in TypeScript contexts to enable auto type inference from JSON schema.
function withTypeProvider () {
return this
Expand Down
9 changes: 0 additions & 9 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,6 @@ const codes = {
"A callback for '%s' hook timed out. You may have forgotten to call 'done' function or to resolve a Promise"
),

/**
* Middlewares
*/
FST_ERR_MISSING_MIDDLEWARE: createError(
'FST_ERR_MISSING_MIDDLEWARE',
'You must register a plugin for handling middlewares, visit fastify.io/docs/latest/Middleware/ for more info.',
500
),

/**
* logger
*/
Expand Down
27 changes: 2 additions & 25 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,9 @@
const { test } = require('tap')
const Fastify = require('..')
const {
FST_ERR_DEC_ALREADY_PRESENT,
FST_ERR_MISSING_MIDDLEWARE
FST_ERR_DEC_ALREADY_PRESENT
} = require('../lib/errors')

test('Should throw if the basic use API has not been overridden', t => {
t.plan(1)
const fastify = Fastify()

try {
fastify.use()
t.fail('Should throw')
} catch (err) {
t.ok(err instanceof FST_ERR_MISSING_MIDDLEWARE)
}
})

test('Should be able to override the default use API', t => {
t.plan(1)
const fastify = Fastify()
Expand All @@ -38,7 +25,7 @@ test('Cannot decorate use twice', t => {
})

test('Encapsulation works', t => {
t.plan(2)
t.plan(1)
const fastify = Fastify()

fastify.register((instance, opts, done) => {
Expand All @@ -47,15 +34,5 @@ test('Encapsulation works', t => {
done()
})

fastify.register((instance, opts, done) => {
try {
instance.use()
t.fail('Should throw')
} catch (err) {
t.ok(err instanceof FST_ERR_MISSING_MIDDLEWARE)
}
done()
})

fastify.ready()
})

0 comments on commit e488a09

Please sign in to comment.