Skip to content

plugin.autoConfig not working with TypeScript compiled code #61

@fox1t

Description

@fox1t

🐛 Bug Report

A valid plugin declaration is not working since fastify-autoload only checks if plugin.autoConfig is defined, but export default plugin compiles to exports.default = in JS.
Checking the code there are already places where fastify-autoload searches for plugin.default property (L169).

To Reproduce

Declare a new fastify plugin written like this in TS:

import { FastifyInstance, FastifyError } from 'fastify'
import fp from 'fastify-plugin'
import nano, { ServerScope, Configuration } from 'nano'

function couchDB(
  fastify: FastifyInstance,
  options: Configuration,
  next: (err?: FastifyError | undefined) => void,
) {
  console.log(options)
  const couch = nano(options)
  fastify.decorate('couch', couch)
  next()
}

couchDB.autoConfig = {
  url: 'http://localhost:5984',
}

export default fp(couchDB)

declare module 'fastify' {
  interface FastifyInstance {
    couch: ServerScope
  }
}

This compiles to:

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
const nano_1 = __importDefault(require("nano"));
function couchDB(fastify, options, next) {
    console.log(options);
    const couch = nano_1.default(options);
    fastify.decorate('couch', couch);
    next();
}
couchDB.autoConfig = {
    url: 'http://localhost:5984',
};
exports.default = fastify_plugin_1.default(couchDB);

As you can see the module lacks of module.exports and just have exports.default.

An alternative method to fix this bug could be using the "legacy" export = syntax in TS.

import { FastifyInstance, FastifyError } from 'fastify'
import fp from 'fastify-plugin'
import nano, { ServerScope, Configuration } from 'nano'

function couchDB(
  fastify: FastifyInstance,
  options: Configuration,
  next: (err?: FastifyError | undefined) => void,
) {
  console.log(options)
  const couch = nano(options)
  fastify.decorate('couch', couch)
  next()
}

couchDB.autoConfig = {
  url: 'http://localhost:5984',
}

export = fp(couchDB)

declare module 'fastify' {
  interface FastifyInstance {
    couch: ServerScope
  }
}

that compiles to:

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
const nano_1 = __importDefault(require("nano"));
function couchDB(fastify, options, next) {
    console.log(options);
    const couch = nano_1.default(options);
    fastify.decorate('couch', couch);
    next();
}
couchDB.autoConfig = {
    url: 'http://localhost:5984',
};
module.exports = fastify_plugin_1.default(couchDB);
//# sourceMappingURL=couchdb.js.map

However many developers don't even know this syntax exists or, even worse, what it does. In addition to that, if it remains like this, fastify-autoload will force developers to use two different export methods, depending if or not the plugin will be autoloaded by fastify-autoload.

Expected behavior

The plugin.autoConfig, when declared on plugin.default.autoConfig is added to the options and passed to the plugin.

Your Environment

  • node version: v12.14.0
  • fastify version: 2.11.0
  • fastify-autoload version: 1.2.0
  • os: macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions