Skip to content

Commit

Permalink
fix: handle chained register (#52) (#53)
Browse files Browse the repository at this point in the history
* fix: handle chained register

* fix: handle chained routes

Co-authored-by: Davide Bianchi <[email protected]>
  • Loading branch information
Eomm and davidebianchi authored Jun 28, 2022
1 parent 52daad0 commit e6dd5c5
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 8 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
})

Expand All @@ -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
Expand Down
66 changes: 65 additions & 1 deletion test/fixture/routes.00.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}
}
]
]
66 changes: 65 additions & 1 deletion test/fixture/routes.01.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}
}
]
]
5 changes: 4 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ test('register', async t => {
})
instance.register(function register3 (instance, opts, next) {
next()
}).register(function register4 (instance, opts, next) {
next()
})
next()
})
Expand All @@ -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)
})
25 changes: 23 additions & 2 deletions test/routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down

0 comments on commit e6dd5c5

Please sign in to comment.