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

Allow autoConfig with callback #381

Merged
merged 5 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ FASTIFY_AUTOLOAD_TYPESCRIPT=1 node --loader=my-custom-loader index.ts

Each plugin can be individually configured using the following module properties:

- `plugin.autoConfig` - Configuration object which will be used as `opts` parameter
- `plugin.autoConfig` - Specifies the options to be used as the `opts` parameter.

```js
module.exports = function (fastify, opts, next) {
Expand All @@ -332,6 +332,22 @@ Each plugin can be individually configured using the following module properties
export const autoConfig = { name: 'y' }
```

You can also use a callback function if you need to access the parent instance:
```js
export const autoConfig = (fastify) => {
climba03003 marked this conversation as resolved.
Show resolved Hide resolved
return { name: 'y ' + fastify.rootName }
}
```

However, note that the `prefix` option should be set directly on `autoConfig` for autoloading to work as expected:
```js
export const autoConfig = (fastify) => {
return { name: 'y ' + fastify.rootName }
}

autoConfig.prefix = '/hello'
```

- `plugin.autoPrefix` - Set routing prefix for plugin

```js
Expand Down
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,17 @@ async function loadPlugin ({ file, type, directoryPrefix, options, log }) {

const plugin = wrapRoutes(content.default || content)
const pluginConfig = (content.default && content.default.autoConfig) || content.autoConfig || {}
const pluginOptions = Object.assign({}, pluginConfig, overrideConfig)
let pluginOptions
if (typeof pluginConfig === 'function') {
pluginOptions = function (fastify) {
return { ...pluginConfig(fastify), ...overrideConfig }
}

pluginOptions.prefix = overrideConfig.prefix ?? pluginConfig.prefix
} else {
pluginOptions = { ...pluginConfig, ...overrideConfig }
}

const pluginMeta = plugin[Symbol.for('plugin-meta')] || {}

if (!encapsulate) {
Expand Down
20 changes: 19 additions & 1 deletion test/commonjs/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const t = require('tap')
const Fastify = require('fastify')

t.plan(101)
t.plan(107)

const app = Fastify()

Expand Down Expand Up @@ -270,4 +270,22 @@ app.ready(function (err) {
t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { works: true })
})

app.inject({
url: '/configPrefix'
}, function (err, res) {
t.error(err)

t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { configPrefix: true })
})

app.inject({
url: '/configPrefixCallback'
}, function (err, res) {
t.error(err)

t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { configPrefixCallback: true })
})
})
13 changes: 13 additions & 0 deletions test/commonjs/basic/foo/configPrefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

module.exports = function (f, opts, next) {
f.get('/', (request, reply) => {
reply.send({ configPrefix: true })
})

next()
}

module.exports.autoConfig = {
prefix: '/configPrefix'
}
14 changes: 14 additions & 0 deletions test/commonjs/basic/foo/configPrefixCallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

module.exports = function (f, opts, next) {
f.get('/', (request, reply) => {
reply.send({ configPrefixCallback: true })
})

next()
}

const options = () => ({})
options.prefix = '/configPrefixCallback'

module.exports.autoConfig = options
12 changes: 11 additions & 1 deletion test/commonjs/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
const t = require('tap')
const Fastify = require('fastify')

t.plan(19)
t.plan(22)

const app = Fastify()

app.decorate('root', 'root')

app.register(require('./options/app'))

app.ready(function (err) {
Expand Down Expand Up @@ -53,6 +55,14 @@ app.ready(function (err) {
t.same(JSON.parse(res.payload), { data: 'test-3' })
})

app.inject({
url: '/plugin-e'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { data: 'test-4-root' })
})

app.inject({
url: '/plugin-y'
}, function (err, res) {
Expand Down
17 changes: 17 additions & 0 deletions test/commonjs/options/plugins/plugin-e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

const fp = require('fastify-plugin')

function plugin (f, opts, next) {
f.get('/plugin-e', (request, reply) => {
reply.send({ data: opts.e })
})

next()
}

plugin.autoConfig = (fastify) => {
return { e: 'test-4-' + fastify.root }
}

exports.default = fp(plugin, { name: 'plugin-e' })
20 changes: 19 additions & 1 deletion test/module/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import t from 'tap'
import fastify from 'fastify'
import basicApp from './basic/app.js'

t.plan(74)
t.plan(80)

const app = fastify()

Expand Down Expand Up @@ -227,4 +227,22 @@ app.ready(function (err) {
statusCode: 404
})
})

app.inject({
url: '/configPrefix'
}, function (err, res) {
t.error(err)

t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { configPrefix: true })
})

app.inject({
url: '/configPrefixCallback'
}, function (err, res) {
t.error(err)

t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { configPrefixCallback: true })
})
})
13 changes: 13 additions & 0 deletions test/module/basic/foo/configPrefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

export default function (f, opts, next) {
f.get('/', (request, reply) => {
reply.send({ configPrefix: true })
})

next()
}

export const autoConfig = {
prefix: '/configPrefix'
}
12 changes: 12 additions & 0 deletions test/module/basic/foo/configPrefixCallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

export default function (f, opts, next) {
f.get('/', (request, reply) => {
reply.send({ configPrefixCallback: true })
})

next()
}

export const autoConfig = () => ({})
autoConfig.prefix = '/configPrefixCallback'
12 changes: 11 additions & 1 deletion test/module/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import t from 'tap'
import fastify from 'fastify'
import optionsApp from './options/app.js'

t.plan(19)
t.plan(22)

const app = fastify()

app.decorate('root', 'root')

app.register(optionsApp)

app.ready(function (err) {
Expand Down Expand Up @@ -52,6 +54,14 @@ app.ready(function (err) {
t.same(JSON.parse(res.payload), { data: 'test-3' })
})

app.inject({
url: '/plugin-e'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { data: 'test-4-root' })
})

app.inject({
url: '/plugin-y'
}, function (err, res) {
Expand Down
14 changes: 14 additions & 0 deletions test/module/options/plugins/plugin-e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fp from 'fastify-plugin'

function plugin (f, opts, next) {
f.get('/plugin-e', (request, reply) => {
reply.send({ data: opts.e })
})

next()
}

export default fp(plugin, { name: 'plugin-e' })
export const autoConfig = (fastify) => {
return { e: 'test-4-' + fastify.root }
}