Automatically wrap route files
See original GitHub issueš Feature Proposal
Weāre migrating an API to fastify and somehow the boilerplate
module.exports = function (fastify, opts, next) {
fastify.route({
method: 'GET',
url: '/',
handler
})
}
feels a bit clunky. Iād like to propose just the route content in a file:
module.exports = {
method: 'GET',
url: '/',
handler
}
For most cases this is totally sufficient. If additional config/options are required, a regular plugin/route function can be exported.
Motivation
As mentioned above, the plugin structure feels a bit clunky in each and every file.
Example
The default type is a function and the new proposal would be an object (method is always required anyway), so Iām thinking somewhere along the lines of (https://github.com/fastify/fastify-autoload/blob/master/index.js#L110):
const content = require(file)
let plugin
if (content && typeof content === 'object' && content.method) {
plugin = function (fastify, opts, next) {
fastify.route(content)
next()
}
} else {
plugin = content
}
This is non-breaking, but to be safe it could also be flagged with an new option āwrapRoutesā or something like that.
Iām testing this locally, but Iām currently getting an error when running npm test
?
test/error.js ......................................... 1/2
not ok should match pattern provided
found: >-
Unexpected token '}' at
/Users/patrick/Sites/fastify-autoload/test/error/lib/a.js:6
pattern: '/Unexpected token \} at .*\/test\/error\/lib\/a.js:6/'
at:
line: 14
column: 5
file: test/error.js
stack: |
test/error.js:14:5
Object._encapsulateThreeParam (node_modules/avvio/boot.js:408:13)
Boot.callWithCbOrNextTick (node_modules/avvio/boot.js:339:5)
release (node_modules/fastq/queue.js:127:16)
Object.resume (node_modules/fastq/queue.js:61:7)
node_modules/avvio/boot.js:155:20
node_modules/avvio/plugin.js:178:7
done (node_modules/avvio/plugin.js:136:5)
check (node_modules/avvio/plugin.js:147:7)
source: |
t.match(err.message, /Unexpected token \} at .*\/test\/error\/lib\/a.js:6/)
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (8 by maintainers)
I thought this issue was on the core repo. I didnāt realize it is in the autoload plugin repo. I know nothing of how this plugin works.
Done. See #55