diff --git a/index.js b/index.js index 92cdcdf8..c6ac816a 100644 --- a/index.js +++ b/index.js @@ -171,7 +171,7 @@ async function findPlugins (dir, options, hookedAccumulator = {}, prefix, depth throw new Error(`@fastify/autoload cannot import hooks plugin at '${file}'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.`) } - accumulatePlugin({ file, type }, indexDirent.name) + accumulatePlugin({ file, type }) const hasDirectory = list.find((dirent) => dirent.isDirectory()) if (!hasDirectory) { @@ -226,7 +226,7 @@ async function findPlugins (dir, options, hookedAccumulator = {}, prefix, depth // Don't place hook in plugin queue if (!autoHooksPattern.test(dirent.name)) { - accumulatePlugin({ file, type }, dirent.name) + accumulatePlugin({ file, type }) } } } @@ -234,14 +234,15 @@ async function findPlugins (dir, options, hookedAccumulator = {}, prefix, depth return hookedAccumulator - function accumulatePlugin ({ file, type }, direntName = 'index.ts') { - const routePath = `${prefix ?? ''}/${direntName}` + function accumulatePlugin ({ file, type }) { + // Replace backward slash to forward slash for consistent behavior between windows and posix. + const filePath = '/' + path.relative(options.dir, file).replace(/\\/g, '/') - if (matchFilter && !filterPath(routePath, matchFilter)) { + if (matchFilter && !filterPath(filePath, matchFilter)) { return } - if (ignoreFilter && filterPath(routePath, ignoreFilter)) { + if (ignoreFilter && filterPath(filePath, ignoreFilter)) { return } diff --git a/test/commonjs/ts-node/routes/bar/index.ts b/test/commonjs/ts-node/routes/bar/index.ts index 96e7b557..32556790 100644 --- a/test/commonjs/ts-node/routes/bar/index.ts +++ b/test/commonjs/ts-node/routes/bar/index.ts @@ -1,5 +1,5 @@ module.exports.default = async (fastify: any) => { fastify.get("/", function () { - return { foo: "bar" }; + return { bar: "bar" }; }) }; diff --git a/test/commonjs/ts-node/routes/foo/baz/index.ts b/test/commonjs/ts-node/routes/foo/baz/index.ts index 96e7b557..7e1d53c9 100644 --- a/test/commonjs/ts-node/routes/foo/baz/index.ts +++ b/test/commonjs/ts-node/routes/foo/baz/index.ts @@ -1,5 +1,5 @@ module.exports.default = async (fastify: any) => { - fastify.get("/", function () { - return { foo: "bar" }; + fastify.get("/customPath", function () { + return { baz: "baz" }; }) }; diff --git a/test/commonjs/ts-node/routes/foo/index.ts b/test/commonjs/ts-node/routes/foo/index.ts index 96e7b557..f1021774 100644 --- a/test/commonjs/ts-node/routes/foo/index.ts +++ b/test/commonjs/ts-node/routes/foo/index.ts @@ -1,5 +1,5 @@ module.exports.default = async (fastify: any) => { fastify.get("/", function () { - return { foo: "bar" }; + return { foo: "foo" }; }) }; diff --git a/test/vitest/basic.test.ts b/test/vitest/basic.test.ts index 3d038428..cc4714e6 100644 --- a/test/vitest/basic.test.ts +++ b/test/vitest/basic.test.ts @@ -26,6 +26,6 @@ describe.concurrent("Vitest test suite", function () { url: '/foo' }) expect(response.statusCode).toBe(200) - expect(JSON.parse(response.payload)).toEqual({ foo: 'bar' }) + expect(JSON.parse(response.payload)).toEqual({ foo: 'foo' }) }) }) diff --git a/test/vitest/filter.test.ts b/test/vitest/filter.test.ts index 6f301e0f..41c6fbc4 100644 --- a/test/vitest/filter.test.ts +++ b/test/vitest/filter.test.ts @@ -79,7 +79,24 @@ describe.concurrent("Vitest match filters test suite", function () { test("Test /baz route", async function () { const response = await app.inject({ method: 'GET', - url: '/foo/baz' + url: '/foo/baz/customPath' + }) + expect(response.statusCode).toBe(200) + }) +}) + +describe.concurrent("Vitest match filters without prefix test suite", function () { + const app = Fastify() + app.register(AutoLoad, { + dir: join(__dirname, '../commonjs/ts-node/routes'), + dirNameRoutePrefix: false, + matchFilter: (path) => path.startsWith("/foo/baz") + }) + + test("Test /baz route", async function () { + const response = await app.inject({ + method: 'GET', + url: '/customPath' }) expect(response.statusCode).toBe(200) })