fastify / fastify/fastify-autoload
Two fastify instances, two `indexPattern` that seems been ignored
- Dominant language
- JavaScript
- Stars
- 373
- Forks
- 77
- PR merge metrics
- No merged PRs in 30d
Description
### 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.12.0
### Plugin version
5.7.1
### Node.js version
v18.12.0
### Operating system
Linux
### Operating system version (i.e. 20.04, 11.3, 10)
Ubuntu 22.04.1 LTS
### Description
When starting the app it seems that `indexPattern` is ignored and all plugins are loaded also for `appV1Private` that has a different pattern regex.
Any idea?
### Steps to Reproduce
These are the relevant parts of the code
Main server, two Fastify instances. Each instance with own routes and own port.
```js
...
const server = fastify({})
const serverPrivate = fastify({})
...
server.register(appV1, { prefix: '/api/v1' })
serverPrivate.register(appV1Private, {prefix: '/api-private/v1'})
...
// public
server.listen({ port: 8443 }, (err, address) => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(`Public Server listening ${address}`)
})
// private
serverPrivate.listen({ port: 9443 }, (err, address) => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(`Private server listening ${address}`)
})
```
appV1
This should load only plugins with files like `module.ts`, right?
```js
import { FastifyPluginCallback, HTTPMethods } from 'fastify'
import { join } from 'path'
import fastifyAutoload from '@fastify/autoload'
import logger from '@root/utils/log';
const app: FastifyPluginCallback = function (fastify, opts, next): void {
/** AUTOLOAD PLUGINS */
fastify.register(fastifyAutoload, {
dir: join(__dirname, 'plugins'),
dirNameRoutePrefix: false,
indexPattern: /.*module(\.ts|\.js|\.cjs|\.mjs)$/,
maxDepth: 1,
encapsulate: false,
logLevel: 'info',
})
next()
}
export default app;
```
appV1***Private***
This should load only plugins with files like `modulePrivate.ts`, right?
```js
import { FastifyPluginCallback, HTTPMethods } from 'fastify'
import { join } from 'path'
import fastifyAutoload from '@fastify/autoload'
import logger from '@root/utils/log';
const app: FastifyPluginCallback = function (fastify, opts, next): void {
/** AUTOLOAD PLUGINS */
fastify.register(fastifyAutoload, {
dir: join(__dirname, 'plugins'),
dirNameRoutePrefix: false,
indexPattern: /.*modulePrivate(\.ts|\.js|\.cjs|\.mjs)$/,
maxDepth: 1,
encapsulate: false,
logLevel: 'info',
})
next()
}
export default app;
```
Inside plugins there are many Fastify plugins BUT only in one there is a file named `modulePrivate.ts`
When starting the app it seems that `indexPattern` is ignored and all plugins are loaded also for `appV1Private`
Any idea?
### Expected Behavior
- `appV1` should load only plugins with `module.ts` inside
- `appV1Private` should load only plugins with `modulePrivate.ts` inside.
Contributor guide
Assessment
This issue has not been assessed yet.