From 7af44b573775696c6e22a990c3f19095f74b93d8 Mon Sep 17 00:00:00 2001 From: lprnmns Date: Sun, 30 Aug 2026 04:42:31 +0300 Subject: [PATCH] fix: allow autoConfig callbacks without global options Signed-off-by: lprnmns --- index.js | 2 +- test/commonjs/auto-config-no-global.js | 23 ++++++++++++++++++++ test/commonjs/auto-config-no-global/route.js | 7 ++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/commonjs/auto-config-no-global.js create mode 100644 test/commonjs/auto-config-no-global/route.js diff --git a/index.js b/index.js index 1d130676..6e884f03 100644 --- a/index.js +++ b/index.js @@ -247,7 +247,7 @@ function registerPlugin (fastify, meta, allPlugins, parentPlugins = {}) { meta.registered = true } -function loadPluginOptions (content, overrideConfig) { +function loadPluginOptions (content, overrideConfig = {}) { const pluginConfig = (content.default?.autoConfig) || content.autoConfig || {} if (typeof pluginConfig === 'function') { const pluginOptions = (fastify) => ({ ...pluginConfig(fastify), ...overrideConfig }) diff --git a/test/commonjs/auto-config-no-global.js b/test/commonjs/auto-config-no-global.js new file mode 100644 index 00000000..421ad312 --- /dev/null +++ b/test/commonjs/auto-config-no-global.js @@ -0,0 +1,23 @@ +'use strict' + +const { describe, it } = require('node:test') +const assert = require('node:assert') +const path = require('node:path') +const Fastify = require('fastify') +const autoLoad = require('../../') + +describe('callback autoConfig without global options', function () { + it('loads a plugin when the global options object is omitted', async function (t) { + const app = Fastify() + t.after(() => app.close()) + + app.register(autoLoad, { + dir: path.join(__dirname, 'auto-config-no-global') + }) + await app.ready() + + const res = await app.inject({ url: '/' }) + assert.strictEqual(res.statusCode, 200) + assert.deepStrictEqual(res.json(), { loaded: true }) + }) +}) diff --git a/test/commonjs/auto-config-no-global/route.js b/test/commonjs/auto-config-no-global/route.js new file mode 100644 index 00000000..a1dec345 --- /dev/null +++ b/test/commonjs/auto-config-no-global/route.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = async function (app) { + app.get('/', async () => ({ loaded: true })) +} + +module.exports.autoConfig = () => ({})