fastify-autoload
fastify-autoload copied to clipboard
autoConfig dependencies not respected
Prerequisites
- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the bug has not already been reported
Fastify version
4.26.1
Plugin version
4.5.1
Node.js version
20.5.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
13.2.1
Description
When I add dependencies to the autoConfig export for a plugin module, they do not seem to be consumed by this plugin.
This line seems to be the problem.
Can this be changed to use the dependencies config from autoConfig, or is there a good reason that it's omitted?
Steps to Reproduce
Define a plugin function and an autoConfig, load that with AutoPlugin, observe that it does not enforce dependency load order.
Expected Behavior
The plugin dependencies are respected when declared in autoConfig in the same way they are when a plugin is defined with fastify-plugin.
Thanks for reporting!
Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.
Alternatively a PR with the fix is also ok.
If you want to declare dependencies you should use fp plugin as mentioned in the doc.
autoConfig is only used for the plugin options, note that this is different from the fp options.
Maybe we can clarify the doc.
For example:
// options for fastifyMysql
export const autoConfig = (fastify: FastifyInstance) => {
return {
promise: true,
host: fastify.config.MYSQL_HOST,
user: fastify.config.MYSQL_USER,
password: fastify.config.MYSQL_PASSWORD,
database: fastify.config.MYSQL_DATABASE,
port: Number(fastify.config.MYSQL_PORT)
};
};
export default fp(fastifyMysql, {
// options for fp
name: "mysql"
});
Am I right @climba03003?
I don't think there is an interest to allow automatic configuration with fp, sounds overkill.
Here is my failure example, I have plugins in the plugins folder:
/plugins
auth.ts
config.ts
in auth.ts:
const authPlugin: FastifyPluginAsync = async (fastify) => {
console.log(fastify.config);
};
export default fp(authPlugin, {
decorators: {
fastify: ["config"],
},
dependencies: ["application-config"],
});
in config.ts
const configPlugin: FastifyPluginAsync = async (fastify) => {
fastify.decorate("config", {foo: "bar"});
};
declare module "fastify" {
interface FastifyInstance {
config: Config;
}
}
export default fp(configPlugin, { name: "application-config" });
and in my server.ts, I autoload these plugins:
// Register plugins
await server.register(autoload, {
dir: path.join(__dirname, "plugins"),
dirNameRoutePrefix: false,
ignorePattern: /.*.no-load\.ts/,
indexPattern: /^no$/i,
});
When I start the application, I got this error:
/app/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:859
return new TSError(diagnosticText, diagnosticCodes, diagnostics);
^
TSError: ⨯ Unable to compile TypeScript:
src/plugins/auth.ts(9,23): error TS2339: Property 'config' does not exist on type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>'.
at createTSError (/app/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:859:12)
at reportTSError (/app/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:863:19)
at getOutput (/app/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:1077:36)
at Object.compile (/app/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:1433:41)
at Module.m._compile (/app/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:1617:30)
at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)
at Object.require.extensions.<computed> [as .ts] (/app/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:1621:12)
at Module.load (node:internal/modules/cjs/loader:1203:32)
at Function.Module._load (node:internal/modules/cjs/loader:1019:12)
at Module.require (node:internal/modules/cjs/loader:1231:19) {
diagnosticCodes: [ 2339 ]
If my change the order of the plugin files in my /plugins folder by prefixing config.ts with _:
/plugins
_config.ts
auth.ts
Then everything works fine, and in my auth.ts plugin, I can access the config via the fastify instance.
Ha sorry, I commented too quickly, in your case this is a typescript issue. And I was wrong regarding dependencies so I removed my message.
@artekr
Do you have this problem while running your test with ts-node or another ts JIT transpiler?
If you put this in auth.ts instead of config.ts, do you still have the issue?
declare module "fastify" {
interface FastifyInstance {
config: Config;
}
}
The issue here is probably that your JIT transpiler is not yet aware of type declaration in config.ts while running auth.ts.
If you use ts-node, you can add this configuration to your tsconfig.json or pass them as command line arguments:
{
"compilerOptions": {},
"ts-node": {
"files": true
},
}
Next time please link us a repository with precise steps to reproduce the bug.
{ "compilerOptions": {}, "ts-node": { "files": true }, }
Yes, I'm using ts-node for development; adding this to my tsconfig.json file does the trick! Thanks very much, @jean-michelet! you saved my day 😃
@thyming
Can we close this issue?