From 64c11be5e5cee963ba9ef2508f991d402ec8694a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s-Combarro?= Date: Wed, 28 Aug 2019 21:12:41 +0200 Subject: [PATCH] Removal of check that plugin functions need to have 3 arguments (#201) --- README.md | 2 +- test/data/incorrect-plugin.js | 12 ------------ test/print-routes.test.js | 12 ------------ test/start.test.js | 13 ------------- util.js | 9 --------- 5 files changed, 1 insertion(+), 47 deletions(-) delete mode 100644 test/data/incorrect-plugin.js diff --git a/README.md b/README.md index 415bb6c1..68a38165 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ module.exports = function (fastify, options, next) { } ``` -If you are using Node 8+, you can use `async` functions too: +If you are using Node 8+, you can use `Promises` or `async` functions too: ```js // async-await-plugin.js diff --git a/test/data/incorrect-plugin.js b/test/data/incorrect-plugin.js deleted file mode 100644 index 6d913f1f..00000000 --- a/test/data/incorrect-plugin.js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict' - -// This is a counter example. WILL NOT WORK! -// You must have 3 arguments in your plugin function. -// Reference examples/plugin.js or examples/plugin-with-options.js -// for a correct example of plugin declaration -module.exports = function (fastify, next) { - fastify.get('/', function (req, reply) { - reply.send({ wont: 'work' }) - }) - next() -} diff --git a/test/print-routes.test.js b/test/print-routes.test.js index 430b9644..f1e764aa 100644 --- a/test/print-routes.test.js +++ b/test/print-routes.test.js @@ -39,18 +39,6 @@ test('should warn on file not found', t => { printRoutes.printRoutes(argv) }) -test('should only accept plugin functions with 3 arguments', t => { - t.plan(1) - - const oldStop = printRoutes.stop - t.tearDown(() => { printRoutes.stop = oldStop }) - printRoutes.stop = function (err) { - t.equal(err.message, 'Plugin function should contain 3 arguments. Refer to documentation for more information.') - } - - printRoutes.printRoutes(['./test/data/incorrect-plugin.js']) -}) - test('should throw on package not found', t => { t.plan(1) diff --git a/test/start.test.js b/test/start.test.js index a95cdd8b..509be1f2 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -114,19 +114,6 @@ test('should start fastify at given socket path', { skip: process.platform === ' }) }) -test('should only accept plugin functions with 3 arguments', t => { - t.plan(1) - - const oldStop = start.stop - t.tearDown(() => { start.stop = oldStop }) - start.stop = function (err) { - t.equal(err.message, 'Plugin function should contain 3 arguments. Refer to documentation for more information.') - } - - const argv = ['-p', getPort(), './test/data/incorrect-plugin.js'] - start.start(argv) -}) - test('should error with a good timeout value', t => { t.plan(1) diff --git a/util.js b/util.js index 454779fd..5b74dc21 100644 --- a/util.js +++ b/util.js @@ -27,10 +27,6 @@ function requireFastifyForModule (modulePath) { } } -function isInvalidCallbackPlugin (plugin) { - return plugin.length !== 3 && plugin.constructor.name === 'Function' -} - function isInvalidAsyncPlugin (plugin) { return plugin.length !== 2 && plugin.constructor.name === 'AsyncFunction' } @@ -44,11 +40,6 @@ function requireServerPluginFromPath (modulePath) { const serverPlugin = require(resolvedModulePath) - if (isInvalidCallbackPlugin(serverPlugin)) { - throw new Error('Plugin function should contain 3 arguments. Refer to ' + - 'documentation for more information.') - } - if (isInvalidAsyncPlugin(serverPlugin)) { return new Error('Async/Await plugin function should contain 2 arguments.' + 'Refer to documentation for more information.')