From 30c592c83d2ecf93de55edde5bb34f0a95a6df43 Mon Sep 17 00:00:00 2001 From: KcZer0 <6112497+KcZer0@users.noreply.github.com> Date: Thu, 28 Dec 2023 17:58:22 +0700 Subject: [PATCH 1/2] fix: include directoryPrefix when using autoPrefix --- index.js | 12 +++-- package.json | 5 +-- test/commonjs/basic.js | 45 +++++++++++++++++-- .../defaultPrefix/nested/defaultPrefix.js | 9 ++++ .../defaultPrefix/nested/noDefaultPrefix.js | 11 +++++ .../defaultPrefix/nested/overridePrefix.js | 13 ++++++ .../basic/defaultPrefix/nested/prefixed.js | 11 +++++ test/module/basic.js | 45 +++++++++++++++++-- .../defaultPrefix/nested/defaultPrefix.js | 9 ++++ .../defaultPrefix/nested/noDefaultPrefix.js | 11 +++++ .../defaultPrefix/nested/overridePrefix.js | 13 ++++++ .../basic/defaultPrefix/nested/prefixed.js | 11 +++++ 12 files changed, 183 insertions(+), 12 deletions(-) create mode 100644 test/commonjs/basic/defaultPrefix/nested/defaultPrefix.js create mode 100644 test/commonjs/basic/defaultPrefix/nested/noDefaultPrefix.js create mode 100644 test/commonjs/basic/defaultPrefix/nested/overridePrefix.js create mode 100644 test/commonjs/basic/defaultPrefix/nested/prefixed.js create mode 100644 test/module/basic/defaultPrefix/nested/defaultPrefix.js create mode 100644 test/module/basic/defaultPrefix/nested/noDefaultPrefix.js create mode 100644 test/module/basic/defaultPrefix/nested/overridePrefix.js create mode 100644 test/module/basic/defaultPrefix/nested/prefixed.js diff --git a/index.js b/index.js index 84707f9f..d31bb6e5 100644 --- a/index.js +++ b/index.js @@ -302,11 +302,17 @@ async function loadPlugin ({ file, type, directoryPrefix, options, log }) { pluginOptions.prefix = (pluginOptions.prefix && pluginOptions.prefix.endsWith('/')) ? pluginOptions.prefix.slice(0, -1) : pluginOptions.prefix const prefixOverride = plugin.prefixOverride !== undefined ? plugin.prefixOverride : content.prefixOverride !== undefined ? content.prefixOverride : undefined - const prefix = (plugin.autoPrefix !== undefined ? plugin.autoPrefix : content.autoPrefix !== undefined ? content.autoPrefix : undefined) || directoryPrefix + const autoPrefix = plugin.autoPrefix !== undefined ? plugin.autoPrefix : content.autoPrefix !== undefined ? content.autoPrefix : undefined + if (prefixOverride !== undefined) { pluginOptions.prefix = prefixOverride - } else if (prefix) { - pluginOptions.prefix = (pluginOptions.prefix || '') + prefix.replace(/\/+/gu, '/') + } else { + if (directoryPrefix !== undefined) { + pluginOptions.prefix = (pluginOptions.prefix || '') + directoryPrefix.replace(/\/+/gu, '/') + } + if (autoPrefix !== undefined) { + pluginOptions.prefix = (pluginOptions.prefix || '') + autoPrefix.replace(/\/+/gu, '/') + } } return { diff --git a/package.json b/package.json index 1b679b64..86348686 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,8 @@ "@types/jest": "^29.0.0", "@types/node": "^20.1.0", "@types/tap": "^15.0.5", + "esbuild": "^0.19.2", + "esbuild-register": "^3.4.1", "fastify": "^4.0.0-rc.2", "fastify-plugin": "^4.0.0", "jest": "^28.1.3", @@ -65,12 +67,9 @@ "tsm": "^2.2.1", "tsx": "^3.7.1", "typescript": "^5.0.2", - "esbuild": "^0.19.2", - "esbuild-register": "^3.4.1", "vite": "^5.0.0", "vitest": "^0.34.1" }, - "dependencies": {}, "standard": { "ignore": [ "test/*/*-error/lib/a.js" diff --git a/test/commonjs/basic.js b/test/commonjs/basic.js index 9ba5aec1..db66da20 100644 --- a/test/commonjs/basic.js +++ b/test/commonjs/basic.js @@ -3,7 +3,7 @@ const t = require('tap') const Fastify = require('fastify') -t.plan(101) +t.plan(115) const app = Fastify() @@ -60,7 +60,7 @@ app.ready(function (err) { }) app.inject({ - url: '/semiautomatic/items/1' + url: '/manualprefix/semiautomatic/items/1' }, function (err, res) { t.error(err) @@ -69,7 +69,7 @@ app.ready(function (err) { }) app.inject({ - url: '/semiautomatic/items' + url: '/manualprefix/semiautomatic/items' }, function (err, res) { t.error(err) @@ -144,6 +144,29 @@ app.ready(function (err) { t.equal(res.statusCode, 404) }) + app.inject({ + url: '/defaultPrefix/nested' + }, function (err, res) { + t.error(err) + t.equal(res.statusCode, 200) + t.same(JSON.parse(res.payload), { indexNested: true }) + }) + + app.inject({ + url: '/defaultPrefix/nested/prefixedNested' + }, function (err, res) { + t.error(err) + t.equal(res.statusCode, 200) + t.same(JSON.parse(res.payload), { prefixedNested: true }) + }) + + app.inject({ + url: '/defaultPrefix/nested/overriddenPrefix' + }, function (err, res) { + t.error(err) + t.equal(res.statusCode, 404) + }) + app.inject({ url: '/overriddenPrefix' }, function (err, res) { @@ -152,6 +175,14 @@ app.ready(function (err) { t.same(JSON.parse(res.payload), { overide: 'prefix' }) }) + app.inject({ + url: '/overriddenPrefixNested' + }, function (err, res) { + t.error(err) + t.equal(res.statusCode, 200) + t.same(JSON.parse(res.payload), { overide: 'prefixNested' }) + }) + app.inject({ url: '/noPrefix' }, function (err, res) { @@ -160,6 +191,14 @@ app.ready(function (err) { t.same(JSON.parse(res.payload), { no: 'prefix' }) }) + app.inject({ + url: '/noPrefixNested' + }, function (err, res) { + t.error(err) + t.equal(res.statusCode, 200) + t.same(JSON.parse(res.payload), { no: 'prefixNested' }) + }) + app.inject({ url: '/a/b/c' }, function (err, res) { diff --git a/test/commonjs/basic/defaultPrefix/nested/defaultPrefix.js b/test/commonjs/basic/defaultPrefix/nested/defaultPrefix.js new file mode 100644 index 00000000..c482af01 --- /dev/null +++ b/test/commonjs/basic/defaultPrefix/nested/defaultPrefix.js @@ -0,0 +1,9 @@ +'use strict' + +module.exports = function (f, opts, next) { + f.get('/', (request, reply) => { + reply.send({ indexNested: true }) + }) + + next() +} diff --git a/test/commonjs/basic/defaultPrefix/nested/noDefaultPrefix.js b/test/commonjs/basic/defaultPrefix/nested/noDefaultPrefix.js new file mode 100644 index 00000000..28fde4d5 --- /dev/null +++ b/test/commonjs/basic/defaultPrefix/nested/noDefaultPrefix.js @@ -0,0 +1,11 @@ +'use strict' + +module.exports = function (f, opts, next) { + f.get('/noPrefixNested', (request, reply) => { + reply.send({ no: 'prefixNested' }) + }) + + next() +} + +module.exports.prefixOverride = '' diff --git a/test/commonjs/basic/defaultPrefix/nested/overridePrefix.js b/test/commonjs/basic/defaultPrefix/nested/overridePrefix.js new file mode 100644 index 00000000..27e50bf1 --- /dev/null +++ b/test/commonjs/basic/defaultPrefix/nested/overridePrefix.js @@ -0,0 +1,13 @@ +'use strict' + +module.exports = function (f, opts, next) { + f.get('/', (request, reply) => { + reply.send({ overide: 'prefixNested' }) + }) + + next() +} + +module.exports.autoPrefix = '/notUsed' + +module.exports.prefixOverride = '/overriddenPrefixNested' diff --git a/test/commonjs/basic/defaultPrefix/nested/prefixed.js b/test/commonjs/basic/defaultPrefix/nested/prefixed.js new file mode 100644 index 00000000..990e99a1 --- /dev/null +++ b/test/commonjs/basic/defaultPrefix/nested/prefixed.js @@ -0,0 +1,11 @@ +'use strict' + +module.exports = function (f, opts, next) { + f.get('/', (request, reply) => { + reply.send({ prefixedNested: true }) + }) + + next() +} + +module.exports.autoPrefix = '/prefixedNested' diff --git a/test/module/basic.js b/test/module/basic.js index 1e55ad6c..76780611 100644 --- a/test/module/basic.js +++ b/test/module/basic.js @@ -2,7 +2,7 @@ import t from 'tap' import fastify from 'fastify' import basicApp from './basic/app.js' -t.plan(74) +t.plan(88) const app = fastify() @@ -57,7 +57,7 @@ app.ready(function (err) { }) app.inject({ - url: '/semiautomatic/items/1' + url: '/manualprefix/semiautomatic/items/1' }, function (err, res) { t.error(err) @@ -66,7 +66,7 @@ app.ready(function (err) { }) app.inject({ - url: '/semiautomatic/items' + url: '/manualprefix/semiautomatic/items' }, function (err, res) { t.error(err) @@ -152,6 +152,29 @@ app.ready(function (err) { t.equal(res.statusCode, 404) }) + app.inject({ + url: '/defaultPrefix/nested' + }, function (err, res) { + t.error(err) + t.equal(res.statusCode, 200) + t.same(JSON.parse(res.payload), { indexNested: true }) + }) + + app.inject({ + url: '/defaultPrefix/nested/prefixedNested' + }, function (err, res) { + t.error(err) + t.equal(res.statusCode, 200) + t.same(JSON.parse(res.payload), { prefixedNested: true }) + }) + + app.inject({ + url: '/defaultPrefix/nested/overriddenPrefix' + }, function (err, res) { + t.error(err) + t.equal(res.statusCode, 404) + }) + app.inject({ url: '/overriddenPrefix' }, function (err, res) { @@ -160,6 +183,14 @@ app.ready(function (err) { t.same(JSON.parse(res.payload), { overide: 'prefix' }) }) + app.inject({ + url: '/overriddenPrefixNested' + }, function (err, res) { + t.error(err) + t.equal(res.statusCode, 200) + t.same(JSON.parse(res.payload), { overide: 'prefixNested' }) + }) + app.inject({ url: '/noPrefix' }, function (err, res) { @@ -168,6 +199,14 @@ app.ready(function (err) { t.same(JSON.parse(res.payload), { no: 'prefix' }) }) + app.inject({ + url: '/noPrefixNested' + }, function (err, res) { + t.error(err) + t.equal(res.statusCode, 200) + t.same(JSON.parse(res.payload), { no: 'prefixNested' }) + }) + app.inject({ url: '/one/' }, function (err, res) { diff --git a/test/module/basic/defaultPrefix/nested/defaultPrefix.js b/test/module/basic/defaultPrefix/nested/defaultPrefix.js new file mode 100644 index 00000000..5b6cd53f --- /dev/null +++ b/test/module/basic/defaultPrefix/nested/defaultPrefix.js @@ -0,0 +1,9 @@ +'use strict' + +export default function (f, opts, next) { + f.get('/', (request, reply) => { + reply.send({ indexNested: true }) + }) + + next() +} diff --git a/test/module/basic/defaultPrefix/nested/noDefaultPrefix.js b/test/module/basic/defaultPrefix/nested/noDefaultPrefix.js new file mode 100644 index 00000000..b399fc27 --- /dev/null +++ b/test/module/basic/defaultPrefix/nested/noDefaultPrefix.js @@ -0,0 +1,11 @@ +'use strict' + +export default function (f, opts, next) { + f.get('/noPrefixNested', (request, reply) => { + reply.send({ no: 'prefixNested' }) + }) + + next() +} + +export const prefixOverride = '' diff --git a/test/module/basic/defaultPrefix/nested/overridePrefix.js b/test/module/basic/defaultPrefix/nested/overridePrefix.js new file mode 100644 index 00000000..95451c6a --- /dev/null +++ b/test/module/basic/defaultPrefix/nested/overridePrefix.js @@ -0,0 +1,13 @@ +'use strict' + +export default function (f, opts, next) { + f.get('/', (request, reply) => { + reply.send({ overide: 'prefixNested' }) + }) + + next() +} + +export const autoPrefix = '/notUsed' + +export const prefixOverride = '/overriddenPrefixNested' diff --git a/test/module/basic/defaultPrefix/nested/prefixed.js b/test/module/basic/defaultPrefix/nested/prefixed.js new file mode 100644 index 00000000..6fe3d9a5 --- /dev/null +++ b/test/module/basic/defaultPrefix/nested/prefixed.js @@ -0,0 +1,11 @@ +'use strict' + +export default function (f, opts, next) { + f.get('/', (request, reply) => { + reply.send({ prefixedNested: true }) + }) + + next() +} + +export const autoPrefix = '/prefixedNested' From 2db2d1909928df8e1cfb5f0bb87932fe53c92c7e Mon Sep 17 00:00:00 2001 From: KcZer0 <6112497+KcZer0@users.noreply.github.com> Date: Thu, 28 Dec 2023 21:31:19 +0700 Subject: [PATCH 2/2] docs(readme): autoPrefix behaviour with dirname prefix --- README.md | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a561972e..deed55a0 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,7 @@ Autoload can be customised using the following options: module.exports.autoPrefix = '/something' + // /plugins/something.mjs export default function (f, opts, next) { f.get('/', (request, reply) => { @@ -203,11 +204,35 @@ Autoload can be customised using the following options: next() } - export const autoPrefix = '/prefixed' + export const autoPrefix = '/something' // routes can now be added to /defaultPrefix/something ``` + When `options.dirNameRoutePrefix` is also set, `autoPrefix` will be concatenated *after* the directory prefix. + + ```js + // /plugins/someDir/something.js + module.exports = function (fastify, opts, next) { + // your plugin + } + module.exports.autoPrefix = '/prefixed' + + + // /plugins/someDir/something.mjs + export default function (f, opts, next) { + f.get('/', (request, reply) => { + reply.send({ something: 'else' }) + }) + + next() + } + + export const autoPrefix = '/prefixed' + + // routes are now added to /defaultPrefix/someDir/prefixed + ``` + - `autoHooks` (optional) - Apply hooks from `autohooks.js` file(s) to plugins found in folder Automatic hooks from `autohooks` files will be encapsulated with plugins. If `false`, all `autohooks.js` files will be ignored. @@ -357,7 +382,7 @@ Each plugin can be individually configured using the following module properties }) } - export const autoPrefix = '/prefixed' + export const autoPrefix = '/something' ```