From e6dd5c558001b8566077ea74796a0378bac579af Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Tue, 28 Jun 2022 08:39:11 +0200 Subject: [PATCH] fix: handle chained register (#52) (#53) * fix: handle chained register * fix: handle chained routes Co-authored-by: Davide Bianchi --- index.js | 6 ++-- test/fixture/routes.00.json | 66 ++++++++++++++++++++++++++++++++++++- test/fixture/routes.01.json | 66 ++++++++++++++++++++++++++++++++++++- test/index.test.js | 5 ++- test/routes.test.js | 25 ++++++++++++-- 5 files changed, 160 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index d42e243..b963543 100644 --- a/index.js +++ b/index.js @@ -117,7 +117,7 @@ function wrapFastify (instance, pluginOpts) { // this Symbol is processed by the `onRegister` hook if necessary pluginFn[kSourceRegister] = getSource()[0] } - originalRegister.call(this, pluginFn, opts) + return originalRegister.call(this, pluginFn, opts) } // *** routes @@ -137,7 +137,7 @@ function wrapFastify (instance, pluginOpts) { // this Symbol is processed by the `onRoute` hook getRouteHandler(url, opts, handler)[kSourceRoute] = getSource()[0] } - originalMethod.call(this, url, opts, handler) + return originalMethod.call(this, url, opts, handler) } }) @@ -147,7 +147,7 @@ function wrapFastify (instance, pluginOpts) { // this Symbol is processed by the `onRoute` hook routeOpts.handler[kSourceRoute] = getSource()[0] } - originalRoute.call(this, routeOpts) + return originalRoute.call(this, routeOpts) } // *** hooks diff --git a/test/fixture/routes.00.json b/test/fixture/routes.00.json index 5c08f5d..6207197 100644 --- a/test/fixture/routes.00.json +++ b/test/fixture/routes.00.json @@ -62,5 +62,69 @@ "onResponse": [], "onTimeout": [] } + }, + { + "method": "GET", + "url": "/get-chain", + "prefix": "", + "hooks": { + "onRequest": [], + "preParsing": [], + "preValidation": [], + "preHandler": [], + "preSerialization": [], + "onError": [], + "onSend": [], + "onResponse": [], + "onTimeout": [] + } + }, + { + "method": "POST", + "url": "/post-chain", + "prefix": "", + "hooks": { + "onRequest": [], + "preParsing": [], + "preValidation": [], + "preHandler": [], + "preSerialization": [], + "onError": [], + "onSend": [], + "onResponse": [], + "onTimeout": [] + } + }, + { + "method": "GET", + "url": "/route-get-chain", + "prefix": "", + "hooks": { + "onRequest": [], + "preParsing": [], + "preValidation": [], + "preHandler": [], + "preSerialization": [], + "onError": [], + "onSend": [], + "onResponse": [], + "onTimeout": [] + } + }, + { + "method": "POST", + "url": "/route-post-chain", + "prefix": "", + "hooks": { + "onRequest": [], + "preParsing": [], + "preValidation": [], + "preHandler": [], + "preSerialization": [], + "onError": [], + "onSend": [], + "onResponse": [], + "onTimeout": [] + } } -] \ No newline at end of file +] diff --git a/test/fixture/routes.01.json b/test/fixture/routes.01.json index e95efb4..e55a11d 100644 --- a/test/fixture/routes.01.json +++ b/test/fixture/routes.01.json @@ -46,5 +46,69 @@ "onResponse": [], "onTimeout": [] } + }, + { + "method": "GET", + "url": "/prefix/get-chain", + "prefix": "/prefix", + "hooks": { + "onRequest": [], + "preParsing": [], + "preValidation": [], + "preHandler": [], + "preSerialization": [], + "onError": [], + "onSend": [], + "onResponse": [], + "onTimeout": [] + } + }, + { + "method": "POST", + "url": "/prefix/post-chain", + "prefix": "/prefix", + "hooks": { + "onRequest": [], + "preParsing": [], + "preValidation": [], + "preHandler": [], + "preSerialization": [], + "onError": [], + "onSend": [], + "onResponse": [], + "onTimeout": [] + } + }, + { + "method": "GET", + "url": "/prefix/route-get-chain", + "prefix": "/prefix", + "hooks": { + "onRequest": [], + "preParsing": [], + "preValidation": [], + "preHandler": [], + "preSerialization": [], + "onError": [], + "onSend": [], + "onResponse": [], + "onTimeout": [] + } + }, + { + "method": "POST", + "url": "/prefix/route-post-chain", + "prefix": "/prefix", + "hooks": { + "onRequest": [], + "preParsing": [], + "preValidation": [], + "preHandler": [], + "preSerialization": [], + "onError": [], + "onSend": [], + "onResponse": [], + "onTimeout": [] + } } -] \ No newline at end of file +] diff --git a/test/index.test.js b/test/index.test.js index 76e31da..5a53f9c 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -62,6 +62,8 @@ test('register', async t => { }) instance.register(function register3 (instance, opts, next) { next() + }).register(function register4 (instance, opts, next) { + next() }) next() }) @@ -84,8 +86,9 @@ test('register', async t => { const reg1 = root.children[0] t.type(reg1.id, 'number') - t.equal(reg1.children.length, 2) + t.equal(reg1.children.length, 3) t.equal(reg1.children[0].name, 'register2') t.equal(reg1.children[1].name, 'register3') + t.equal(reg1.children[2].name, 'register4') t.equal(reg1.hooks.onRequest.length, 1) }) diff --git a/test/routes.test.js b/test/routes.test.js index 5953ac0..d9dae8e 100644 --- a/test/routes.test.js +++ b/test/routes.test.js @@ -17,11 +17,32 @@ test('routes', async t => { app.post('/post', noop) app.put('/put', noop) app.options('/options', noop) + app.get('/get-chain', noop).post('/post-chain', noop) + app.route({ + method: 'GET', + url: '/route-get-chain', + handler: noop + }).route({ + method: 'POST', + url: '/route-post-chain', + handler: noop + }) app.register(function register1 (instance, opts, next) { instance.get('/get', noop) instance.post('/post', noop) instance.put('/put', noop) + instance.get('/get-chain', noop).post('/post-chain', noop) + instance.route({ + method: 'GET', + url: '/route-get-chain', + handler: noop + }).route({ + method: 'POST', + url: '/route-post-chain', + handler: noop + }) + instance.register(function register3 (sub, opts, next) { sub.get('/hooks', { preHandler: [hook1, hook2] @@ -45,11 +66,11 @@ test('routes', async t => { t.equal(root.children.length, 2) t.equal(root.children[0].name, 'register1') t.equal(root.children[1].name, 'sibling') - t.equal(root.routes.length, 4) + t.equal(root.routes.length, 8) t.same(root.routes, require('./fixture/routes.00.json')) const reg1 = root.children[0] - t.same(reg1.routes.length, 3) + t.same(reg1.routes.length, 7) t.same(reg1.routes, require('./fixture/routes.01.json')) const reg2 = reg1.children[0]