Skip to content

Commit

Permalink
Fix file path for filters (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoattal authored Jan 8, 2023
1 parent d8338d1 commit d9bbb0d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -226,22 +226,23 @@ 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 })
}
}
}
await Promise.all(directoryPromises)

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
}

Expand Down
2 changes: 1 addition & 1 deletion test/commonjs/ts-node/routes/bar/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports.default = async (fastify: any) => {
fastify.get("/", function () {
return { foo: "bar" };
return { bar: "bar" };
})
};
4 changes: 2 additions & 2 deletions test/commonjs/ts-node/routes/foo/baz/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports.default = async (fastify: any) => {
fastify.get("/", function () {
return { foo: "bar" };
fastify.get("/customPath", function () {
return { baz: "baz" };
})
};
2 changes: 1 addition & 1 deletion test/commonjs/ts-node/routes/foo/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports.default = async (fastify: any) => {
fastify.get("/", function () {
return { foo: "bar" };
return { foo: "foo" };
})
};
2 changes: 1 addition & 1 deletion test/vitest/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
})
})
19 changes: 18 additions & 1 deletion test/vitest/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down

0 comments on commit d9bbb0d

Please sign in to comment.