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

remove app.use and fix middleware tests #3506

Merged
merged 23 commits into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
53 changes: 0 additions & 53 deletions docs/Middleware.md

This file was deleted.

4 changes: 4 additions & 0 deletions docs/Migration-Guide-V3.md
Expand Up @@ -32,6 +32,10 @@ await fastify.register(require('fastify-express'));
fastify.use(require('cors')());
```

**v4:**

Starting v4 we have deprecated the use of `.use`. Both `middie` and `fastify-express` will still be there and maintained.
genzyy marked this conversation as resolved.
Show resolved Hide resolved

### Changed logging serialization ([#2017](https://github.com/fastify/fastify/pull/2017))

The logging [Serializers](Logging.md) have been updated to now Fastify
Expand Down
10 changes: 0 additions & 10 deletions fastify.js
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
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
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()
})