diff --git a/fastify-plugin.d.ts b/fastify-plugin.d.ts index 9a438e7..63fb2e2 100644 --- a/fastify-plugin.d.ts +++ b/fastify-plugin.d.ts @@ -12,7 +12,7 @@ import * as fastify from 'fastify'; * @param next The `next` callback is not available when using `async`/`await`. If you do invoke a `next` callback in this situation unexpected behavior may occur. */ declare function fastifyPlugin( - fn: fastify.Plugin, + fn: fastify.Plugin | { default: fastify.Plugin }, options?: fastifyPlugin.PluginOptions | string, next?: fastifyPlugin.nextCallback ): fastify.Plugin; diff --git a/index.js b/index.js index 6229799..e6d4c6c 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,10 @@ const console = require('console') const extractPluginName = require('./stackParser') function plugin (fn, options = {}) { + if (typeof fn.default !== 'undefined') { // Support for 'export default' behaviour in transpiled ECMAScript module + fn = fn.default + } + if (typeof fn !== 'function') { throw new TypeError(`fastify-plugin expects a function, instead got a '${typeof fn}'`) } diff --git a/test/test.js b/test/test.js index d44e3dc..1b21456 100644 --- a/test/test.js +++ b/test/test.js @@ -22,6 +22,21 @@ test('should return the function with the skip-override Symbol', t => { t.ok(plugin[Symbol.for('skip-override')]) }) +test('should support "default" function from babel module', t => { + t.plan(1) + + const plugin = { + default: () => {} + } + + try { + fp(plugin) + t.pass() + } catch (e) { + t.is(e.message, 'fastify-plugin expects a function, instead got a \'object\'') + } +}) + test('should throw if the plugin is not a function', t => { t.plan(1)