Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: include directoryPrefix when using autoPrefix #350

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
45 changes: 42 additions & 3 deletions test/commonjs/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const t = require('tap')
const Fastify = require('fastify')

t.plan(101)
t.plan(115)

const app = Fastify()

Expand Down Expand Up @@ -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)

Expand All @@ -69,7 +69,7 @@ app.ready(function (err) {
})

app.inject({
url: '/semiautomatic/items'
url: '/manualprefix/semiautomatic/items'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change necessary due to the change in the fixture, or is this a breaking change?

Can you please not alter existing tests?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry. Yes it is breaking change.

The behaviour between dirNameRoutePrefix and autoPrefix was not properly defined in the docs before this change.

Copy link
Author

@KcZer0 KcZer0 Dec 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the intended behaviour in the changed test is to override directoryPrefix, but keep options.prefix, I think a new exported option plugin.directoryOverride would be better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid a breaking change, thanks.

}, function (err, res) {
t.error(err)

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions test/commonjs/basic/defaultPrefix/nested/defaultPrefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

module.exports = function (f, opts, next) {
f.get('/', (request, reply) => {
reply.send({ indexNested: true })
})

next()
}
11 changes: 11 additions & 0 deletions test/commonjs/basic/defaultPrefix/nested/noDefaultPrefix.js
Original file line number Diff line number Diff line change
@@ -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 = ''
13 changes: 13 additions & 0 deletions test/commonjs/basic/defaultPrefix/nested/overridePrefix.js
Original file line number Diff line number Diff line change
@@ -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'
11 changes: 11 additions & 0 deletions test/commonjs/basic/defaultPrefix/nested/prefixed.js
Original file line number Diff line number Diff line change
@@ -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'
45 changes: 42 additions & 3 deletions test/module/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)

Expand All @@ -66,7 +66,7 @@ app.ready(function (err) {
})

app.inject({
url: '/semiautomatic/items'
url: '/manualprefix/semiautomatic/items'
}, function (err, res) {
t.error(err)

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions test/module/basic/defaultPrefix/nested/defaultPrefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

export default function (f, opts, next) {
f.get('/', (request, reply) => {
reply.send({ indexNested: true })
})

next()
}
11 changes: 11 additions & 0 deletions test/module/basic/defaultPrefix/nested/noDefaultPrefix.js
Original file line number Diff line number Diff line change
@@ -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 = ''
13 changes: 13 additions & 0 deletions test/module/basic/defaultPrefix/nested/overridePrefix.js
Original file line number Diff line number Diff line change
@@ -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'
11 changes: 11 additions & 0 deletions test/module/basic/defaultPrefix/nested/prefixed.js
Original file line number Diff line number Diff line change
@@ -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'